【问题标题】:Using C to Find Min and Max value using Function使用 C 使用函数查找最小值和最大值
【发布时间】:2021-01-24 06:34:04
【问题描述】:

我需要编写一个程序,用户可以输入他们定义的任意多的数字,然后程序会尝试找出哪个是最小值和最大值。我面临的问题是:

  1. 当程序执行时,第二行将在printf之前等待用户输入(数字)
  2. “系统”错误似乎不可靠,有时有效,有时无效
  3. 程序只检查最后一个数字条目,因此它只显示最小和最大的最后一个数字

您可以在答案中给出提示或更正。非常感谢。

#include <stdio.h>
float max(float num1){
    float a=0, b;
        if(num1 > a){
            a=num1;
        }
    return a;
}

float min(float num2){
    float x=100, y;

        if(num2 < x ){
            x=num2;
        }

    return num2;
}

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
    
    }
    maxi= max(data_Input);
    mini= min(data_Input);

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

输出:

How many number would you like to type in? : 5
70
Type in the number : 
Type in the number : 90.7
Type in the number : 99
Type in the number : 30
Type in the number : 50
The Lowest Number is 50.00
The Highest Number is 50.00

【问题讨论】:

  • “错误“系统”似乎不可靠”这句话是什么意思?
  • 欢迎来到 SO。您似乎使用ax 作为当前最小值/最大值的存储,并且您似乎希望可以在函数调用之间使用它。您可能会再次阅读有关函数中变量的生命周期的信息。还有关于static 关键字。
  • 如果你想检查每一个输入,你应该用每一个输入调用你的函数。 min 也应该返回 `x´。
  • 另外,阅读 scanf 如何处理换行符。
  • 对于用户交互,考虑抑制 scanf() 调用中的“\n”,否则它会挂起(参见stackoverflow.com/questions/40948635/…)。此外,因为 printf() 是一个缓冲函数。如果你在没有终止“\n”的情况下调用它,有时你可能会遇到没有显示的情况。为避免这种情况,请在每个 printf() 之后立即调用 fflush(stdout),而不使用 "\n"

标签: c max min


【解决方案1】:

好的,问题是您没有在输入每个连续数字后更新 data_input。您所做的是,将最后一个数字与 0 或 100 进行比较,这在逻辑上是不正确的。 您如何将第一个数字作为输入,然后在每次连续输入后,将其与最小值和最大值进行比较。这是示例代码。

#include <stdio.h>
float max(float num1, float num2){
    if(num1 > num2){
        return num1;
    }
    return num2;
}

float min(float num1, float num2){
    if(num1 < num2){
        return num1;
    }
    return num2;
}

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);
    
    // the first number will be minimum and maximum
    mini = data_Input;
    maxi = data_Input;

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        // make it a composite if condition
        while(data_Input<0 || data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(maxi, data_Input);
        mini= min(mini, data_Input);
    
    }
    

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

【讨论】:

  • 啊,我回想起了代码。我唯一不明白的是浮动功能。既然已经返回 num1,为什么还要在 float max() 处返回 num2?另外,在两个函数之间使用相同的变量是否合适(在 float max 和 float min 处)?
  • 回答函数的后半部分,两个函数之间使用相同的变量是绝对可以的,因为我们传递的变量的值并没有改变。
  • 并且,对于问题的前一部分。函数 max 应该返回两个数字中的最大值。我们首先检查 number1 是否更大,即最大值,足够公平地返回它。而且,如果该块没有被执行,则意味着 number2 更大,所以我们只需返回它。所以,现在函数 max() 返回我们两个变量的最大值,我们将它存储在我们的 maxi 变量中,例如 maxi = max(maxi, x)。现在,maxi 没有被更改为函数 max,所以这样使用它没有问题。希望对您有所帮助。
【解决方案2】:

程序会检查最后一个数字,因为您在 for 手镯中调用了 min 和 max 函数,因此您可以在 for 手镯中调用它们,例如:

for(interval=2; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(data_Input);
        mini= min(data_Input);
    }

而不是重写相同的代码,您可以只要求 for 循环中的数字并将您的间隔初始化为 1,这样您的 main 将如下所示:

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : ");
    scanf("%d\n",&times);

    for(interval=1; interval<=times; interval++){
        printf("\nType in the number: ");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:");
            scanf("%f",&data_Input);
        }
        maxi= max(data_Input);
        mini= min(data_Input);
    }

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

【讨论】:

  • 是的,你是对的! @MuhammadJunaidHaris 您可以在上面的答案中看到正确的 max 和 min 函数@Exc4pe
  • 是的,我既没有完全掌握函数背后的逻辑,也没有完全掌握存储和比较变量值的逻辑。谢谢
【解决方案3】:

解决 printf 问题 很容易。由于stdout 是行缓冲的(至少默认情况下 - 它可以更改),只要在缓冲区中插入换行符,它就会被刷新。因此,只需在每条消息打印后添加一个换行符,例如

printf("How many number would you like to type in ? : \n");

你会没事的。


说到minmax的错误计算,你的尝试基本上有两个大问题:

  • 您没有获得times 输入,但只有一个。事实上,每次调用 scanf 时,您都会覆盖相同的变量 data_Input,而不会与之前的输入进行任何比较
  • 在最后一次输入之后,您只调用一次min()max() 函数。此外,您尝试将参数与具有本地存储且生命周期仅限于函数本身的局部变量进行比较,以便在下一次调用时再次初始化它

为了在函数内有一个变量,它只在第一次初始化时才被初始化,你可以使用static关键字。但是不是我建议你解决这个问题。

在我看来,您不需要比较函数:您可以在每次获得新输入时更新 maximini(同时解决上述两个问题):

int main(){
    int times, interval;
    float mini, maxi, data_Input;

    printf("How many number would you like to type in ? : \n");
    scanf("%d",&times);

    printf("Type in the number: ");
    scanf("%f", &data_Input);

    maxi = data_Input;
    mini = data_Input;

    for(interval=2; interval<=times; interval++){
        printf("\nType in the number: \n");
        scanf("%f",&data_Input);
        while(data_Input<0){
            printf("Invalid Input! Please re-enter the number:\n");
            scanf("%f",&data_Input);
        }
        while(data_Input>100){
            printf("Invalid Input! Please re-enter the number:\n");
            scanf("%f",&data_Input);
        }
        
        /* Check input and, in case, update max and min */
        if(data_Input > maxi)
             maxi = data_Input;

        if(data_Input < mini)
             mini = data_Input;
    }

    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

比较是在循环内部进行的,因此您不需要将输入存储到数组中。

【讨论】:

  • 是的,我确实错过了分数。我个人也认为在使用函数时更难管理代码。既然是作业,我得试试我的创意,不管怎样,谢谢你的解释
【解决方案4】:

你的代码有很多问题。

  1. 函数min & max 没有任何意义
  2. 你没有检查scanf的结果,不知道是否成功
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main()
{
    int times, interval;
    float mini, maxi, data_Input;

    do 
    {
        printf("\nHow many number would you like to type in ? : ");
    }while(scanf(" %d\n",&times) != 1);

    for(interval = 0; interval < times; interval++)
    {
        do 
        {
            printf("\nType in the number: ");
        }while(scanf(" %f",&data_Input) != 1 && (data_Input < 0 || data_Input > 100));

        printf("%f\n", data_Input);

        maxi = interval == 0 ? data_Input : MAX(maxi, data_Input);
        mini = interval == 0 ? data_Input : MIN(mini, data_Input);
    
    }
    printf("The Lowest Number is %.2f\n", mini);
    printf("The Highest Number is %.2f\n", maxi);
    return 0;
}

【讨论】:

  • define 行是否也选择了其他功能? (浮动最大值和最小值)
【解决方案5】:

程序执行时,第二行会在 printf 之前等待用户输入(数字)

"%d\n" 中删除"\n"。它会阻塞,直到在数字之后检测到非空白并且不需要。

printf("How many number would you like to type in ? : ");
// scanf("%d\n",&times);
scanf("%d",&times);
printf("Type in the number: ");

如果仍然没有按预期看到输出,请将其刷新。通常打印'\n' 会刷新stdout,但不一定。

printf("How many number would you like to type in ? : ");
fflush(stdout);  // add

错误“系统”似乎不可靠,有时有效,有时无效

至少这个问题:顺序检查与检查两个条件一起输入有效性。

而不是

    while(data_Input<0){
      ...
    }
    while(data_Input>100){
      ...
    }

一起测试。

    while(data_Input<0 || data_Input>100){
      ...
    }

程序只检查最后一个数字条目,因此它只显示 min 和 max 中的最后一个数字

True 作为代码只比较一次对max() 的调用。该函数仅将数字与 0 进行比较,而不是先前的值。 min() 也是如此。

考虑改变算法

// Pseudo code for max
prompt How many number would you like to type in ? : "
get times

max_value = -INF // set to minimum possible float

for each value 1 to times
  prompt "Type in the number: "
  get data_Input
  if  data_Input > max_value
    max_value = data_Input

print max_value

【讨论】:

  • 看了你的回答后,我似乎有功课要做,并且获得了一些知识。非常感谢
猜你喜欢
  • 2016-08-12
  • 1970-01-01
  • 2013-09-28
  • 2017-09-20
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 2020-03-20
相关资源
最近更新 更多