使用函数找到最少三个值[保持]

时间:2022-09-21 22:50:10
#include <stdio.h>
double minimum(double x, double y, double z) {
    double temp = 0;

    //Logic here

}
int main(void) {
    double x, y, z, minVal;

    printf("Please enter three numeric values: ");
    scanf("%lf%lf%lf", &x, &y, &z);
    minVal = minimum(x, y, z);
    printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);

    return 0;
}

Logic of the code should go within comment in first function. Function should then result in minVal and printed too console

代码的逻辑应该在第一个函数中进行注释。然后函数应该导致minVal并打印太多控制台

3 个解决方案

#1


3  

 #include <stdio.h>
 #include <math.h>

double minimum(double x, double y, double z)
{
  double temp = 0;

  if (isnan(x) || isnan (y) || isnan(z))
    return NAN;

  temp = (x < y) ? x : y;
  return (temp < z)? temp : z;
}

int main(void) {
    double x, y, z, minVal;

    printf("Please enter three numeric values: ");
    scanf("%lf%lf%lf", &x, &y, &z);
    minVal = minimum(x, y, z);
    printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);

    return 0;
}

#2


1  

method for double:

双重方法:

int main(void)
{
     double a, b, c, temp, min;

     printf ("Enter three nos. separated by spaces: ");
     scanf ("%lf%lf%lf", &a, &b, &c);

     temp = (a < b)    ? a : b;
     min =  (c < temp) ? c : temp;

     printf ("The Minimum of the three is: %lf", min);

     /* indicate success */
     return 0;
 }

method for int:

int的方法:

int main(void)
{
    int a, b, c, temp, min;

    printf ("Enter three nos. separated by spaces: ");
    scanf ("%d%d%d", &a, &b, &c);

    temp = (a < b)    ? a : b;
    min =  (c < temp) ? c : temp;

    printf ("The Minimum of the three is: %d", min);

    /* indicate success */
    return 0;
}

#3


0  

First of all you should have return type double for the minValue function

首先,你应该为minValue函数返回double类型

Then your logic should be Consider 3 numbers a, b, c and another double temp Then...

然后你的逻辑应该是考虑3个数字a,b,c和另一个双重温度然后......

If (a < b)

Temp = a

Else

Temp = b

If(temp < c)

Return temp

Else 

Return c

#1


3  

 #include <stdio.h>
 #include <math.h>

double minimum(double x, double y, double z)
{
  double temp = 0;

  if (isnan(x) || isnan (y) || isnan(z))
    return NAN;

  temp = (x < y) ? x : y;
  return (temp < z)? temp : z;
}

int main(void) {
    double x, y, z, minVal;

    printf("Please enter three numeric values: ");
    scanf("%lf%lf%lf", &x, &y, &z);
    minVal = minimum(x, y, z);
    printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);

    return 0;
}

#2


1  

method for double:

双重方法:

int main(void)
{
     double a, b, c, temp, min;

     printf ("Enter three nos. separated by spaces: ");
     scanf ("%lf%lf%lf", &a, &b, &c);

     temp = (a < b)    ? a : b;
     min =  (c < temp) ? c : temp;

     printf ("The Minimum of the three is: %lf", min);

     /* indicate success */
     return 0;
 }

method for int:

int的方法:

int main(void)
{
    int a, b, c, temp, min;

    printf ("Enter three nos. separated by spaces: ");
    scanf ("%d%d%d", &a, &b, &c);

    temp = (a < b)    ? a : b;
    min =  (c < temp) ? c : temp;

    printf ("The Minimum of the three is: %d", min);

    /* indicate success */
    return 0;
}

#3


0  

First of all you should have return type double for the minValue function

首先,你应该为minValue函数返回double类型

Then your logic should be Consider 3 numbers a, b, c and another double temp Then...

然后你的逻辑应该是考虑3个数字a,b,c和另一个双重温度然后......

If (a < b)

Temp = a

Else

Temp = b

If(temp < c)

Return temp

Else 

Return c