Random Number Gen. w/ Math Equations

I'm having many problems with my Homework . . I'm trying to make a code that generates 10 Random Numbers and then prints the Sum of all 10 of them, as well as print their Average, and the Largest & Smallest of all Numbers.

Here is what I have so far. I know how to generate 10 Random Numbers. My problem is printing out the fields for avg, large and small. Been looking all over the internet and i'm not having much luck. . Don't have the greatest teacher either . .

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <Windows.h>

using namespace std;


int main()
{
	int Smith[10];
	int k;
	int MrHolder; //for specific rand. number gen.

        k = 0;

	while (k < 10) // gen. rand. numbers between 10 and 99
	{
		MrHolder = rand();
		if (MrHolder > 9 && MrHolder < 100)
		{
			Smith[k] = MrHolder;
			k++;
		}
	}
	k = 0;
	while (k < 10)
	{
		cout << " " << Smith[k];
		k++;
	}
	
	cout << "\t\t\tSum of Numbers       : " << ; // print the Sum of all Random Numbers 
	cout << "\t\t\tAverage of Numbers   : " << ; // print the Average Number of all Random Numbers
	cout << "\t\t\tBiggest of Numbers   : " << ; // print the Largest of all Random Numbers
	cout << "\t\t\tSmallest of Numbers  : " << ; // print the Smallest of all Random Numbers
	
	cout << "\n\n\n\n\n\t\t\t";
	cout << system("pause");
	cout << system("cls");
Last edited on
(1) Use functions. Put all that stuff that fills Smith[] with random numbers in a function. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
void fill_array_with_random_numbers_in_range( int* a, int n, int min, int max )
{
	int k = 0;
	while (k < n)
	{
		int x = rand();
		if (x >= min && x <= max)
		{
			a[k] = x;
			k++;
		}
	}
}

This makes life easy in main():

1
2
3
4
5
6
7
int main()
{
	int Smith[10];

	srand( time( 0 ) );  // (You forgot to randomize your PRNG like this)

	fill_array_with_random_numbers_in_range( Smith, 10, 10, 99 );

Now you need functions to return the sum of numbers in your array. From that you can calculate the average. And a simple function (or two) to find the largest and smallest.

8
9
10
	cout << "\t\t\tSum of Numbers        : " << sum_of_array( Smith, 10 ) << "\n";
	cout << "\t\t\tAverage of Numbers   : " << average_of_array( Smith, 10 ) << "\n";
	//etc 

Help with the function prototypes:

1
2
3
int sum_of_array( int* a, int n );
double average_of_array( int* a, int n );
//etc 

Now for your question. How do you find the sum of an array. Suppose I give you:

    2,3,5,7,11

What is the sum? Easy: 0+2+3+5+7+11. Use a loop.

To compute the average, you need to know the sum (which you now have a function to compute) and the number of elements in the array, right?

To find the largest number in an array, what do you do?

    3, -7, 15, 12, 45, -97, 18

What do you do, in your own head, to find the largest? Make the computer do the same thing.

Hope this helps.
Topic archived. No new replies allowed.