功能和分离程序 [英] Functions and separating a program

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

问题描述

基本上,我这周的作业是:

写出三个函数:
一个当提供两个整数时,返回下一个。
另一个返回两个整数的较高者。
第三个返回提供的两个整数的平均值
为了测试你的函数,编写一个提示两个整数的程序,然后
打印出较低的值,然后再打印出较高的值,然后再平均值。
您的程序应该包含函数原型。



无论如何,正如我之前提到的,尽管这是我最喜欢的模块。

所以这里是我的代码到目前为止(令人尴尬)。我无法弄清楚,如果我的逻辑应该高于或低于整数的主线。整体上真的很混乱。我一直在努力研究这个模块(远远超过所有其他模块),但仍然觉得它是一个迷失的孩子。

  #include< stdio.h> 
#include< stdlib.h>

int getLow(int num1 int num2);

int getHigh(int num1 int num2);

int getAvg(int num1 int num2);

int main()
{
int lores,hires,avres;


printf(输入两个整数:\\\
);
scanf(%i%i,& num1,& num2);
$ b int getLow(int num1,int num2){

if(num1 printf(%i是最低的\ n ,num1);}
if(num1> num2){
printf(%i is the lowest \\\
,num2);}}

int getLow( int num1,int num2){

if(num1> num2){
printf(%i是最高的\ n,num1);}
if( num1 printf(%i是最高的\ n,num2);}}

int getAvg(int num1,int num2)

getAvg =(num1 + num2)/ 2;

printf(%i是两个整数的平均数\\\
,getAvg);

返回0;
}


解决方案

  #include< stdio.h> 
#include< stdlib.h>

int getLow(int num1,int num2);
int getHigh(int num1,int num2);
float getAvg(int num1,int num2);

int main()
{
int num1,num2,avres;

printf(输入两个整数:\\\
);
scanf(%i%i,& num1,& num2);

float avg = getAvg(num1,num2);
printf(最小的数字是%d,getLow(num1,num2));
printf(最大的数字是%d,getHigh(num1,num2));
printf(%f是两个整数的平均数\ n,avg);

返回0;


$ b float getAvg(int num1,int num2)
{
return(num1 + num2)/ 2 .;

$ b $ int getLow(int num1,int num2)
{
if(num1 {
return num1;
}
else
{
return num2;
}
}

int getHigh(int num1,int num2)
{
if(num1> num2)
{
返回num1;
}
else
{
return num2;
}

}


Basically my homework this week is:

Write three functions: One which when supplied with two integers, returns the lower. Another which returns the higher of two supplied integers. And a third which returns the average of two supplied integers To test your functions, write a program which prompts for two integers and then prints out the lower, then the higher and then the average. Your program should include the function prototypes.

Anyway, as I mentioned before, I seem to have real difficulty with this module - even though it is my favourite.

So here is my code so far (embarrassing). I cant work out if my logic should be above or below the int main line. Just really confused overall. I have been trying so hard with this module (far more than all of the others) but still feel like a lost child with it.

#include <stdio.h>
#include <stdlib.h>

int getLow(int num1 int num2);

int getHigh(int num1 int num2);

int getAvg(int num1 int num2);

int main()
{
    int lores, hires, avres;


    printf("Enter two integers:\n");
    scanf("%i %i", &num1, &num2);

    int getLow(int num1, int num2){

    if (num1 < num2){
        printf("%i is the lowest\n", num1);}
      if (num1 > num2){
        printf("%i is the lowest\n", num2);}}

   int getLow(int num1, int num2){

    if (num1 > num2){
        printf("%i is the highest\n", num1);}
      if (num1 < num2){
        printf("%i is the highest\n", num2);}}

    int getAvg(int num1, int num2)

    getAvg = (num1 + num2) / 2;

     printf("%i is the average of the two integers\n", getAvg);

    return 0;
}

解决方案

#include <stdio.h>
#include <stdlib.h>

int getLow(int num1, int num2);
int getHigh(int num1, int num2);
float getAvg(int num1, int num2);

int main()
{
    int num1, num2, avres;

    printf("Enter two integers:\n");
    scanf("%i %i", &num1, &num2);

    float avg = getAvg(num1, num2);
    printf("The smallest number is %d", getLow(num1, num2));
    printf("The largest number is %d", getHigh(num1, num2));
    printf("%f is the average of the two integers\n", avg);

    return 0;

}

float getAvg(int num1, int num2)
{
    return (num1 + num2) / 2.;
}

int getLow(int num1, int num2)
{
    if (num1 < num2)
    {
        return num1;
    }
    else
    {
        return num2;
    }
}

int getHigh(int num1, int num2)
{
    if (num1 > num2)
    {
        return num1;
    }
    else
    {
        return num2;
    }

}

这篇关于功能和分离程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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