Average

hi to all,

i want to write a program, which, when user inputs a number, array of same size is made, and user then initializes the elements of an array, and at the end, we average those inputs(numbers)

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

using namespace std;

int main()
{
	int n;
	
	cout << "Enter the number of values to average: ";
	cin >> n;

    int arr[n];

	for(int cnt = 0; cnt < n; ++cnt)
	{
	
    int m;

	arr[cnt] = m;
	
	cout << "[" << cnt << "] = ";	
	cin >> m;

	}

cout << "Average = " << endl;
}


i get these errors :
...cpp(12) : error C2057: expected constant expression
...cpp(12) : error C2466: cannot allocate an array of constant size 0
...cpp(12) : error C2133: 'arr' : unknown size


of course as you can see, i havent made average computation, because i dont know how to make "mathematical operations", i do between arrays, but not like this, so if anyone could help me out with that, or give me link to tutorial mabe, ill be glad :)


ty in advance for all the help
It's just what the error says. The size for an array created in stack needs to be constant (either literal or const) because it needs to be known at compile time. Note that gcc doesn't have this restriction, but the proper way to create a variable size array is T *array=new T[n] where T is the needed type and n the desired size.
You could also try vectors.
Line 19 will produce a runtime error because m is uninitialized at that point.
You are mixing tabs and spaces for indentation.
main doesn't return 0.
Remember that the average is sumation(i=0..n-1;vals[i])/n
Last edited on
To make a non constant array, use dynamic allocation (see Language tutorial at http://www.cplusplus.com/doc/tutorial/dynamic.html )

the resultin should be like this:
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
#include <iostream>

using namespace std;

void main()
{
     int n;
	
     cout << "Enter the number of values to average: ";
     cin >> n;

     int* arr = new int[n];
     float average=0;

     for(int cnt = 0; cnt < n; ++cnt)
     {
	
            cout << "[" << cnt << "] = ";	
            cin >> arr[cnt];
            average += arr[cnt];

     }

     average /= n;

     cout << "Average = " << average << endl << endl;

     system("Pause");
}
Last edited on
Or here is one that does not use dynamic allocation of memory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	int n;
	cout << "Enter the number of values to average: ";
	cin >> n;
	int each_entry;
	int average = 0;
	for (int count=0; count<n; count++)
	{
	    cout << "Enter values: ";
	    cin >> each_entry;
	    average = (average + each_entry);
	}
	average = (average/n);
	cout << "this is the values entered: " << n << endl;
	cout << "this is the average: " << average << endl;
	return 0;
}
Last edited on
ty all that really helped me
Last edited on
Topic archived. No new replies allowed.