Number Analysis Program

In my computer science class we are working on this program and I really feel like I am having trouble with arrays, I have barely gotten started because I have no clue how to actually get it to display lowest and highest numbers, nevertheless the average. Any help is GREATLY appreciated.

Number Analysis Program: Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array (use the arrays as we've discussed in class--DO NOT use any list class) and then display the following data:
- The lowest number in the array
- The highest number in the array
- The total of the numbers in the array
- The average of the numbers in the array

Again, I've barely started but I'm already so stuck.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	//Declarations
	const int SIZE = 20;
	string numbers[SIZE];
	int smallest;
	int i = 0;
	int userNumber;

	//Get User Numbers
	for (i = 0; i < SIZE; i++)
	{
		cout << "Enter 20 Numbers Please: ";
		cin >> numbers[i];
		if (numbers[i] < numbers[i + 1])
		{
			cout << numbers[i];
		}
	}
	return 0;
}
if (numbers[i] < numbers[i + 1])
At i = 0, you only have 1 element in your array, so your program should crash.

You should ask the user to input a number first, store that at index 0 and then start your loop at i = 1.

Pseudo-code
1
2
3
4
5
6
7
8
9
10
11
12
// declarations
int numbers[SIZE]        // why are you using a string array?, should be an int to store numbers
ask user to input number
store it in numbers[0]
total += numbers[0]

for(int i = 1; i < SIZE; i++)
        ask user for another number
        store it in numbers[i]
        if(numbers[i] < numbers[i-1])
                lowest = numbers[i]
        total += numbers[i]        // cumulative total 
Last edited on
Below, I put in the comments on the code some info to help you get start it.

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
int main()
{
	//Declarations
	const int SIZE = 20;
	string numbers[SIZE]; // You are working with numbers; therefore, switch the string type.
	                      // to a numerical type (i.e. int)
	int smallest; 
	int i = 0;
	int userNumber; // You are not using this variable in the program. Do you need it?
	                // if not, then get rid of it.

	//Get User Numbers
	for (i = 0; i < SIZE; i++)
	{
		cout << "Enter 20 Numbers Please: ";
		cin >> numbers[i];
		//if (numbers[i] < numbers[i + 1]) // This if statement is not need it.
		//{
		cout << numbers[i]; // I recommend you add the << endl or space to separate the
		                    // numbers during the display.
		//}
	}

	// To find the lowest number in the array.
	// - Initialized the smallest variable with the first element of the array.
	// - Run a for loop that begins with the second element and will stop at less than
	// the size of the elements in the array.
	// - Put an if statement inside the for loop to check if the numbers is less than
	// the smallest number, then that number will be assigned to the smallest number
	// variable.

	return 0;
}


To find the highest number, it is nearly identical to find the lowest number.

You may need a running total to obtain the total numbers of the array. Hence, the average, as well.
Programs that calculate the total of a series of numbers typically use two elements:
- A loop that reads each number in the series.
- A variable that accumulates the total of the numbers as they are read. The variable that is used to accumulate the total of the numbers is called an accumulator . It is often said that the loop keeps a running total because it accumulates the total as it reads each number in the series.

So the average will be the running total variable / total number of elements

I hope it helps.

Edited: integralfx gives you a great example.
Last edited on
Topic archived. No new replies allowed.