模块化使用功能 [英] Modularity Using functions

查看:84
本文介绍了模块化使用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我,我需要编写一个带有3个函数的程序,findaverage,howfarapart和whichisclosest。我得到了第一个(找到三个#的平均值),但我有问题调用howfarapart因为它应该只接收两个参数。平均值和三个整数值之一。

例如,5.40和7它返回值1.60; 5.40和5它返回值0.40

这里是程序的副本到目前为止。



#include< iostream>

使用命名空间std;

// void introduction();

void findaverage(double,double,double);

// void howfarapart(double,double);

int main()

{

double numone,numsec,numthird ,平均值,数量;

cout<<"" Misael Caballero,项目#3 \ n"

<<"这是一个计算三个给定数字的平均值\ n \ n" ;;


cout<<"输入从1到100的三个整数值! \ n" ;;

cin>> numone>> numsec>> numthird;

cout<<"三个原始整数是:<< ;< numone<<","<< numsec<<","<< numthird<<" \\\
"<< endl;


findaverage(numone,numsec,numthird); //函数在这里调用


返回0;

}

void findaverage(double numone,double numsec ,双倍数量)


{

平均双倍数,数量,方式;


平均值=(numone + numsec + numthird)/ 3;

cout<<"""""<<<<<" \ n \ n";

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


}

void howfarapart(double average,double num)

{

先加倍,第二,第三;

双倍数,numsec,numthird;


first = average-num;

cout<<"""""<<<<<<<<<<<" \ n";

second = average -num;

cout<<"第二个是<<<<<<" \ n" ;;
;

cout<<"""""<<<<<<" \ n \ n" ;;

return;

}

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;
}

推荐答案

我做过类似的事。你的数据将如何从用户输入?


我会努力帮助你。今天晚些时候!
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!



我做了一个类似的事情。你的数据将如何从用户输入?


我会努力帮助你。今天晚些时候!
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!



首先程序将取三个整数值,然后主程序调用一个名为findaverage的函数


cin>> numone>> numsec>> numthird;


现在函数findaverage被称为


void findaverage(double& numone,double& numsec,double& numthird)

{

double average;


average =(numone + numsec + numthird)/ 3;

cout<<"平均值是<<< average<< " \\\
\\\
英寸;


所以现在我需要调用一个名为howfarapart的函数,向它发送两个参数,我得到的平均值加上三个整数值中的一个。(主程序调用)这个函数三次,对于三个整数值中的每一个都有一次。)如果函数确定这两个参数相隔多远。

例如如果是5.40和7,则返回1.60; if5.40和5,它返回0.40。函数返回的答案总是正数或零。


感谢您的帮助

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


好的,我有一个程序可以进行计算在3个数字上,我没有一个会从findaverage()中返回一个值。 tomain()并使用它。如果findaverage()是一个无效的函数它不会返回一个vaue到main()传递给下一个函数。


我会修改我的程序并发布它。对不起,这花了这么久。在几个小时内完成。
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.


这篇关于模块化使用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆