sum and average of array

this is what code i have and i need help finishing thanks!!!

Write a program that demonstrates using an array to perform some simple statistics. Your program should declare a 10 element double array, use one loop to read 10 values from the user and store them in the array, use a second loop to compute the sum and average of the values in the array, use a third loop to find the largest and smallest values in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()

//int _tmain(int argc, _TCHAR* argv[])
{
	int myarray [10];
	cout << " enter a number ";
	cin >> myarray[0];
	cin >> myarray[1];
	cin >> myarray[2];
	cin >> myarray[3];
	cin >> myarray[4];
	cin >> myarray[5];
	cin >> myarray[6];
	cin >> myarray[7];
	cin >> myarray[8];
	cin >> myarray[9];


	return 0;
}
In your assignment there is written "use one loop to read 10 values from the user and store them in the array". I do not see any loop in your code.
this is what i have now it outputs sum and average just need min and max
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
#include <iostream>
using namespace std;
int main()

//int _tmain(int argc, _TCHAR* argv[])
{
	int a, sum =0;

	int myarray [10];
	cout << " enter a number ";
	cin >> myarray[0];
	cin >> myarray[1];
	cin >> myarray[2];
	cin >> myarray[3];
	cin >> myarray[4];
	cin >> myarray[5];
	cin >> myarray[6];
	cin >> myarray[7];
	cin >> myarray[8];
	cin >> myarray[9];

	for (a=0; a<5; a++)
	{
		

		sum+=myarray[a];
	}

	cout << " The sum is:" << sum << endl;
	
	int average=0;
for(int a=0; a<5; a++) 
{
	average+=myarray[a];
}
average/=5;
	cout << " The average is:" << average << endl;
	return 0;
}

Your program should declare a 10 element double array


type double?

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
#include<iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){


double myArray[10];

int sum=0;

double average;

//prompt user to input 10 values
cout<<"Enter 10 numbers please: \n";

for(int counter=0;counter<10;counter++){ //loop 10 times 0-9
        cout<<"Enter number "<<counter+1<<": ";
        cin>>myArray[counter]; //store value in the array
}//end loop for
cout<<endl;

for(int counter=0;counter<10;counter++){//loop 10 times 0-9
        sum+=myArray[counter]; //compute the sum
}//end loop for

average=sum/10; //calculate the average

/*
Now you can write the third loop using the same logic
hint: think what should be the initial value of a smallest and largest
variable at the first loop
*/

cout<<"The sum is: "<<sum<<"\nThe average is: "<<average<<'\n'<<endl;

return 0; //indicates successful termination
}//end main 



Enter 10 numbers please: 
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 9
Enter number 10: 10

The sum is: 55
The average is: 5
Last edited on
whats wrong now? getting errors
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
int main()
{


double myArray[10];

double sum=0;

double average;


cout<<"Enter 10 numbers please: \n";

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

for(int counter=0;counter<10;counter++)
{
        sum+=myArray[counter];
}

average=sum/10; 
cout<<"The sum is: "<< sum <<"\nThe average is: "<<average<<'\n'<<endl;

int min, max,counter;
	min=myArray[counter];
max=myArray[counter];
for(int counter=1;counter<10;counter++)
	{
		if(min>myArray[counter])
		{
			min=myArray[counter];
		}
		else if(max<myArray[counter])
		{
			max = myArray[counter];
		}
	}

cout<<"Maximum number is: "<< max << endl;
cout<<"Minimum number is: "<< min << endl;


return 0; 
}
In lines 30 and 31, you are attempting to use counter without having initialized it. However, you don't really need a counter variable there based on how your following for-loop works. min = myArray[0] and max = myArray[0] will probably work fine.
output is not correct what did I do wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int min, max;
	min = myArray[0];
	max = myArray[0];
for(int counter=1;counter<10;counter++)
	{
		if(min>myArray[0])
		{
			min=myArray[0];
		}
		else if(max<myArray[0])
		{
			max = myArray[0];
		}
	}

cout<<"Maximum number is: "<< max << endl;
cout<<"Minimum number is: "<< min << endl;
Err... I meant to replace counter with 0 on lines 30 and 31 only. The counter variables in your for loop are fine. Sorry if I didn't make that clear.
Thanks got it!!!!
You should study the basics again, get a good C++ Book -size Bible-
check this code

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
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include<iomanip>
using std::setprecision;


int main(){


double myArray[10];



double average,
        sum=0,
        smallest,
        largest;

//prompt user to input 10 values
cout<<"Enter 10 numbers please: \n";

for(int counter=0;counter<10;counter++){ //loop 10 times 0-9
        cout<<"Enter number "<<counter+1<<": ";
        cin>>myArray[counter]; //store value in the array
}//end loop for
cout<<endl;

for(int counter=0;counter<10;counter++){//loop 10 times 0-9
        sum+=myArray[counter]; //compute the sum
}//end loop for

average=sum/10; //calculate the average

for(int counter=0;counter<10;counter++){
        if(counter==0){ //if the first loop is executing
                smallest=myArray[counter]; 
                largest=myArray[counter];
        }else{
                if(myArray[counter]<smallest)
                        smallest=myArray[counter];
                if(myArray[counter]>largest)
                        largest=myArray[counter];
        }//end if...else
}//end loop for


cout<<"The sum is: "<<sum<<"\nThe average is: "<<setprecision(3)<<average
        <<"\nThe smallest number is: "<<smallest<<"\nThe largest number is: "
        <<largest<<'\n'<<endl;

return 0; //indicates successful termination
}//end main 


Enter 10 numbers please: 
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 123
Enter number 10: 90

The sum is: 249
The average is: 24.9
The smallest number is: 1
The largest number is: 123
Topic archived. No new replies allowed.