Question

In: Computer Science

Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...

Problem: Construct a C 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 a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If the user has entered 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 the signal to stop as described previously.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<stdio.h>

//method to read n numbers and find average
float processValues(int n){
        float sum=0, input;
        //looping for n times
        for(int i=1;i<=n;i++){
                //asking and reading a value
                printf("Enter number #%d: ",i);
                scanf("%f",&input);
                //adding to sum
                sum+=input;
        }
        //finding and returning average
        float avg=sum/n;
        return avg;
}

int main(){
        int n;
        //looping
        do{
                //asking, reading number of values
                printf("Enter the number of values (0 or negative to quit): ");
                scanf("%d",&n);
                //checking if value is positive
                if(n>0){
                        //processing values and finding average
                        float avg=processValues(n);
                        //displaying average
                        printf("The average of entered values: %.2f\n",avg);
                }
        }while(n>0); //loop until n is 0 or negative
        //displaying a goodbye message.
        printf("You chose to quit. Goodbye!\n");
        return 0;       
}

/*OUTPUT*/

Enter the number of values (0 or negative to quit): 5
Enter number #1: 11
Enter number #2: 22
Enter number #3: 33
Enter number #4: 44
Enter number #5: 55
The average of entered values: 33.00
Enter the number of values (0 or negative to quit): 3
Enter number #1: 100
Enter number #2: 50
Enter number #3: 25.45
The average of entered values: 58.48
Enter the number of values (0 or negative to quit): 4
Enter number #1: 100
Enter number #2: 100
Enter number #3: 98
Enter number #4: 99.5
The average of entered values: 99.38
Enter the number of values (0 or negative to quit): 0
You chose to quit. Goodbye!

Related Solutions

Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT