how unlimited the amount of numbers to average

This is a discussion on how unlimited the amount of numbers to average within the C++ Programming forums, part of the General Programming Boards category; Hey Guys!! I need some help with this. I want to find a more efficient way to do this. I ...

  1. #1
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42

    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
    "All things come to an end"

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    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;

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my Average Array program?
    By special1zed in forum C Programming
    Replies: 12
    Last Post: 03-16-2009, 12:01 AM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. calcuations
    By redmondtab in forum C Programming
    Replies: 39
    Last Post: 09-15-2006, 09:09 PM
  5. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 06:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21