Array help

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.

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 
        else if (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"
Can it be done?
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.
Something like:

1
2
3
4
for ( i =1 ; i < 10 )
{
cout << endl ;
}



How do you reset counts?
No. Use an extra variable to count the number of displayed numbers per row and use an IF statement to control whether you output an endl or a space.
Or, decide on remainder == 0 (and skip the special case of i==0)
1
2
3
4
 if ( i % 10 == 0 && i > 0)
   std::cout << std::endl;
 else 
   std::cout << " ";

Topic archived. No new replies allowed.