2010-08-24 250 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]); 

}