# how unlimited the amount of numbers to average

• 05-21-2003
Machewy
how unlimited the amount of numbers to average
Hey Guys!!
I need some help with this.
I want to find a more efficient way to do this. I also want it to have a unlimited amount of numbers that one can average:
Code:

```#include <iostream> #include <stdlib.h> using namespace std; int main(int argc, char * argv[]) {         float grade1;         float grade2;         float grade3;         float grade4;         float grade5;         float grade6;         float grade7;         float grade8;         float grade9;         float grade10;                 int selection;                 cout<<" Welcome to Average Those Numbers!"<<endl             <<"---------------------------------"<<endl                 <<endl                 <<"Type in the amount of numbers you would like to average:"<<endl;         cin>>selection;                 if (selection == 1)         {                 cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 float average;                 average = grade1;                 cout<<"Your average is "<<average<<endl;                 return 0;     }         if (selection == 2)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 float average;                 average = (grade1 + grade2) / 2;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 3)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 float average;                 average = (grade1 + grade2 + grade3) / 3;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 4)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 float average;                 average = (grade1 + grade2 + grade3 + grade4) / 4;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 5)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 6)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 cin>>grade6;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6) / 6;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 7)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 cin>>grade6;                 cin>>grade7;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7) / 7;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 8)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 cin>>grade6;                 cin>>grade7;                 cin>>grade8;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8) / 8;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 9)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 cin>>grade6;                 cin>>grade7;                 cin>>grade8;                 cin>>grade9;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8 + grade9) / 9;                 cout<<"Your average is "<<average<<endl;                 return 0;         }         if (selection == 10)         {                        cout<<"Type in your numbers:"<<endl;                 cin>>grade1;                 cin>>grade2;                 cin>>grade3;                 cin>>grade4;                 cin>>grade5;                 cin>>grade6;                 cin>>grade7;                 cin>>grade8;                 cin>>grade9;                 cin>>grade10;                 float average;                 average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8 + grade9 + grade10) / 10;                 cout<<"Your average is "<<average<<endl;                 return 0;         }                else         {                 cout<<"Error! Please input only up to 10 grades!"<<endl;                 system("PAUSE");         }                                         return 0; }```
Thank-you so much!
best regards,
Matthew Johnson:)
• 05-21-2003
nickname_changed
Use arrays. Ie
Code:

```float Numbers[20] = {1, 74, ...etc}; int Num = 0; cout << "Type in the amount of numbers you want to average: "; cin >> Num; float Total; // Add them up for (int X = 0; X != Num, X++) {   Total = Total + Numbers[X]; } // Divide Total = Total / Num; cout << "The total is: " << Total;```
• 05-21-2003
PJYelton
If you want an unlimited amount, look into the STL containers, most notably vectors:
Code:

```vector<float> grades; float num=0, total=0, avg=0; cout<<"Enter grades (-1 to end):"<<endl; while (1) {     cout<<"---->";     cin>>num;     if (num==-1)       break;     grades.push_back(num);     total+=num; } avg=total/grades.size(); cout<<"The average test score is "<<avg<<endl;```
• 05-21-2003
Prelude
Or you could simply do away with saving the grades to begin with if all you need is the average. Then you would only be constrained by the size limitations of the summation type you use:
Code:

```#include <iostream> using namespace std; int main() {     float grade;     long  sum = 0;     long  n = 0;     cout<<"Now averaging grades, type Q to quit"<<endl;     while (cout<<"Enter grade "<< n <<": ", cin>> grade) {         sum += grade;         n++;     }     cout<<"The average is "<< ((n != 0) ? sum / n : 0) <<endl; }```