I need help in average of numbers

How can I do the average of numbers through array?

#include<iostream>
using namespace std;
int main()
{
float a;
float b;
float sum=0;

cout<<"Enter The Number You Want- ";
cin>>a;

for(float i=0;i<a;i++){
cout<<"Enter The Numbers ";
cin>>b;
sum=sum+b;
}

cout<<"Here's The Sumation ";
cout<<sum/a;

}
Hi Siam928007

I think this is what you were going for:
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
#include<iostream>

using namespace std;



int main()
{
	double maxNumbers;
	double number;
	double sum = 0;
	double average;

	cout << "\n How many numbers do you want to input?\n _";
	cin >> maxNumbers;
	
	cout << "\n";

	for (int count = 0; count < maxNumbers; count ++)
	{
		cout << " Enter number " << count + 1 << ": ";
		cin >> number;
		
		sum += number;
	}
	
	cout << "\n\n The sum of all number is: " << sum;
	
	average = (sum / maxNumbers);
	
	cout << "\n The average of those numbers is: " << average;
	cout << "\n\n\n";
	
	
	system("PAUSE");
	
	return (0);
}


A couple of things to point out:
First please always use code tags (the <> icon on the right of the text box) it makes your code a lot easier for people to read.
In a for loop, if you're incrementing by one, why not put an int?
Try to name your variables with meaningful names, for your code it's fine, it's only a couple of lines, but imagine 20k lines of code with variables such as a, b, c, d... you don't know what each one does.
I doubt your program compiles, you're missing the return statement at the end of your main loop.

I hope this helps
Hi H00G0
Thanks for helping but I want to do it through array
Well it's not much more complicated really:
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
#include<iostream>

using namespace std;



int main()
{
	const int MAX_NUMBERS = 10;
	
	double numbers[MAX_NUMBERS] = {};
	double sum = 0;
	double average;

	cout << "\n You will be prompted to enter 10 numbers.\n\n";


	for (int count = 0; count < MAX_NUMBERS; count ++)
	{
		cout << " Enter number " << count + 1 << ": ";
		cin >> numbers[count];
		
		sum += numbers[count];
	}
	
	cout << "\n\n The sum of all number is: " << sum;
	
	average = (sum / MAX_NUMBERS);
	
	cout << "\n The average of those numbers is: " << average;
	cout << "\n\n\n";
	
	
	system("PAUSE");
	
	return (0);
}


If you want to chose the amount of number to input though, You'll have to use vectors. You can't initialise an array with a non const variable :)
the best you can do in standard c++ is a pointer.
cin >> size
int * ip = new int[size];
.. and from here forward treat ip as an array[size]
... etc
for(i = 0; i < size; i++)
sum += ip[i];
sum/= size;

...
delete[] ip; //when done with it, return the memory to the OS.


Which is more or less identical to what a vector does for you, except the vector does the memory management and maintains the size for you so you can't mess it up. That is what a vector IS at its most basic level: its a wrapper for arrays/pointer-arrays that gives you more functionality and protects you from mistakes.

Last edited on
I must be missing something. Why does this work if you can't initialize an array with a non-const variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;

int main()
{
    int maxnums=0;
    cout << "Enter the max numbers you want: ";
    cin >> maxnums;

	double numbers[maxnums] = {};
	double sum = 0;

	for (int count = 0; count < maxnums; count ++){
		cout << " Enter number " << count + 1 << ": ";
		cin >> numbers[count];
		sum += numbers[count];
	}
	
	cout << "\n\nThe sum of all numbers is:\t" << sum << endl;
	
	cout << "Average of those numbers is:\t" << (sum / maxnums) << endl;
	
return 0;
}
Last edited on
> Why does this work if you can't initialize an array with a non-const variable

It only works on non-conforming implementations of C++.
(The default for some implementations is not to conform to the C++ standard in this regard.)

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int maxnums=0;
    std::cin >> maxnums;

    [[maybe_unused]] double numbers[maxnums] ;
    std::cout << "compiled and ran, though this is not standard C++\n\n" ;
}

echo && echo 'GNU default' && g++ -std=c++17 -O3 -Wall -Wextra -pthread -march=native main.cpp && ./a.out
echo && echo 'LLVM default' && clang++ -std=c++17 -stdlib=libstdc++ -O3 -Wall -Wextra -pthread -march=native main.cpp && ./a.out

echo && echo 'GNU force C++ conformance' && g++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors -pthread -march=native main.cpp && ./a.out
echo && echo 'LLVM force C++ conformance' && clang++ -std=c++17 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread -march=native main.cpp && ./a.out

GNU default
compiled and ran, though this is not standard C++


LLVM default
compiled and ran, though this is not standard C++


GNU force C++ conformance
main.cpp: In function 'int main()':
main.cpp:8:41: error: ISO C++ forbids variable length array 'numbers' [-Wvla]
[[maybe_unused]] double numbers[maxnums] ;
^

LLVM force C++ conformance
main.cpp:8:33: error: variable length arrays are a C99 feature [-Werror,-Wvla-extension]
[[maybe_unused]] double numbers[maxnums] ;
^
1 error generated.

http://coliru.stacked-crooked.com/a/5e7c1a92bcf64f6c
Ah, I see. That's interesting; I didn't realize different implementations would treat it so differently. Thanks very much for the explanation!
Cheddar which compiler are you using?
I did it on cpp.sh and compiled it with c++14. No warnings or anything, even with everything turned on.

I was really just curious, after reading hoogo's comment, so I modified his code, above, to see if it would work, and then wondered why it did work after seeing the comment about having to use a const.

Anyway. JLBorges explanation makes sense to me. Good to know for future reference.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   int n;
   cout << "Enter the number of data points ( > 0 ): ";   cin >> n;
   valarray<double> V( n );
   cout << "Enter " << n << " values: ";
   for ( int i = 0; i < n ; i++ ) cin >> V[i];
   cout << "Mean: " << V.sum() / n;
}

Topic archived. No new replies allowed.