Help on Arrays

Apr 29, 2013 at 8:00pm
Well here's what I got. I need help to be able to use negative numbers and also it keeps giving me the average and sum for all 10 numbers but I want it to do that for all the numbers combined.

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
#include <iostream>
#include <cmath>
using namespace std;

const int SIZE = 10;


int main()
{

	double myArray[SIZE];


	for (int i=0; i<10; i++)
	{
		cout << "Enter a valid number: " << endl;
		cin >> myArray[i];
	}


	
	for (unsigned int i=0;i<10; i++)
	{
		int sum = 0;
		sum += myArray[i];
		double avg = sum/10;

		cout<<"The sum is: "<<sum<<endl;
		cout<<"The average is: "<<avg<<endl;
		
	}
	
	
	double smallest = 99999999;
	double largest = 0;
	for (size_t i = 0; i != SIZE; ++i)
	{
		if (myArray[i] <= smallest)
        smallest = myArray[i];

		if (myArray[i] >= largest)
		largest = myArray[i];
		
		cout<<"The smallest is: "<<smallest<<endl;
		cout<<"The largest is: "<<largest<<endl;
	}
	return 0;
	
}
 

Apr 29, 2013 at 8:03pm
If I have correctly understood you need the following

1
2
3
4
5
6
7
8
9
10
	double sum = 0;
	for (unsigned int i=0;i<10; i++)
	{
		sum += myArray[i];
	}

	double avg = sum/10;

	cout<<"The sum is: "<<sum<<endl;
	cout<<"The average is: "<<avg<<endl;

Apr 29, 2013 at 9:33pm
That helped just a little. But there's something wrong with the smallest/largest part and it keeps giving me the smallest and largest for every single number
Apr 29, 2013 at 9:53pm
Look at how vlad did change your average calculation and reporting. What did he do there? Same solution applies to the min/max.
Topic archived. No new replies allowed.