How to write this simple program according to standards

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Break2
    New Member
    • Jul 2007
    • 13

    How to write this simple program according to standards

    I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple.
    However I want to do it according to a standard I used before to write a program which asked a user to enter the current year and his birthyear after which the current age of the user was computed and written to the screen.

    I got some help with the second program from some of you, thanks for that and have now been working a substantial part of the evening on the other program.

    The idea is that I build the program like this (this should be the standard):

    function protocol.
    main section with actual function in it.
    function definition.

    This is the program which computes the current age (this program works fine and is according to the standard I would like to use):

    Code:
    #include <iostream> //needed for input output
    
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    int CalculateAge (int, int);  //function prototype
    
    int main()
    {                         
    	 int currentyear; //declaration variables
    	 int birthyear; //declaration variables
    
    	 cout << "Type current year here: ";
    	 cin >> currentyear;
    
    	 cout << "Type year of birth: ";
    	 cin >> birthyear;
    
    	 cout << "This is your age now: ";
    	 cout << CalculateAge (currentyear, birthyear) << endl;
    
    	 system("pause"); 
    
    	 return 0;
    }
    
    int CalculateAge (int current_year, int birth_year) // function definition
    {
    	 int age = (current_year - birth_year);
    	 return age;
    }
    For the above program it was not that complicated to build a function protocol and a function defintion. I am however running into more problems with the program that computes the average because in this program I have to do two things; first I have to add up the number of integers, after that I have to divide them. How do I put that into one function protocol, same goes for the definition. Or do I have to write two seperate function protocols / definitions?

    This is the program which computes the average:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int CalculateAverage (int, int, int, int, int); //protocol
    int Average (int , int);  // protocol
    
    int main()
    {
    int integer1;
    int integer2;
    int integer3;
    int integer4;
    int integer5;
    
    cout << "type the first: ";
    cin >> integer1;
    cout << "type the second: ";
    cin >> integer2;
    cout << "type the third: ";
    cin >> integer3;
    cout << "type the fourth: ";
    cin >> integer4;
    cout << "type the fifth: ";
    cin >> integer5;
    
    system("pause");
    
    cout << "Average is: " << Average (int, int) << endl;  // actual function
    
    return 0;
    }
    
    
    
    int CalculateAverage (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5) 
    
    {
    int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5);
    
    int average = (average / 5)
    
    return average;
    }
    Thanks!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Originally posted by Break2
    int CalculateAverag e (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5)

    {
    int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5);

    int average = (average / 5)

    return average;
    }
    defines int average twice. That won't compile.

    You should have:
    [code=c]
    int CalculateAverag e (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5)

    {
    int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5) / 5;

    return average;
    }
    [/code]

    The hard-coded 5 is not cool but will work for you for now.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by weaknessforcats
      This code:


      defines int average twice. That won't compile.

      You should have:
      [code=c]
      int CalculateAverag e (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5)

      {
      int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5) / 5;

      return average;
      }
      [/code]

      The hard-coded 5 is not cool but will work for you for now.
      Also, there shouldn't be any "int" s in the declareation of average.

      Comment

      • Rasputin
        New Member
        • Jun 2007
        • 33

        #4
        Or more compact:

        [code=c]
        int CalculateAverag e (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5)
        {
        return ( (integer_1 + integer_2 + integer_3 + integer_4 + integer_5) / 5);
        }
        [/code]

        and maybe CalculateAverag e would be better returning a float instead of int.


        Ras.
        Last edited by Rasputin; Jul 19 '07, 06:48 AM. Reason: parentheses

        Comment

        • ravenspoint
          New Member
          • Jul 2007
          • 111

          #5
          A more elegant solution, IMHO, is to calculate a running average. This way, you can avoid endless parameter lists and hard-coding the number of inputs.

          The function prototype might look like this:

          // param[in] number the new number we want to add
          // param[in,out] count number of values that have been considered, start with 0
          // param[in,out] total the sum of all numbers input
          // return

          float RunAverage( int number, int& count, int& total )

          the code would look like this

          total += number;
          count++;
          return (float) total / count;

          The trick here is that you are passing the parameters count and total by reference, not by value. The function manipulates them, but the mainline is responsible for keeping them safe.

          Alternatively, you could use static variables for these, private inside the function. The trouble is, you then need to some way to tell your function when to re-initialise, which requires either a second parameter or a "special" value for number.

          Comment

          Working...