2012-02-24 49 views
1

我有這個家庭作業,其中用戶被要求輸入數字,然後計算平均中位數和模式,然後詢問他/她是否想再次玩,並重復該程序或狹路相逢。一切都編譯完成,但我似乎可以弄清楚出錯的幾件事:計算平均中位數模式c編程陣列

平均工作。中位數沒有。如果整數數組的長度爲偶數,即數組中的4個數字,則中位數應該是中間兩個數字的平均值。所以如果數字是「1,3,5,6」,那麼中位數應該是4.000000。該模式也不起作用,當被要求「再次玩耍?」時任何答案都會導致程序突然退出並崩潰。有人可以幫我找到我的平均模式計算中的錯誤,並幫助我的菜單?

#define MAX 25 
#include <stdio.h> 
#include <stdbool.h> 
#include <time.h> 
#include <stdlib.h> 

int readTotalNums(); 
void fillArray(int total, int nums[]); 
void sortArray(int nums[], int total); 
double findMean(int nums[], int total); 
double findMedian(int nums[], int total); 
int findMode(int nums[], int total); 
void printResults(double mean, double median, double mode); 
bool goAgain(); 

int main() { 
    int nums[MAX]; 
    int total; 
    double mean, median, mode; 
    do { 
     total = readTotalNums(); //guarantee 1-25 
     fillArray(total, nums); //read in the #s don't need to check range 
     sortArray(nums, total); 
     mean = findMean(nums, total); 
     median = findMedian(nums, total); 
     mode = findMode(nums, total); 
     printResults(mean, median, mode); 
    } while (goAgain()); 
    return 0; 
} 

int readTotalNums() { 
    int num; 
    do { 
     printf("How many numbers? "); 
     scanf("%i", &num); 
    } while (num < 1 || num > 25); 
    return num; 
} 

void fillArray(int total, int nums[]) { 
    int temp; 
    int i; 
    printf("Please enter %i numbers\n", total); 
    for (i = 0; i <= total-1; i++) { 
     scanf("\n%i",&nums[i]); 
    } 
} 

void sortArray(int nums[], int total) { 
int x; 
int y; 
for(x=0; x<total; x++) { 
    for(y=0; y<total-1; y++) { 
     if(nums[y]>nums[y+1]) { 
      int temp = nums[y+1]; 
      nums[y+1] = nums[y]; 
      nums[y] = temp; 
     } 
    } 
} 
} 

double findMean(int nums[], int total) { 
    int i; 
    double sum = 0.0; 
    for(i = 0; i < total; i++) { 
     sum += nums[i]; 
    } 
    return (sum/total); 
} 

double findMedian(int nums[], int total) { 
    int temp; 
    int i,j; 
    for(i=0;i<total;i++) 
     for(j=i+1;j<total;j++) { 
      if(nums[i]>nums[j]) { 
       temp=nums[j]; 
       nums[j]=nums[i]; 
       nums[i]=temp; 
      } 
     } 
     if(total%2==0) { 
      return (nums[total/2]+nums[total/2-1])/2; 
     }else{ 
      return nums[total/2]; 
     } 
} 

int findMode(int nums[],int total) { 
    int i, j, maxCount, modeValue; 
    int tally[total]; 
    for (i = 0; i < total; i++) { 
     tally[nums[i]]++; 
    } 
    maxCount = 0; 
    modeValue = 0; 
    for (j = 0; j < total; j++) { 
     if (tally[j] > maxCount) { 
      maxCount = tally[j]; 
      modeValue = j; 
     } 
    } 
    return modeValue; 
} 

void printResults(double mean, double median, double mode) { 
    printf("Mean: %d\tMedian: %d\tMode: %i", mean, median, mode); 
} 


bool goAgain() { 
    char *temp; 
    printf("\nWould you like to play again(Y/N)? "); 
    scanf("%s", &temp); 
    while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y') { 
     printf("\nI am sorry that is invalid -- try again"); 
     printf("\nWould you like to play again(Y/N)? "); 
     scanf("%s", &temp); 
    } 
    if (temp == 'y' || temp == 'Y') { 
     return true; 
    } else { 
     return false; 
    } 
} 

輸出應該是這樣的:

How many numbers 4 
Please enter 4 numbers 
6 
2 
5 
25 
Mean: 9.50 Median: 5.50 Mode: 2 
Go again (y/n) n 
+1

%d不適用於雙打,適用於整數。它是浮點數的%f或%lf – Avi 2012-02-24 01:43:25

+0

@Avram'6 + 5'是'11'。不''7。 – asaelr 2012-02-24 01:47:52

+0

你是對的 – Avi 2012-02-24 01:53:06

回答

2

嗯,我發現3個問題:

  1. 你的printf是錯誤的。要打印double,您應該使用%f。不是%d%i
  2. 使用前,您應該初始化tally
  3. goAgaintemp應該是char,您應該使用%c而不是%s
+0

@ nandakumar-edamana,請不要修復沒有損壞的東西。 – asaelr 2017-12-01 12:45:41

-1

對於findMedian,您不需要對整個數組進行排序。

for(i=0;i < int(total/2)+1;i++) 

會沒事的。