第三天的C編程讓我忍無可忍。我正在做一個初學者的練習,其中我生成隨機數並計算均值,標準偏差,中位數和模式。在C中計算模式 - 數學有點不對
模式中的問題。我正在繼續研究其他一些項目,但同時我會發布此信息,看看是否有人能夠發現我的錯誤。用戶在開始時輸入隨機數的範圍和它們的數量。如果最小值爲1,則模式返回正確的值,但如果最小值較大,則返回正確的值。
如果對如何允許多個模式有任何見解,我也會很感興趣 - 我有一個近似的想法如何做到這一點(額外的循環和額外的數組?但不是很確定我如何「 d只處理打印新陣列中的相關值)。
這裏的(只的相關部分)我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main() {
// setting parameters
int SIZE, MIN, MAX;
printf("How low should the smallest random number be?\n");
scanf("%d",&MIN);
printf("How high should the largest random number be?\n");
scanf("%d",&MAX);
printf("How many random numbers do you want?\n");
scanf("%d",&SIZE);
int rnx[SIZE];
int biggles, *tally, count=0;
int mode;
int i,j;
float mean, sumdev, median;
tally = (int*) calloc (MAX-MIN,sizeof(int)); // creates an array for the tally in the mode function and initializes it to zero for the incrementing.
srand(time(NULL)); // random seed outside the loop
// generate random numbers into an array
for(i=0;i<SIZE;i++) {
rnx[i]=round(((double)rand()/(RAND_MAX)*(MAX-MIN)+MIN));
}
BubbleSort(rnx,SIZE); // left out for brevity the actual function I wrote for this but it works
// calculates the mode
for(i=MIN;i<MAX;i++) {
for(j=0;j<SIZE;j++) {
if(rnx[j]==i) {
tally[i-MIN]++; // in the second array we register how many times each number occurs in the random sequence, checking from the minimum to maximum.
}
}
}
mode = biggles;
// for (j=0;j<10;j++) {
for(i=MIN;i<MAX;i++) {
if(tally[i-MIN]>count) {
count=tally[i-MIN];
if(count>1) {
mode=i-MIN+1; }
}
}
if (mode!=biggles) {
printf("The mode of the random numbers is %d\n",mode); }
else { printf("The random numbers have no mode.\n"); } // in case there is no mode. but what if there is more than one?
free(tally);
return 0;
}
我注意到的第一件事是'biggles'永遠不會被初始化,所以當你做'mode = biggles'時''你把'mode'設置爲一個完全隨機的值。(另外,'biggles'不是一個描述性或有意義的名稱。) – Patashu
縮進需要修復。另外,「無模式」意味着一個空的列表。這可能不是檢查它的最好方法。如果您至少有一個號碼,則至少有一種模式。 – xaxxon
@Patashu'不確定'將是比'完全隨機'更好的單詞選擇。 – Dukeling