我遇到了一个问题,我的函数似乎只返回一个。我以为我正确地返回了函数,但似乎不是。
char goAgain()
{
char ch;
do
{
printf("Would you like to go again? ");
ch = getchar();
while(fgetc(stdin) != '\n');
}while(ch != 'n' || 'y');
return ch;
}
double findMedian(int array[], int length)
{
int mid;
double median, medianLeft, medianRight;
if(length % 2 == 0)
{
mid = length / 2;
// medianLeft = array[mid];
// medianRight = array[mid + 1];
}
else
{
mid = length / 2;
median = array[mid];
}
return median;
}
这就是我所说的中位数
double mean = findMedian(array, length);
为什么在我的回报中只给我一个。同样,当我尝试重复 goAgain 时,我也没有得到正确的 ch 来返回。
option = goAgain();
与 java 世界相比,c 世界中的情况有很大不同。
do
{
int num = menu();
if(num == 1)
fillArray(array, size);
else if(num == 2)
{
int newSize = readNum();
fillArray(array, newSize);
}
else if(num == 3)
{
int length = size;
sortArray(array);
double mean = findMean(array, length);
double median = findMedian(array, length);
printResults(mean, median);
}
else
{
printf("Please enter a valid number\n");
num = menu();
}
option = goAgain();
}while(option == 'y');