2010-08-24 183 views
0

我写的代码,但我不获得最大的价值如何找到最大值寻找最小和最大

#include <stdio.h> 
int main(void) 
{ 
     double temp[0]; 
     float max,min; 
     float i; 
     short c,j,k; 
     float sum=0; 
     float nu[c]; 

     printf("Number of values :"); 
     scanf("%f",&i); 

     for (c=1;i>=c;c++) 
     { 
      printf("values="); 
      scanf("%f",&nu[c]); 

      nu[c]=temp[0]; 

      for (j = 0; i>=j; j++) 
      { 
      if (temp[0]> nu[c]) 
      { 
      //  nu[c]=temp[0]; 
        max = temp[0]; 

      } 
     } 
     sum = sum + nu[c]; 
     } 
     printf("sum = %f \n",sum); 
     printf("maximum value = %f\n",max); 
} 
+0

可能重复的[找到sum,min,max与数组](http://stackoverflow.com/questions/3547633/finding-sum-min-max-with-array)(来自同一作者)。 – paxdiablo 2010-08-24 11:16:47

回答

2

如果用-Wall运行你的编译器(如你应该总是做),你应该已经看到了问题:

gcc -Wall -o foo foo.c 
foo.c: In function 'main': 
foo.c:35:14: warning: unused variable 'k' 
foo.c:33:14: warning: unused variable 'min' 
foo.c:62:1: warning: control reaches end of non-void function 
foo.c:37:4: warning: 'c' is used uninitialized in this function 

这里是一个程序的源代码,工程:

#include <stdio.h> 

int main(void) 
{ 
    float max = 0; // hypothesis : numbers keyed by the users are > 0 
    float min = 0; // hypothesis : numbers keyed by the users are > 0 
    int i, c; 
    float sum = 0; 
    float nu[100]; // hypothesis : no more than 100 numbers keyed by user 
        // hypothesis : float type is enough to store numbers keyed by user 
    printf("Number of values :"); 
    scanf("%d",&i); 

    for (c = 0; c < i; c++) 
    { 
     printf("values="); 
     scanf("%f",&(nu[c])); 

     if (nu[c] > max) { 
      max = nu[c]; 
     } 

     if (min == 0) { 
      min = nu[c]; 
     } else if(nu[c] < min) { 
      min = nu[c]; 
     } 

     sum = sum + nu[c]; 
    } 
    printf("sum = %f \n",sum); 
    printf("maximum value = %f \n", max); 
    printf("minimum value = %f \n", min); 

    return 0; 
} 
+0

谢谢我得到了最大值的答案,但我没有得到最小值的答案 – 2010-08-25 06:07:55

+0

我更新了源代码来计算最小值并更正迭代(c应该从0开始,因为C中的数组总是从0开始) – 2010-08-25 07:12:45

+0

谢谢我得到了答案 – 2010-08-25 12:04:41

0

删除临时(不能看到它的点)

设置最大的一些初始值(例如,第一个数组的元素)

和if语句改变:

if (nu[c]> max) 
      { 
        max = nu[c]; 

      } 
0

这可能不是解决你r的问题,但nu[c]似乎不好分配给我,因为c值不确定

1

您需要一个for循环,而不是嵌套循环。

  1. 使用的第一个for循环获取输入
  2. 分配输入数组变量的第一个元素,称之为最大
  3. 使用比较数组中的每个元素的for循环再次
  4. 如果你得到大于最大值然后使用以下代码将max最大值改变为该值:

    if(max < a [i]) max = a [i];

在这些步骤结束时,您可以获得最大值。

试着把这些步骤放到C代码中,它应该没问题。您编写的代码存在一些问题。我写了一小段代码让你获得元素数量的输入,并将它们存储到你的数组中。

int number_of_elements; 
printf("Enter the number of elements:\n"); 
scanf("%d", &number_of_elements); 
for(i=0;i<number_of_elements;i++) 
scanf("%f",&a[i]); 
1

下面是一些有用的指针代码:

#include <stdio.h> 
int main(void) 
{ 
     double temp[0]; 
     float max,min; 
     float i; 
     short c,j,k;      // k isn't used anywhere. 
     float sum=0; 
     float nu[c];      // c isn't set at this point so the 
             // runtime couldn't create an 
             // array anyway. 

     printf("Number of values :"); 
     scanf("%f",&i); 

     for (c=1;i>=c;c++) 
     { 
      printf("values="); 
      scanf("%f",&nu[c]); 
             // You should set min/max to nu[c] 
             // if c is 1 regardless. 
      nu[c]=temp[0];     // Why are you scanning into nu[c] 
             // then immediately overwriting? 

      for (j = 0; i>=j; j++)   // Why figure out the max *every* 
      {        // time you enter a value? 
      if (temp[0]> nu[c]) 
      { 
      //  nu[c]=temp[0]; 
        max = temp[0]; 

      } 
     } 
     sum = sum + nu[c]; 
     } 
     printf("sum = %f \n",sum); 
     printf("maximum value = %f\n",max); 
} 

我也建议你回去my original answer并创建你的代码。与试图摆弄数组相比,它确实是一种更干净的方法。

让我强调一下:如果你使用的是数组,那么你就是这么做的!

1

或者这

#include <stdio.h> 

int main(void) 

{ 
float temp; 

int val,i,j,k; 

double sum = 0; 

double number[val]; 

printf("Enter the number of values: "); 

scanf("%d", &val); 

double number[val]; 

for(i=1; i <= val ;i++) 
{ 
    printf("enter a value: "); 

    scanf("%lf", &number[i]); 

    sum = sum + number[i]; 

} 

for(i=1;i<=val;i++) 

     { 

    for(j=i+1;j<=val;j++) 

    { 

    if(number[i] > number[j]) 

    { 

    temp=number[i]; 

    number[i]=number[j]; 

    number[j]=temp; 

    } 

    } 

} 

printf("Sum = %.lf\n", sum); 

printf ("Maximum element: %f\n",number[val]); 

printf ("Minimum element: %lf\n", number[1]); 

}