#include<iostream>
usingnamespace 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.
#include<iostream>
usingnamespace std;
int main()
{
constint 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.
> 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 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.
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.
#include <iostream>
#include <valarray>
usingnamespace 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;
}