The scanf before the switch/case function is never used and it completely stops the program. Althought I'm also not sure if the functions work since i haven't been able to properly run the program. I just want for the scanf to work properly. It is probably some dumb mistake like always but if you anyone could help me find out why it wont even prompt for an input then i would be very grateful. Any other corrections to the rest of the code would also be greatly appreciated.
#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);
}