Need help with random number arrays

okay so im trying to finish this project for school and im having trouble here are the requirements

1.Write a program to generate 100 random numbers (rand() function) and output them to the console.

2.Write a program to generate 100 random numbers and calculate their sum and average. Output the random numbers to the console, ten random numbers on each output line, and then output the sum and average of the numbers.

3.Generate 100 random numbers and calculate their sum and average. Create an array and store the numbers in the array. The sum and average should be the two last values in the array.

4.Generate 100 random numbers and calculate their sum and average. Create an array and store the numbers in the array. The sum and average should be the two last values in the array.
Output the random numbers to the console, ten random numbers on each output line, and then output the sum and average of the numbers.
Sort the random numbers in the array in ascending order (smallest to largest). The sum and average are not part of the sort.
Output the sorted numbers to the console, ten numbers on each line.

5.Generate 100 random numbers and calculate their sum and average. Create an array and store the numbers in the array. The sum and average should be the two last values in the array.
Output the random numbers to the console, ten random numbers on each output line, and then output the sum and average of the numbers. Open a file and send the same output to the file as you sent to the console.
Sort the random numbers in the array in ascending order (smallest to largest). The sum and average are not part of the sort.
Output the sorted numbers to the console, ten numbers on each line. Send the sorted output to the file, as well. It should contain the unsorted numbers, their sum and average, and the sorted numbers.


now i have the first one done but i got really lost after that this is what my code is like for the first part

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int i;
        for (i = 1; i <= 100; i++) {
	printf("%d ", 1 + (rand() % RAND_MAX));
	if (i % 5 == 0) {
			printf("\n");
		}
	} 
	return 0; 
}


but after that i am lost on how to take the output and find their sum and average. im very new to c++ and have only been working with it for about a month so any help or advice would be greatly appreciated.
You could modify the code you have for part one like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() 
{
    
    for (int i = 1; i <= 100; i++) 
    {
        int rnd = rand();
        
        printf("%d ", rnd);
        
        if (i % 5 == 0) 
        {
            printf("\n");
        }
    } 
    return 0; 
}


The important change is line 6. Here a new variable which I called rnd is added.

There's no need to do this: (rand() % RAND_MAX) because the numbers are already in the range 0 to RAND_MAX, that extra step doesn't add anything useful.

But that's a side-issue. The important thing is, now you have the number stored in the variable rnd, not only can you print it out, but you can still do other things with its value too.

Here, you need to introduce a new variable, before the start of the loop, insert this line:
 
    int total = 0;


Then, inside the loop, each time you create a new random number, add it to total.

That will allow you, after the loop has completed, to calculate the average and print out the results.
Here you go:

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;

void Display(int numbers[])
{
    for(int i = 0;i < 100;i++)
    {
        cout << numbers[i] << '\t';
        if(i % 10 == 0)
        {
            cout << endl;
        }
    }

    cout << endl << endl;

    cout << "Sum: " << numbers[100] << endl;
    cout << "Average: " << numbers[101] << endl;
}

void sortData(int numbers[])
{
    int nSwaps = 1;
    int temp;
    while(nSwaps != 0)
    {
        nSwaps = 0;

        for(int i = 0; i < 100; i++)
        {
            if(numbers[i] > numbers[i+1])
            {
                temp = numbers[i+1];
                numbers[i+1] = numbers[i];
                numbers[i] = temp;

                nSwaps++;
            }
        }
    }
}

int main(int nNumberofArgs,char* pszArgs[])
{
    int numbers[102];
    int total = 0;
    double average = 0;

    srand((unsigned)time(0));
    for(int i = 0;i < 100;i++)
    {
        numbers[i] = rand() % 100 + 1;
    }

    for(int i = 0;i < 100;i++)
    {
        total += numbers[i];
    }

    average = total / 100;

    numbers[100] = total;
    numbers[101] = average;

    char filename[256];
    cout << "What data file would you like to store your info?" << endl;
    cout << "Filename: ";
    cin.getline(filename,256);

    for(int s = 0;s != '\n';s++)
    {
        if(filename[s] == NULL)
        {
            filename[s] = '.';
            filename[s + 1] = 't';
            filename[s + 2] = 'x';
            filename[s + 3] = 't';
            filename[s + 4] = NULL;
            break;
        }
    }

    cout << "Opening and storing contents into: " << filename << endl;
    cout << "Unsorted set: " << endl;
    Display(numbers);

    ofstream data;
    data.open(filename, fstream::in|fstream::app);

    data << "Unsorted numbers: \n";
    for(int i = 0;i < 100;i++)
    {
        data << numbers[i] << '\t';
        if(i % 10 == 0)
        {
            data << '\n';
        }
    }

    sortData(numbers);
    cout << endl << "Sorted set: " << endl;
    Display(numbers);

    data << "Sorted numbers: \n";
    data << "Unsorted numbers: \n";
    for(int i = 0;i < 100;i++)
    {
        data << numbers[i] << '\t';
        if(i % 10 == 0)
        {
            data << '\n';
        }
    }

    data << "Sum: " << numbers[100] << '\n';
    data << "Average: " << numbers[101] << '\n';
    data << "---------------------------------------" << '\n';

    system("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.