468,771 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

How to write this simple program according to standards

13
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):

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> //needed for input output
  2.  
  3. using namespace std;
  4.  
  5. //---------------------------------------------------------------------------
  6.  
  7. int CalculateAge (int, int);  //function prototype
  8.  
  9. int main()
  10. {                         
  11.      int currentyear; //declaration variables
  12.      int birthyear; //declaration variables
  13.  
  14.      cout << "Type current year here: ";
  15.      cin >> currentyear;
  16.  
  17.      cout << "Type year of birth: ";
  18.      cin >> birthyear;
  19.  
  20.      cout << "This is your age now: ";
  21.      cout << CalculateAge (currentyear, birthyear) << endl;
  22.  
  23.      system("pause"); 
  24.  
  25.      return 0;
  26. }
  27.  
  28. int CalculateAge (int current_year, int birth_year) // function definition
  29. {
  30.      int age = (current_year - birth_year);
  31.      return age;
  32. }
  33.  
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int CalculateAverage (int, int, int, int, int); //protocol
  6. int Average (int , int);  // protocol
  7.  
  8. int main()
  9. {
  10. int integer1;
  11. int integer2;
  12. int integer3;
  13. int integer4;
  14. int integer5;
  15.  
  16. cout << "type the first: ";
  17. cin >> integer1;
  18. cout << "type the second: ";
  19. cin >> integer2;
  20. cout << "type the third: ";
  21. cin >> integer3;
  22. cout << "type the fourth: ";
  23. cin >> integer4;
  24. cout << "type the fifth: ";
  25. cin >> integer5;
  26.  
  27. system("pause");
  28.  
  29. cout << "Average is: " << Average (int, int) << endl;  // actual function
  30.  
  31. return 0;
  32. }
  33.  
  34.  
  35.  
  36. int CalculateAverage (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5) 
  37.  
  38. {
  39. int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5);
  40.  
  41. int average = (average / 5)
  42.  
  43. return average;
  44. }
  45.  
Thanks!
Jul 18 '07 #1
4 2052
weaknessforcats
9,207 Expert Mod 8TB
This code:
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;
}
defines int average twice. That won't compile.

You should have:
Expand|Select|Wrap|Line Numbers
  1. int CalculateAverage (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5) 
  2.  
  3. {
  4. int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5) / 5;
  5.  
  6. return average;
  7. }
  8.  
The hard-coded 5 is not cool but will work for you for now.
Jul 18 '07 #2
ilikepython
844 Expert 512MB
This code:


defines int average twice. That won't compile.

You should have:
Expand|Select|Wrap|Line Numbers
  1. int CalculateAverage (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5) 
  2.  
  3. {
  4. int average = (int integer_1 + int integer_2 + int integer_3 + int integer_4 + int integer_5) / 5;
  5.  
  6. return average;
  7. }
  8.  
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.
Jul 19 '07 #3
Or more compact:

Expand|Select|Wrap|Line Numbers
  1. int CalculateAverage (int integer_1, int integer_2, int integer_3, int integer_4, int integer_5)
  2. {
  3.      return ( (integer_1 + integer_2 + integer_3 + integer_4 + integer_5) / 5);
  4. }
  5.  
and maybe CalculateAverage would be better returning a float instead of int.


Ras.
Jul 19 '07 #4
ravenspoint
111 100+
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.
Jul 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

31 posts views Thread by da Vinci | last post: by
7 posts views Thread by Alan Silver | last post: by
26 posts views Thread by Martin Jørgensen | last post: by
14 posts views Thread by Jeroen | last post: by
23 posts views Thread by asit dhal | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.