C Language How would I link the int addAll function into main\'s case \'b\' that
ID: 3919367 • Letter: C
Question
C Language
How would I link the int addAll function into main's case 'b' that sums up array values and case 'c' that takes average of array values. Here is my code:
#include <stdio.h>
#include <math.h>
int addAll(int arr [], int len)
{
int sumArr = 0;
int y;
for(y = 0; y < len; y++)
{
sumArr = sumArr + arr[y];
}
return sumArr;
} //end of addAll function
int main(void)
{
//Declare Variables
char letter = 'z'; // letter choice from user
int length; //length of array
int i; //loop control variable
int sumValues;
double sumAverage;
int j; //loop control max variable
int k; //loop control min variable
int f; //loop control variable for array copy
printf("Please enter the length of your array: ");
scanf("%d", &length);
int data [length]; //data array to process
printf("Please enter each value for the array on a new line. ");
for(i = 0; i < length; i++)
{
printf("Enter next value: ");
scanf("%d", &data[i]);
}
while (letter != 'q')
{
//Create menu of options for user
printf("Please enter b, c, d, e, or f to process array ");
printf("b. Compute the sum of array values ");
printf("c. Compute average of array values ");
printf("d. Find the max values in the array ");
printf("e. Find the min values in the array ");
printf("f. Create a copy of the array ");
printf("q. Quit ");
//Read in user's choice
scanf(" %c", &letter);
switch(letter)
{
case 'b': printf("You selected b. ");
printf("Now computing the sum. ");
???????????
???????????
printf("The sum of values in the array is: %d ", sumValues);
break;
case 'c': printf("You selected c. ");
printf("Now computing the average. ");
?????????????
?????????????
sumAverage = sumValues / length;
printf("The average of the values in the array is: %lf ", sumAverage);
break;
case 'd': printf("You selected d. ");
printf("Now locating the max. ");
double max = data [0];
int indexOfMax = 0;
for(j = 0; j < length; j++)
{
if (data[j] > max)
{
max = data[j];
indexOfMax = j;
} //end max if loop
} //end max for loop
printf("The max value in the array is: %lf ", max);
break;
case 'e': printf("You selected e. ");
printf("Now locating the min. ");
double min = data [0];
int indexOfMin = 0;
for(k = 0; k < length; k++)
{
if (data[k] < min)
{
max = data[k];
indexOfMin = k;
} //end min if loop
} //end min for loop
printf("The min value in the array is: %lf ", min);
break;
case 'f': printf("You selected f. ");
printf("Now creating a copy of array. ");
printf("The copied array is: ");
break;
case 'q': printf("Bye! ");
break;
default : printf("Error. Select one from menu. ");
break;
} // end of switch
} //end of while loops
} //end of main function
Explanation / Answer
here is the complete C program as per the requirement.
=========================================================
Program:
=========================================================
#include <stdio.h>
#include <math.h>
int addAll(int arr [], int len)
{
int sumArr = 0;
int y;
for(y = 0; y < len; y++)
{
sumArr = sumArr + arr[y];
}
return sumArr;
} //end of addAll function
int main(void)
{
//Declare Variables
char letter = 'z'; // letter choice from user
int length; //length of array
int i; //loop control variable
int sumValues;
double sumAverage;
int j; //loop control max variable
int k; //loop control min variable
int f; //loop control variable for array copy
printf("Please enter the length of your array: ");
scanf("%d", &length);
int data [length]; //data array to process
printf("Please enter each value for the array on a new line. ");
for(i = 0; i < length; i++)
{
printf("Enter next value: ");
scanf("%d", &data[i]);
}
while (letter != 'q')
{
//Create menu of options for user
printf("Please enter b, c, d, e, or f to process array ");
printf("b. Compute the sum of array values ");
printf("c. Compute average of array values ");
printf("d. Find the max values in the array ");
printf("e. Find the min values in the array ");
printf("f. Create a copy of the array ");
printf("q. Quit ");
//Read in user's choice
scanf(" %c", &letter);
switch(letter)
{
case 'b': printf("You selected b. ");
printf("Now computing the sum. ");
sumValues = addAll(data, length);
printf("The sum of values in the array is: %d ", sumValues);
break;
case 'c': printf("You selected c. ");
printf("Now computing the average. ");
sumValues = addAll(data, length);
sumAverage = sumValues / length;
printf("The average of the values in the array is: %lf ", sumAverage);
break;
case 'd': printf("You selected d. ");
printf("Now locating the max. ");
double max = data [0];
int indexOfMax = 0;
for(j = 0; j < length; j++)
{
if (data[j] > max)
{
max = data[j];
indexOfMax = j;
} //end max if loop
} //end max for loop
printf("The max value in the array is: %lf ", max);
break;
case 'e': printf("You selected e. ");
printf("Now locating the min. ");
double min = data [0];
int indexOfMin = 0;
for(k = 0; k < length; k++)
{
if (data[k] < min)
{
max = data[k];
indexOfMin = k;
} //end min if loop
} //end min for loop
printf("The min value in the array is: %lf ", min);
break;
case 'f': printf("You selected f. ");
printf("Now creating a copy of array. ");
printf("The copied array is: ");
break;
case 'q': printf("Bye! ");
break;
default : printf("Error. Select one from menu. ");
break;
} // end of switch
} //end of while loops
} //end of main function
=========================================================
Sample Output:
Please enter the length of your array:
5
Please enter each value for the array on a new line.
Enter next value:
2
Enter next value:
4
Enter next value:
6
Enter next value:
8
Enter next value:
10
Please enter b, c, d, e, or f to process array
b. Compute the sum of array values
c. Compute average of array values
d. Find the max values in the array
e. Find the min values in the array
f. Create a copy of the array
q. Quit
b
You selected b.
Now computing the sum.
The sum of values in the array is: 30
Please enter b, c, d, e, or f to process array
b. Compute the sum of array values
c. Compute average of array values
d. Find the max values in the array
e. Find the min values in the array
f. Create a copy of the array
q. Quit
c
You selected c.
Now computing the average.
The average of the values in the array is: 6.000000
Please enter b, c, d, e, or f to process array
b. Compute the sum of array values
c. Compute average of array values
d. Find the max values in the array
e. Find the min values in the array
f. Create a copy of the array
q. Quit
q
Bye!
=========================================================
Kindly Check and Verify Thanks..!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.