C++| Computing Average

Sunday 6 January 2013

Average is basically the sum of all numbers within a list divided by the size of the list. In my world as a student, average means the overall total of my grades. Professors usually give grades after every end of the exams and as a student you really need to supervise your grades to prevent from failing. One thing to do that is to get your average grade.

Computing average is a little confusing especially if your calculator is not around. So, I made a program that will compute for your average to make computing more easier and it is user-friendly. The program is structured with the basic commands in C++ language. Check out the code below.



 1:    #include <iostream>
   2:        
   3:     #include <conio.h>
   4:        
   5:      using namespace std;
   6:       
   7:        
   8:       
   9:           int main()
  10:     
  11:          {
  12:       
  13:             float num1 = 0.0f;
  14:       
  15:               float num2 = 0.0f;
  16:      
  17:                float num3 = 0.0f;
  18:       
  19:                float num4 = 0.0f;
  20:       
  21:                float num5 = 0.0f;
  22:      
  23:              float ave = 0.0f;
  24:       
  25:               cout << "Enter a0: ";
  26:      
  27:                cin >> num1;
  28:      
  29:                cout << endl;
  30:      
  31:                cout << "Enter a1: ";
  32:       
  33:                 cin >> num2;
  34:       
  35:                cout << endl;
  36:     
  37:               cout << "Enter a2: ";
  38:     
  39:              cin >> num3;
  40:      
  41:                cout << endl;
  42:      
  43:              cout << "Enter a3: ";
  44:      
  45:                cin >> num4;
  46:      
  47:               cout << endl;
  48:      
  49:              cout << "Enter a4: ";
  50:       
  51:                cin >> num5;
  52:       
  53:               cout << endl;
  54:       
  55:               ave = (num1 + num2 + num3 + num4 + num5) / 5;
  56:      
  57:               cout << "The average of the five inputs a0...a4 = " << ave << endl;
  58:      
  59:       
  60:                 getch();
  61:       
  62:              }}


In this program, we use the float command to declare the variables. Float is used if you want to enter not just whole numbers but also numbers with decimal points, not like the int command which only allows whole numbers for inputs. As declared at the top part of the program, we have num1, num2, num3, num4 and num5 as a temporary storage for the inputs. We also have the ave, means average, in storing the total computation for average.

The program runs like this, first it will ask you to input five (5) numbers. After that ave will do the computing. As seen in the codes above, ave holds the equation in getting the average. After that, it will show you the result. Then the program terminates. You can check out the sample output below.




  

Hope this program helped you. Feel free to leave a comment below. Thanks!


No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...