Average of n numbers

cin>> n numbers (but there is not exactly n it depends on user how many number he/she will include) n>=2&&n<=1000
how can i find average of them..
To find the sum of n numbers, add all the numbers and divide the sum by n
Last edited on
how ?
i want to write
for (i=2:i<=n;i++)
cin>>a[i]
then sum/n however we cant include n and i cannot do
I know logic first-I have to include numbers then i hve to count maybe with k++ then I have to divide sum of numbers to k--but i cant write it in program esp..first stage how i can include numbers without knowing quantity of them
Last edited on
...hope that this code can help you :)))

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;
#define maxLenght 10000
int main()
{

	int lenght;

	cout<<"Enter the lenegt of the array: ";
	cin>>lenght;

	double myArray[maxLenght];

	
	for(int i = 0; i < lenght; i++)
	{
		cout<<"Enter value with index: "<<i<<endl;
		cin>>myArray[i];
	}

	cout<<"\n";

	double avg;
	double sum = 0;

	for(int i = 0; i < lenght; i++)
	{
		sum += myArray[i];

		avg = sum / lenght;
	}

	cout<<"Avg is: "<<avg<<endl;
	


cin.get(); cin.get();
return 0;
}
how i can include numbers without knowing quantity of them
You need a way to know when the user ended the input. Use that as the condition of the loop.
Topic archived. No new replies allowed.