计算平均中位数模式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
I have this homework assignment where the user is asked to input numbers and then calculates the mean median and mode, followed by asking if he/she wants to play again, and either repeating the program or quitting. Everything compiles, but I can seem to figure out the few things going wrong:
The mean works. the median doesn't. If the array of ints has an even length, ie 4 numbers in the array, the median is supposed to be the middle two numbers averaged out. so if the numbers are '1, 3, 5, 6' in order, then the median should be 4.000000. The mode doesn't work either, and when asked to 'play again?' any answer causes the program to suddenly exit and crash. can someone help me find the error in my mean median mode calculations, and help me with the menu?
#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;
}
}
the output should be something like this:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我发现了 3 个问题:
double
,您应该使用%f
。不是%d
或%i
。tally
。goAgain
中,temp
应为char
,并且应使用%c
而不是%s< /代码>。
Well, I found 3 problems:
double
, you should use%f
. not%d
or%i
.tally
before using.goAgain
,temp
should bechar
, and you should use%c
instead of%s
.对于 findMedian,您无需对整个数组进行排序。
会没事的。
For findMedian, you no need to sort the whole array.
Will be fine.