我一直在試圖編寫一個程序,它將輸入存儲到一個數組中,然後允許我將它打印出來。它也讓我知道哪個數字最大。我想弄清楚的是如何讓我的程序告訴我數組中輸入的最大數量的次數(發生次數)。這是我的代碼到目前爲止。截至目前,該代碼輸出我輸入到數組中的數字,數組中最大的元素,以及每次輸入的數字的出現(數字的出現是不正確的)。在每個數字的所有事件數量結果是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,並且您的第一個max是elem [0]。當您遍歷數組的其餘部分時,找到一個更大的元素時,將最大值重置爲新值,並將出現次數重置爲1,否則如果元素等於當前最大值,則會提高出現率。而已。它不應該採取一次以上的掃描來完成*所有這些*。 – WhozCraig 2015-02-12 09:18:13