In C programming language please.
-Construct a program that will make use of user defined functions, the do/while loop, the for loop and selection.
-Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit.
-If the user enters in a 0 or negative number the program should exit with a message to the user indicating they chose to exit.
-If the user has entered in a valid positive number, your program should pass that number to a user defined function.
-The user defined function will use a for loop to compute an average value. The function will use the number passed to it to determine how many times it will prompt the user to supply a value. The user may enter in any number value positive, floating point or negative.
-The for loop will continue to prompt the user, calculating the average of the values entered. The function should return the calculated value to the calling function. The calling function using the do/while loop should print out the average and then repeat the process until the user enters in the signal to stop as described previously.
#include <stdio.h>
float averageFinder(int x); // function prototype
int main() //main function
{
int a; //declaring integer variable a
float average; //declaring floating variable average
do //using do while loop for the input of the number of inputs for calculating average
{
printf("\nEnters number of values : "); //Inputting the no. of
scanf("%d",&a); //values to compute average on
if(a>0) //only for positive integers for the process
{
average=averageFinder(a); // calling the averageFinder function with argument "a"
printf("average = %f",average); //after calculating average of the first do-while loop, prints the output average
}
}
while(a>0); //loop will continue till the inputted value is positive integer
printf("\nYou chose to exit !"); //if negative value is inputted program will end
return 0;
}
float averageFinder(int x) // averageFinder function to calculate average for "x" numbers
{
float result; //declaring result variable as float
float input; //declaring input variable as float
for(int i=0;i<x;i++) // for loop will prompt till the inputted no of values earlier above
{
printf("Enter %dst element : ",i+1); //Inputting each of the
scanf("%f",&input); //values for calculating average
result+=input; //summing each of the values of element in variable result
}
result=(result/(float)x); //finally calculating average by dividing float by total no of values
return result; // returns the average of all prompted values
}
Code Pic:
Output Pic :
Summary : C Program that uses user-defined functions, do/while loop, for loop and selection. It uses a do-while loop, and ask the user to enter in a +ve value i.e the number of values they wish to have caculated average by the program or if entered a negative value or zero 0, the program will end with a return message as "You chose to exit". The user-defined function here, named as averageFinder takes the previously prompted +ve value as its parameter and uses the for-loop to input the numbers on which further average is calculated.
The screenshot of the Ouput as well as the code is provided above.
Each line in the code is explained clearly with comments.
P.S. Hope you are satisfied with the provided solution, please give a thumbs up, Good Day !
Get Answers For Free
Most questions answered within 1 hours.