473,698 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write this simple program according to standards

13 New Member
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 2384
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
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:
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 Recognized Expert Contributor
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
Rasputin
33 New Member
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 CalculateAverag e would be better returning a float instead of int.


Ras.
Jul 19 '07 #4
ravenspoint
111 New Member
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
14329
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? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
24
3243
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 activities in different languages by requesting small working programs as source. Well, having thought & read about the benefits of teamwork, oss, extreme programming etc i thought, instead of writing software that has a known purpose - why not do...
7
1421
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 problem. I have been a web designer for many years and have always made a specific point of writing HTML that is valid according to W3C specifications. With web forms, the .NET framework seems to generate some of the HTML, and what it generates is not...
26
2817
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 I get this behaviour: C:>program -help or C:>program -h printf("\nBla. bla. Here is some help and arguments\n").... etc.
14
2569
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 in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
12
2085
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 are accessed by the main program to provide the system functionality. Using the text editor, the user can: • Input the name of a file • The program will check if a file with this name exists or not. If yes, it will tell the user “This File...
23
2991
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
1447
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 After these have been entered by the user the current age of the user needs to be computed. I accomplished it because it is not all that difficult. However I want to do it according to standards and I would like to have the function prototype above...
5
1389
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 protocol before the main section and a function definition after the main section. Now I would like to build a program that takes 2 integers and switches them, integer A becomes integer B and the other way around. Simple. What I have now is this: ...
0
8672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9021
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.