Почему не записывается минимальный элемент?
В выводе дает мусорное значение.
// Runtime.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
int main()
{
srand(time(0));
const int arraySize = 200000;
int array[arraySize];
int counter = 0;
int min = array[0];
for (counter; counter < arraySize; counter++)
{
array[arraySize] = rand() % 50 - rand() % 50;
std::cout << array[arraySize] << " ";
}
for (counter = 1; counter < arraySize; counter++)
{
if (array[counter] < min)
min = array[counter]; // почему не минимальный элемент?
}
std::cout << "\nmin: " << min << std::endl;
std::cout << "\nruntime: " << clock() / 1000.0 << std::endl;
system("pause");
return 0;
}
Ответы (1 шт):
Автор решения: Антон
→ Ссылка
Потому что в массиве изначально ничего не записывается
#include "stdafx.h"
#include <iostream>
#include <ctime>
int main()
{
srand(time(0));
const int arraySize = 200000;
int array[arraySize];
int counter = 0;
int min = array[0];
for (counter; counter < arraySize; counter++)
{
array[arraySize] = rand() % 50 - rand() % 50; // !!! ВЫХОД ЗА ГРАНИЦЫ МАССИВА
std::cout << array[arraySize] << " "; // !!! ВЫХОД ЗА ГРАНИЦЫ МАССИВА
}
for (counter = 1; counter < arraySize; counter++)
{
if (array[counter] < min)
min = array[counter]; // почему не минимальный элемент?
}
std::cout << "\nmin: " << min << std::endl;
std::cout << "\nruntime: " << clock() / 1000.0 << std::endl;
system("pause");
return 0;
}