#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int number = 0;
int count = 0;
float average = 0;
int largest = -1;
int smallest = -1;
int *array;
cout << "Please enter a number between 20 and 100: ";
cin >> number;
if (number > 20 && number < 100)
{
array = newint[number];
srand((unsigned)time(0));
int random;
for (int index = 0; index < number; index++)
{
random = (rand() % 1000) + 1;
}
}
else
{
cout << "The number must be between 20 and 100. \n";
cout << "Please enter a number between 20 and 100: " << endl;
cin >> number;
}
system("Pause");
return 0;
}
int k = 0;
array = newint[number];
srand((unsigned)time(0));
int random;
for (int index = 0; index < number; index++)
{
random = (rand() % 1000) + 1;
array[k] = random; k++;
}
My code is compiling but when it runs I'm getting an infinite loop of 168. Anything obvious I am doing wrong? I tried adding code so it can display the contents of the array in lines of 10.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int number = 0;
int count = 0;
float average = 0;
int largest = -1;
int smallest = -1;
int *array;
cout << "Please enter a number between 20 and 100: ";
cin >> number;
if (number >= 20 && number <= 100)
{
int k = 0;
array = newint[number];
srand((unsigned)time(0));
int random;
for (int index = 0; index < number; index++)
{
random = (rand() % 1000) + 1;
array[k] = random; k++;
}
for (int i = 0; i < 1000;)
{
cout << array[i] << "";
if ((i + 1) % 10 == 0)
{
cout << endl;
}
}
}
else
{
cout << "The number must be between 20 and 100. \n";
cout << "Please enter a number between 20 and 100: " << endl;
cin >> number;
}
system("Pause");
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cassert>
usingnamespace std;
int main( /* int argc, char** argv */ ) {
int
number = 0,
count = 0,
largest = -1,
smallest = -1,
i = 0,
random = 0;
float average = 0.0;
int* array = nullptr;
// do...while() is REALLY good for verifying input
do {
cout << "Please enter a number between 20 and 100: ";
cin >> number;
} while(number < 20 || number > 100);
array = newint[number];
assert(array); // Allocation check
srand( (unsigned)time(0) );
// Why did you have two loop counters for this? Only one is needed
for(i = 0; i < number; i++) {
random = ( rand() % 1000 ) + 1;
array[i] = random;
} // END for(i)
for (i = 0; i < number; i++) {
cout << array[i] << ' ';
if( (i + 1) % 10 == 0) {
cout << '\n';
} // END if
} // END for(i)
return 0;
} // END main