-2
問題:我試圖在一個隨機整數數組中找到最常見的六個數字,但我只找到了我的循環數。程序不顯示最頻繁的數字
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Salih Atacan Karagöz 210201055
int main()
{
long x=210201055; //password
long y=0;
printf("Enter password:");
scanf("%d",&y);
if(x==y) //password check if statement is true progrom continues
{
int tekrar[49]= {0}; //array for counting repeating numbers
int numbers[49]= {0}; //array for creating random numbers
int z; //integer for loop counter
int counter=0; //counter for loop
int i;
int j=0;
int high=0;
printf("Enter loop count:");
scanf("%d",&z);
srand(time(NULL));
for(counter=1; counter<=z; counter++) //loop will do under these lines z times
{
i=0;
while(i<6) //every time we need 6 random numbers so loop need to create max 6 numbers (0,1,2,3,4,5,)
{
i++;
numbers[i]=rand()%50+1; //+1 for prevent creating zeros mod 49 for creating numbers below 49
tekrar[numbers[i]]++; //array +1 t its value every time same number comes
}
}
for(i=1; i<=49; i++)
{
printf("%d->%d\t",i,tekrar[i]); //for printing created numbers
}
printf("\n -----------\tMost Frequent Six Numbers\t-------------\n");
printf(":(");
int count;
int maxCount = 0;
int maxValue = 0;
for(j=0;j<9;j++){
count=0;
for(i=0; i<49; i++){
if (numbers[i]==numbers[i+1]){
count++;
}
if(count>maxCount){
maxCount==count;
}
}
maxValue=numbers[i];
}
printf("Number: %d", maxValue);
printf("\n-----------GUESS-----------\n"); //we will use bouble sort here
printf(":(");
}
else //if password is wrong program continues from here
{
printf("Wrong Password");
}
}
之前,你可以找到6個最常見的數字,你需要無需運行時錯誤即可運行代碼。 'int tekrar [49] = {0};'是塊的一個元素太小:'for(i = 1; i <= 49; i ++){printf(「%d - >%d \ t」,i ,tekrar [i]);}'Index'i'會循環到49,但是'tekrar'的最大索引值應該限制爲48個。另外,我假設_bouble sort_,你打算用_bubble sort_?查找*** [qsort(...)](http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm)*** – ryyker 2014-12-02 13:44:36
@CiocomoTrombetta請創建一個[最小,完整,可驗證的示例](http ://stackoverflow.com/help/mcve) – 2014-12-02 13:48:52
是的,我錯過了那一個。其中的小問題 – 2014-12-02 13:56:04