Question

In C programming language please. -Construct a program that will make use of user defined functions,...

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.

Homework Answers

Answer #1
#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 !

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
Develop a C++ program that determines the largest and second largest positive values in a collection...
Develop a C++ program that determines the largest and second largest positive values in a collection of data Prompt the user to enter integer values until they enter any negative value to quit You may presume the user will input at least two valid integer values Create a loop structure of some sort to execute this input cycle Maintain the largest and second largest integer as the user inputs data This logic should be placed inside your loop structure Arrays...
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
8.39 Lab 8d: Even Entry Loop This program will ask the user to enter an even...
8.39 Lab 8d: Even Entry Loop This program will ask the user to enter an even number and continue to do so until they enter the given QUIT condition. The prompt for the user to enter a number is written as follows: "Enter an even number or [QUIT] to quit:" The quitting condition in this case will be the number 999. If the number they entered was an even number, output the message "Good job!" Otherwise, output the message "[number]...
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2....
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2. Calculate Current in Amps 3. Calculate Voltage in volts 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for voltage in Volts and the current in Amps. Use the following formula: R= E/i Where: E= voltage in volts I= current in amps R= resistance in ohms If the user enters 2 the program should ask for the voltage...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Write the C++ language statements to ask a user to enter a number within the range...
Write the C++ language statements to ask a user to enter a number within the range of 0 to 100 (0 and 100 are valid inputs). When the value is not valid, you should prompt the user to enter another value. (The user may enter many invalid numbers, so you must use loop). Only after the user enters a valid number should you display the message “Good” and the value of the number.
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously...
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously prompt the user for numbers. When the user enters a negative number, print the highest positive number entered by the user and exit the program. Print a zero if they did not enter any positive numbers. 2.DivisionWrite an ANNA assembly program (div.ac) that divides two positive numbers that come from user input and returns both the quotient and the remainder. For example, if the...
Use C code please! Write the function definition for a function called FindQuo that takes one...
Use C code please! Write the function definition for a function called FindQuo that takes one argument of type double (arg1 ) and returns a double.  The purpose of the function is to calculate the quotient of arg1 divided by a double (num) that will be entered by the user. Inside the function declare ask and get a double value from the user called num Use a loop (while or do/while) to ensure that the user does not enter a 0.0....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...