473,406 Members | 2,369 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,406 software developers and data experts.

Modularity Using functions

can some one please help me I need to write a program with 3 function, findaverage, howfarapart and whichisclosest. I got the first one (findaverage for three #) but im having problem with calling howfarapart cause it supposed to receive only two parameters. average and one of the three integer value.
example, 5.40 and 7 it returns the value 1.60; 5.40 and 5 it returns the value0.40
here is a copy of the program so far.



#include<iostream>
using namespace std;
//void introduction();
void findaverage(double,double,double);
// void howfarapart(double, double);
int main()
{
double numone,numsec,numthird,average,num;
cout<<"Misael Caballero, Project #3 \n"
<<"This is a program that computes the average of three given numbers \n\n";

cout<<"Type in three integer values from 1 to 100! \n";
cin>>numone>>numsec>>numthird;
cout<<"the three original integers are:"<<numone<<","<<numsec<<","<<numthird<<"\n"<< endl;

findaverage( numone, numsec, numthird); //function is call here


return 0;
}
void findaverage(double numone, double numsec, double numthird)

{
double average,num,howfarapart;

average=(numone+numsec+numthird)/3;
cout<<"the average is "<<average<<"\n\n";

//num=howfarapart(numone, numsec, numthird);

}
void howfarapart(double average, double num)
{
double first,second,third;
double numone,numsec,numthird;

first=average-num;
cout<<"the first is "<<first<<"\n";
second=average-num;
cout<<"the second is "<<second<<"\n";
third=average-num;
cout<<"the third is "<<third<<"\n\n";
return;
}
Oct 28 '06 #1
13 2490
teddarr
143 100+
I've done one similar to this. How is your data going to be input from the user?

I'll work something up to help you out. Later today!
Oct 29 '06 #2
I've done one similar to this. How is your data going to be input from the user?

I'll work something up to help you out. Later today!
First the program will take three integer values then the main program call a function named "findaverage"

cout<<"Type in three integer values from 1 to 100! \n";
cin>>numone>>numsec>>numthird;

now the function "findaverage" is called

void findaverage(double& numone, double& numsec, double& numthird)
{
double average;

average=(numone+numsec+numthird)/3;
cout<<"the average is "<<average<<"\n\n";

So now I need to call a function named "howfarapart", sending it two parameters the average which I got above and plus one of the three integer values.(the main program calls this function three times, once for each of the three integer values.) If the function determines how far apart the two parameters are.
e.g. if 5.40 and 7, it returns 1.60; if5.40 and 5, it returns 0.40. the answer returned by the function is always positive or zero.

Thanks for your help
Oct 29 '06 #3
teddarr
143 100+
OK, I have a program that will do the calculations on the 3 numbers, bit I don't have one that will return a value from "findaverage()" to "main()" and use it. If "findaverage()" is a void fuction it will not return a vaue to main() to be passed to the next function.

I'll modify my program and post it. Sorry it's taking so long. more in a few hours.
Oct 30 '06 #4
OK, I have a program that will do the calculations on the 3 numbers, bit I don't have one that will return a value from "findaverage()" to "main()" and use it. If "findaverage()" is a void fuction it will not return a vaue to main() to be passed to the next function.

I'll modify my program and post it. Sorry it's taking so long. more in a few hours.



Here is where im having problems, this function only takes two parameters
average and num, but now it has to be called three time that is because num has three different values.

void howfarapart(double& average, double& num)
{
double first,second,third;

double numsec,numthird;
first=average-numone;
cout<<"the first is "<<abs(first)<<" from the average"<<"\n";
second=average-numsec;
cout<<"the second is "<<abs(second)<<" from the average"<<"\n";
third=average-numthird;
cout<<"the third is "<<abs(third)<<" from the average"<<"\n";
}
Oct 30 '06 #5
teddarr
143 100+
Are you allowed to use loops yet? if statements, while statements and such?
Oct 30 '06 #6
yes im allow to use any loop, but remenber that the parameters in the function are average and num. The function "howfarapart" will be call three times so the average will be the same for all thre time thefunction is call,and num is going to change three times with three different integer values.
average num far apart
5.40 7 1.60
5.40 5 .40
Oct 30 '06 #7
teddarr
143 100+
Try This!!


#include <iostream>
#include <cmath> //for abs()
using namespace std;

//function prototypes
double findaverage(double numone, double numtwo, double numthree);
double howfarapart(double avg, double num);

/**************main()*****************/
void main()
{
cout<<"Intro Statement"<<endl;

double numone = 0;
double numtwo = 0;
double numthree = 0;
double avg = 0;
double num = 0;
double how = 0;

cout<<"Please enter a number: "<<endl;
cin>>numone;
cout<<"Please enter another number: "<<endl;
cin>>numtwo;
cout<<"Please enter a third number: "<<endl;
cin>>numthree;

avg = findaverage(numone, numtwo, numthree);
cout<<"the average of the 3 numbers entered is: "<<avg<<endl<<endl;

num = numone;
how = howfarapart(avg, num);
cout<<numone<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

num = numtwo;
how = howfarapart(avg, num);
cout<<numtwo<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

num = numthree;
how = howfarapart(avg, num);
cout<<numthree<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

}
/************findaverage()***************/
double findaverage(double numone, double numtwo, double numthree)
{
int sum = 0;
int av = 0;

sum = numone+numtwo+numthree;
av = sum/3;

return av;
}
/**************howfarapart()*********/
double howfarapart(double avg, double num)
{
double howfar = 0;

howfar = abs(avg-num);

return howfar;
}


I think the purpose of this exercise is to teach you to use vaule returning functions. Void functions will not do this. Notice the function prototypes, function definitions, and the return statements of each function.

Once you understand these concepts you are home free. This was the hardest part for me to grasp. Once I got it...it was like a lightbulb came on. Your on the right track. Just fill in the blank spots and make the jump from void functions to value returning functions and you've got it.

Thanks for the practice.
Oct 30 '06 #8
teddarr
143 100+
alternate find average()

/************findaverage()***************/
double findaverage(double numone, double numtwo, double numthree)
{
int av = 0;

av = (numone+numtwo+numthree)/3;

return av;
}


Gice whichisclosest() a try. I've got a structure in my head that might help if you get stuck.

Good Luck.
Oct 30 '06 #9
Thank you very much, you wer right I needed to stay away from the void and take the return value part.
for the "whichisclosest" function I will just use a if else statement.
one more thing if i need to print out at least 8 sets of data values at the end of the program I need a counter right? but where do I place it? and do I need to ask the user to enter y/n to end the program?


Try This!!


#include <iostream>
#include <cmath> //for abs()
using namespace std;

//function prototypes
double findaverage(double numone, double numtwo, double numthree);
double howfarapart(double avg, double num);

/**************main()*****************/
void main()
{
cout<<"Intro Statement"<<endl;

double numone = 0;
double numtwo = 0;
double numthree = 0;
double avg = 0;
double num = 0;
double how = 0;

cout<<"Please enter a number: "<<endl;
cin>>numone;
cout<<"Please enter another number: "<<endl;
cin>>numtwo;
cout<<"Please enter a third number: "<<endl;
cin>>numthree;

avg = findaverage(numone, numtwo, numthree);
cout<<"the average of the 3 numbers entered is: "<<avg<<endl<<endl;

num = numone;
how = howfarapart(avg, num);
cout<<numone<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

num = numtwo;
how = howfarapart(avg, num);
cout<<numtwo<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

num = numthree;
how = howfarapart(avg, num);
cout<<numthree<<" is "<<how<<" from "<<avg<<"."<<endl<<endl;

}
/************findaverage()***************/
double findaverage(double numone, double numtwo, double numthree)
{
int sum = 0;
int av = 0;

sum = numone+numtwo+numthree;
av = sum/3;

return av;
}
/**************howfarapart()*********/
double howfarapart(double avg, double num)
{
double howfar = 0;

howfar = abs(avg-num);

return howfar;
}


I think the purpose of this exercise is to teach you to use vaule returning functions. Void functions will not do this. Notice the function prototypes, function definitions, and the return statements of each function.

Once you understand these concepts you are home free. This was the hardest part for me to grasp. Once I got it...it was like a lightbulb came on. Your on the right track. Just fill in the blank spots and make the jump from void functions to value returning functions and you've got it.

Thanks for the practice.
Oct 30 '06 #10
teddarr
143 100+
I see 2 ways to look at this problem. If you don't know how many data sets you are going to use then you can place everything in main() after the variable declaration in a while loop "while (repeat =='y' || repeat== 'Y')" and repeat the program infinately. Key here is to declare repeat as a char type variable.

char repeat = y;

while (repeat =='y' || repeat== 'Y')
{
.
.
.
}

second, for only 8 data sets, place everything above in a while loop "while(count <= 8)" and put a "count++" statement just about anywhere inside the while loop. Don't forget to initialize count to 0.

int count = 0;

while (count <=8)
{
.
.
.
count++;
}

that should work or at least get you close.
Oct 30 '06 #11
teddarr
143 100+
Oops, I think you need to initialize count to 1. try it both ways and see which works.
Oct 30 '06 #12
thank you for opening my eyes to so many things I can see now where I was making the mistake. just one last thing this is my last code.
this function determines which one is the smallest number out of the three given, and prints a nessage saying that the smallest is closest to the average.

I try doing it with void and double. I dont need to print out a value just a message saying which one is closest to the average.

thanks

/*****whichisclosest******/
double whichisclosest( double& numone,double& numtwo, double& numthree)
{
double avg;
if(avg<=numone)
cout<<"the first one is closest to the average";
else if(avg<=numtwo)
cout<<"the second one is closest to the average";
else if(avg<=numthree)
cout<<"the third one is closest to the average";
return 0;

alternate find average()

/************findaverage()***************/
double findaverage(double numone, double numtwo, double numthree)
{
int av = 0;

av = (numone+numtwo+numthree)/3;

return av;
}


Gice whichisclosest() a try. I've got a structure in my head that might help if you get stuck.

Good Luck.
Oct 30 '06 #13
the three numbers are from the howfarapart result.


thank you for opening my eyes to so many things I can see now where I was making the mistake. just one last thing this is my last code.
this function determines which one is the smallest number out of the three given, and prints a nessage saying that the smallest is closest to the average.

I try doing it with void and double. I dont need to print out a value just a message saying which one is closest to the average.

thanks

/*****whichisclosest******/
double whichisclosest( double& numone,double& numtwo, double& numthree)
{
double avg;
if(avg<=numone)
cout<<"the first one is closest to the average";
else if(avg<=numtwo)
cout<<"the second one is closest to the average";
else if(avg<=numthree)
cout<<"the third one is closest to the average";
return 0;
Oct 30 '06 #14

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

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
4
by: Christian Christmann | last post by:
Hi, I'm reading "C++ Coding Standards" by Herb Sutter. On page 67 there's an example which I don't understand: --------------------------------- class Base{// ... virtual void Foo(int);...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
12
by: BDowling | last post by:
Hi all, I have an existing Visual C++ project that builds a DLL (not COM or ATL or anything). I wish to use this DLL in a Visual Studio 2005 C# project without having to define each function and...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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,...
0
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...

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.