473,226 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 2355
weaknessforcats
9,208 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

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

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
24
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
7
by: Alan Silver | last post by:
Hello, I am a complete and utter newbie at ASP.NET, so please forgive any stupid questions ;-) I am just trying to get my head around the whole web forms business, but have run into a...
26
by: Martin Jørgensen | last post by:
Hi, I'm learning C-programming. I have a program which I would like to modify so it takes arguments from the commandline. Let call the program: program.exe. Could somebody shortly explain how...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
12
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that...
23
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
11
by: Break2 | last post by:
I am trying to write to a program that will compute the current age of the one executing the program. It is done by asking 2 questions: - What is the current year - What is your birth year ...
5
by: Break2 | last post by:
I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard. In my programs I want to have a function...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.