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):
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:
Thanks!
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; }
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; }
Comment