473,408 Members | 1,840 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,408 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 2364
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.