从不使用开关/外壳功能之前的scanf,它会完全停止程序。 虽然我也不确定这些功能是否有效,因为我还没有能够正常运行该程序。我只是想让scanf正常工作。这可能是一些愚蠢的错误,但如果你有人能帮助我找出为什么它甚至不会提示输入那么我会非常感激。对其余代码的任何其他更正也将非常感激。
#include <stdio.h>
#include <math.h>
#define Max_Numbers 50//defined max array size
float CalcAverage(float list[], int numbers);//function to calulate average
int Minimum(float list[], int numbers);//calculates minimum
int Maximum(float list[], int numbers);//calculate max
float StandDev(float list[], int numbers);//calculates standard deviation
int main()
{
int numbers;
float output;
int idx;
char option;
float list[Max_Numbers];
printf("Enter the amount of numbers you wish to input:\n");
scanf ("%d", &numbers);//prompting for size of array
if(numbers > Max_Numbers)
{
printf("Too many numbers\n");// im not really sure how to get this to work as well
return -1;
}
for (idx = 0; idx < numbers; idx++)//counter for entered values
{
printf("Enter a number\n");
scanf("%d", &list[idx]);
}
printf("What would you like to do?\n");
printf("(1): Calculate Average\n"); //menu options to determine which
printf("(2): Calculate Maximum\n"); //function will be used
printf("(3): Calculate Minimum\n");
printf("(4): Calculate Standard Deviation\n");
printf("Enter menu option:\n");
scanf("%c", &option);//scanf for menu options, program stops before this scanf
switch (option)//I believe the program is breaking at this switch
{
case ('1'):
output = CalcAverage(list, numbers);//calls to CalcAverage
break;
case ('2'):
output = Maximum(list, numbers);//calls to Maximum
break;
case ('3'):
output = Minimum(list, numbers);//Calls to minimum
break;
case ('4'):
output = StandDev(list, numbers);//calls to StandDev
break;
default:
printf("Invalid menu option\n");\\default option in case wrong value entered
break;
}
printf("The result is %.2f", output);// the result
}
float CalcAverage(float list[], int numbers)//function definition for average
{
int idx;
float sum = 0;
float result;
for(idx = 0; idx < numbers; idx++)//counter used to add all values
{
sum = sum + list[idx];
}
result = (float) sum / numbers;
return result;
}
int Maximum(float list[], int numbers)//function def. for max
{
int idx;
int maximum = 0;
for(idx = 0; idx < numbers; idx++)//counter used to compare values
{
if (maximum < list[idx]) //im not sure if i wrote some of
{ //these functions correctly
maximum = list[idx]; //The main problem i have is
} //that scanf that breaks the
} //program
return maximum;
}
int Minimum(float list[], int numbers)//fun def. for min
{
int idx;
int minimum = 0;
for(idx = 0; idx < numbers; idx++)
{
if (minimum > list[idx])
{
minimum = list[idx];
}
}
return minimum;
}
float StandDev(float list[], int numbers)//function def. for standard deviation
{
int idx;
float avg;
float newlist[numbers];
float newavg;
avg = CalcAverage(list, numbers);
for(idx = 0; idx < numbers; idx++)
{
newlist[idx] = (list[idx] - avg) * (list[idx] - avg);//Is this the proper way to put those
} //values in the new array?
newavg = CalcAverage(newlist, numbers);
return sqrt(newavg);
}