I have an interactive program that creates random numbers. The user defines the amount of numbers and the range of the numbers. What is happening is that all the results output in one long column. I need for the output to be in rows of 10. For example, if the user wants 55 random numbers, the first-fifth row would have 10 outputs and the last row would have 5.
int main()
{
srand ((int) time (NULL));
int low, high ;
double amount ; // declaration of double type variables
cout << "How many random numbers(Choose between 1 and 250)?: " << endl ;
cin >> amount ;
if (amount > 250 )
{
cout << "That entry is invalid, please re-enter a number between 1 and 250: " ;
}
else
cout << "What is the low end of the range?: " << endl ;
cin >> high ;
cout << "What is the high end of the range?: " << endl ;
cin >> low ;
if (high > low || low < high)
{
cout << "That range is invalid, re-enter low end of range: " ;
cin >> high ;
cout << "Re-enter high end: " ;
cin >> low ;
}
int random_number = 0;
int largest = high - 1; //value limits
int smallest = low + 1; //value limits
int sum = 0;
for (int i = 1; i < amount + 1 ; i++)
{
random_number = rand() % low + 1; //amount of random numbers
cout << "Random # " << i << ": " << random_number << endl; //prints random_number
sum += random_number; //sum = sum + random_number
if (random_number > largest) //if random_number is bigger than largest
largest = random_number; //its now largest
elseif (random_number < smallest) //else if its smaller than smallest
smallest = random_number; //its now smallest
}
cout << "Largest number is: " << largest << endl //prints largest
<< "Smallest number is: " << smallest << endl //prints smallest
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< "Average is: " << setprecision(3) << sum / amount << endl; //prints average to 3 decimal places
return(0); // integer zero returned
} // closes container "main"
In your display loop, count the number of numbers you have printed and then decide: If you have printed 10 numbers already, then print an endl and reset the count, but if not just print a space and increase the count by one.