Question

In: Computer Science

Construct this program in C programming Please. Using a do/while loop, your program will ask/prompt the...

Construct this program in C programming Please.

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.

Solutions

Expert Solution

Note: Function name and variable names used are arbitrary. These can be changed according to the user's choice. Please refer to code snippets for a better understanding of comments and indentations.

IDE Used: Dev C

Program Code

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


// function that will that number of input and find average
float find_avg(int tot_inp)
{
   // variables
   int itr;
   float inp_val;           // each input value
   float tot_sum = 0;       // total sum
  
   // start loop find average
   for(itr = 0; itr < tot_inp; itr++ )
   {
       // get each number
       printf("\nNumber %d : ",itr+1);
       scanf("%f", &inp_val);
      
       // calculate total sum
       tot_sum += inp_val;
   }

   // return the average
   return tot_sum/tot_inp;
  
}

int main()
{
   // variables
   int tot_inp;       // total input
   bool more;           // more inputs true or false
   float the_avg;       // the average storage variable
  
   // do till user doesn't want to quit
   do
   {
       // prompt user to enter total number of inputs
       // ask to stop by entering 0 or negative value
       printf("Input number of values to enter (press 0 or negative value to terminate) : ");
       scanf("%d",&tot_inp);
      
       // if total input greater than 0
       if(tot_inp > 0)
       {
           printf("\nEnter %d values",tot_inp);
          
           // call the average finding function
           the_avg = find_avg(tot_inp);
          
           // print the average
           printf("\nAverage of %d entered values is %0.2f\n",tot_inp, the_avg);
          
           // set more to true i.e loop continue again
           more = true;
       }
      
       // else if 0 or negative
       else
       {
           // printf exit statement and set more to false i.e doesn't want to continue
           printf("\nYou choose to exit!! Quiting............");
           more = false;
       }
   }while(more == true);
      
   return 0;
}

Code Snippets

Sample Output


Related Solutions

A)Write a C++ program using a while loop for Programming Exercise 5.1 on p. 193. Turn...
A)Write a C++ program using a while loop for Programming Exercise 5.1 on p. 193. Turn in a printout of the program and a printout of the results. Test the program for the two test cases in the book along with a third test case that includes 10 valid numbers (including some negative and some positive).
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...
C programming. Explain by taking a programming example how do while loop is different from while...
C programming. Explain by taking a programming example how do while loop is different from while loop?
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...
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:...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
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: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Multiples of 2 and 3: write a c++ program Using a while loop, write a program...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
1 for each of the problems listed below write the c++ program while using WHILE loop...
1 for each of the problems listed below write the c++ program while using WHILE loop structures A program will display each term in the following sequence 1 -10 100 -1000 10000 -100000 1000000 A program will calculate and display the corresponding celsius temperatures for the given Farenheit ones from 0f to 212 f (hint c=(f-32/1.8)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT