2015-02-12 77 views
0

我一直在試圖編寫一個程序,它將輸入存儲到一個數組中,然後允許我將它打印出來。它也讓我知道哪個數字最大。我想弄清楚的是如何讓我的程序告訴我數組中輸入的最大數量的次數(發生次數)。這是我的代碼到目前爲止。截至目前,該代碼輸出我輸入到數組中的數字,數組中最大的元素,以及每次輸入的數字的出現(數字的出現是不正確的)。在每個數字的所有事件數量結果是0.這顯然是不正確的。再次,我需要我的程序顯示最大的數字(它所做的)以及只有最大數字的出現次數。歡迎提供所有建議,提示或想法。謝謝。計算出現的最大數

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 
#include <ctype.h> 



int main() 
{ 
int arrayNum[15]; 
int a; 
int max=0; 
int location; 


for(a=0; a < 15; a++) 
    { 
     printf("Enter element %d:", a); 
     scanf("%d",&arrayNum[a]); 
    } 

for(a=0; a < 15; a++) 
    { 
     printf("%d\n", arrayNum[a]); 
    } 

for (a = 1; a < 15; a++) 
    { 
    if (arrayNum[a] > max) 
    { 
     max = arrayNum[a]; 
     location = a+1; 
    } 
    } 
printf("Max element in the array in the location %d and its value %d\n", location, max); 

for(a=0; a<15; a++) 
    { 
     if(arrayNum[a+1] == arrayNum[a]) 
      continue; 
     else 
      printf("Number %d: %d occurences\n", arrayNum[a]); 
    } 
return 0; 





} 
+1

保持一個發生次數,最初爲1,並且您的第一個max是elem [0]。當您遍歷數組的其餘部分時,找到一個更大的元素時,將最大值重置爲新值,並將出現次數重置爲1,否則如果元素等於當前最大值,則會提高出現率。而已。它不應該採取一次以上的掃描來完成*所有這些*。 – WhozCraig 2015-02-12 09:18:13

回答

0

替換最後一個for循環與下面的代碼

NoOfOccurances = 0; 
for(a=0; a<15; a++) 
    { 
     if(max == arrayNum[a]) 
     { 
      NoOfOccurances++; 
     } 

    } 

printf("Number %d: %d occurences\n", max,NoOfOccurances); 
0

對於你的第三個for循環中,一個在那裏你發現數量最多的數組中,我建議設定最大到arrayNum [ 0],這樣即使負數也能工作。

然後,要知道有多少次出現的最高數字,您需要一個count變量,每增加一個數組(count++),數組的數目就等於最大值。要做到這一點,你需要另一個for循環。

祝你好運。

0

我在代碼中發現了一些問題。首先,第三個for循環從1開始,但它不更新max作爲arrayNum[0]的值。

然後,手頭的問題,我想有兩個變量:

int max; // The maximum value 
int max_count; // The count of the maximum value 

接着,邏輯找到最大,計數,如下:

對於每一個元素,將它與所見的最大值進行比較。如果相等,則增加max_count。如果它更大,請用值更新max,並將max_count設置爲1.如果更小,則忽略它。喜歡的東西:

max = arrayNum[0]; 
max_count = 1; 
for (int a = 1; a < 15; ++a) 
{ 
    if (arrayNum[a] == max) 
     max_count++; 
    else if (arrayNum[a] > max) 
    { 
     max_count = 1; 
     max = arrayNum[a]; 
    } 
} 
0

所有你需要做的是引入新的變量來跟蹤max出現的次數。當找到新的值max時,將該計數設置爲零。當發現後續值等於max時,增加計數器。

順便說一句,您的代碼沒有正確地找到其當前形式的最大值。嘗試一個測試用例,其中數組元素都是負數。嘗試另一個測試用例,其中所有值均爲正值,並輸入第一個值(arrayNum[0])爲最大值。在這兩種情況下,您都會發現您的功能實際上不會找到最大值。

1

開始下面的循環就在max仍然是0讓

max = a[0]; 

    for (a = 1; a < 15; a++) 
    { 
    if (arrayNum[a] > max) 
    { 
     max = arrayNum[a]; 
     location = a+1; 
    } 
    } 

後來

int n=0; 
for(i=0;i<15;i++) 
{ 
    if(max == a[i]) 
    n++; 
} 

printf("Number of times max appears in the array is %d\n",n); 
0

你可以做你想做的事,只是一個循環迭代:

int count = 1; 
int position = 0; 
int max = arrayNum[0]; 
int N = 15; 
int p; 

for (p = 1; p < N; ++p) 
{ 
    if (arrayNum[p] > max) // Find a bigger number 
    { 
     max = arrayNum[p]; 
     pos = p; 
     count = 1; 
    } 
    else if (arrayNum[p] == max) // Another occurrences of the same number 
      count++; 
} 
-1

時間複雜度爲O(n)的簡單解決方案

int maxoccurence(int a[],int ar_size) 
{ 
    int max=a[0],count=0,i; 

    for(i=0;i<ar_size;i++) 
    { 
     if(a[i]==max)//counting the occurrence of maximum element 

     count++; 

     if(a[i]>max)//finding maximum number 
     { 
      max=a[i]; 

      count=1; 
     } 
    } 

    printf("Maximum element in the array is %d\n",max); 

    return count; 
}