Putting 100 random integers into an array and displaying the sum,average etc.

Hello Guys!! Everything about my code is working perfectly fine the only thing Im trying to figure out is why when I try to print 10 integers per line I am unable to do so! Under displayData I am unable to make it so only 10 integers per line is the output and it is organized neatly any help is appreciated! Thank you!

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
using namespace std;

//Function Prototypes
void arraySort(int[], int);
void displayData(int[], int);

int main()
{
    //Declared Variables
    srand(NULL);
    int random[100];
    int randomNumber;
    int sum = 0;
    double medianVal;
    double average;
    
    // Generates 100 random numbers from
    //500-1000
    for (int x = 0; x < 100; x++)
    {
        randomNumber = (rand() % 500) + 500;
        random[x] = randomNumber;
    }
    
    // Calls arraySort function to sort in ascending order
    arraySort(random, 100);
    
    // Displays 100 random numbers sorted
    cout << "Here are the sorted 100 numbers!\n" << endl;
    displayData(random, 100);
    
    // Prints smallest number from 100 random numbers
    cout << "\nThe smallest number is: " << random[0] << endl;
    
    // Prints largest number from 100 random numbers
    cout << "\nThe largest number is: " << random[99] << endl;

    // Median value calculation and then displays
    // the median value
    medianVal = (random[49]+random[50])/2;
    cout << "\nThe median of all the values is: " << medianVal << endl;
    
    // The sum of all 100 numbers
    for (int i = 0;i<100; i++)
    {
        sum+=random[i];
    }
    
    cout << "\nThe sum of all the numbers is: " << sum << endl;
    
    //average of all 100 numbers
    average = sum/100;
    
    cout << "\nThe average is: " << average << endl;
    
    return 0;
}

// Function used to strt array by using a temp variable
// This swaps all numbers to put them in ascending order
void arraySort(int array[], int num)
{
    int temp;
    bool swap;
    
    do
    {
        swap = false;
        for (int x = 0; x < (num - 1); x++)
        {
            if (array[x] > array[x + 1])
            {
                temp = array[x];
                array[x] = array[x + 1];
                array[x + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

// prints 100 random numbers from smallest to largest
void displayData(int array[], int num)
{
    for (int c = 0; c < num; ++c)
        cout << array[c] << " ";
    if ((c+1) % 10 == 0){
        cout << endl;
        
    }
}
it worked for me, although I had to add in a couple brackets...


1
2
3
4
5
6
7
8
9
10
// prints 100 random numbers from smallest to largest
void displayData(int array[], int num)
{
    for (int c = 0; c < num; ++c){//added bracket
        cout << array[c] << " ";
        if ((c+1) % 10 == 0){
            cout << endl;
        }
    }//added bracket
}



here is the output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Here are the sorted 100 numbers!

505 511 512 522 526 527 530 537 539 540 
542 543 545 558 559 560 567 567 569 582 
584 584 586 587 591 594 623 624 635 649 
651 667 670 672 676 678 690 698 711 726 
729 729 736 739 750 754 763 776 777 781 
782 784 788 793 802 805 808 813 814 815 
824 827 835 836 846 857 862 862 862 864 
867 868 868 870 873 873 883 886 886 893 
895 899 903 913 915 919 921 921 925 926 
926 929 929 932 934 956 956 980 992 996 

The smallest number is: 505

The largest number is: 996

The median of all the values is: 781

The sum of all the numbers is: 75184

The average is: 751
Last edited on
Topic archived. No new replies allowed.