In: Computer Science
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need to keep track of how many values have been entered into the array. Then the program will determine the largest value entered and display that value. To determine the largest value, pass both the array and the count of how many values were entered to a function named double max(double values[], int numValues). The second parameter of the function indicates the actual number of values stored in the array. The function will then use a loop to determine the largest value. When the function returns to main, the program should display the largest value (i.e., the print should be in main).
#include <stdio.h>
#define MAX 100
double max(double values[], int numValues)
{
double maxValue = values[0];
for(int i = 1; i < numValues; i++)
{
if(values[i] > maxValue)
{
maxValue = values[i];
}
}
return maxValue;
}
int main()
{
char toContinue = 'Y';
double temp;
double values[MAX] = {0.0};
int numValues = 0;
while(numValues < MAX)
{
scanf("%lf", &temp);
values[numValues] = temp;
numValues++;
printf("Want to enter more (Y/y) : ");
scanf("%s", &toContinue);
if(toContinue != 'Y' && toContinue != 'y')
{
break;
}
}
double maxValue = max(values, numValues);
printf("%.6lf\n", maxValue);
return 0;
}