Question

In C please! Thank you! Write a program that finds the smallest (min), largest (max), and...

In C please! Thank you!

Write a program that finds the smallest (min), largest (max), and average (mean) of N values entered by the user. Your program should begin by prompting the user for N, the number of values to be entered. Then the program should accept that number of values, determining the min, max, and mean. Then it should display those values.

This is an extension of a previous lab. First, get it working for N values. Then, extend it to include error-checking, in case the user makes a mistake when entering values.

When scanning for a value for N, the program should use error checking to protect against erroneous inputs from the user and only accept positive integer values. The other values entered can be any value, but error checking should still be employed to ensure that the user entered a value. If there is ever an incorrect input the user should be re-prompted for the value and told of the error.

Steps for the program:

  • Prompt the user for N
  • Scan the value of N
  • Prompt and Scan for N Values
    • Simultaneously search for Max and Min values
    • Add all values to generate a sum
  • Calculate the average by dividing the sum by N, MEAN=sum/N
  • Display min, max, and mean values

An example run of the program is given below where the user enters five values (ie N=5):

MIN, MAX, and MEAN CALCULATOR

How many values are to be entered?: -4
INPUT ERROR!
How many values are to be entered?: 4

Value 1: 2
Value 2: as
INPUT ERROR!
Value 2: 17
Value 3: 3
Value 4: 5

The minimum value is 2, the maximum value is 17, and the average value is 6.75.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    //Declare variables
    int N,values[50],max,min,i,num;
    float sum=0,mean;
  
    printf("How many values are to be entered?");
    scanf("%d",&N);
    while(N<0)
    {
    printf("INPUT ERROR!");
    printf("\nHow many values are to be entered?");
    scanf("%d",&N);
    }
  
    for(i=0;i<N;i++)
    {
     printf("Value %d : ",i+1);
     while(scanf("%d",&values[i])==0)
     {
     printf("INPUT ERROR!");
     printf("\nValue %d : ",i+1);
     scanf("%*s");
     }
    }
     max=min=values[0];
     for(i=0;i<N;i++)
     {
     sum+=values[i];
     /* If current element of array is greater than max */
     if(values[i]>max)
     {
      max = values[i];
      }

      /* If current element of array is smaller than min */
      if(values[i]<min)
      {
       min = values[i];
       }
     }
   
    mean=sum/N;
    printf("The minimum value is %d, the maximum value is %d, and the average value is %.2f.",min,max,mean);
    return 0;
}

Output

Add a comment
Know the answer?
Add Answer to:
In C please! Thank you! Write a program that finds the smallest (min), largest (max), and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • please solve this question: Write a character Max-Heap Builder program in C++. The program should display...

    please solve this question: Write a character Max-Heap Builder program in C++. The program should display the menu below. Each item in the menu should be implemented in a function. a. Add a node. One node to be added to the max-heap. b. Delete a node. One node to be deleted from the max-heap. C. Search a node. Returns true if the node exists in the max-heap, otherwise it returns false. d. Print the tree. Prints the heap in level-order...

  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • 3. (c-program) Write a program that finds the largest and smallest value in a series of...

    3. (c-program) Write a program that finds the largest and smallest value in a series of numbers entered by a user. The user must enter 25 values. Note if that if the user enters "0" they must correct and add another number. Print out the original entered values, the largest number, the smallest number. Note whether the user tried to enter "O".

  • Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds...

    Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds the sum and average of a series of numbers he program should first prompt the user to enter total numbers of numbers are to be summed and averaged. It should then as for input for each of the numbers, add them, and print the total of the numbers and their average 2. Write a progra m that finds the area of a circle. The...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • Write a program in C++ to calculate the average (mean) and the standard deviation of a...

    Write a program in C++ to calculate the average (mean) and the standard deviation of a list of numbers using vectors. Your program should... Prompt for the number of values the user wishes to enter Prompt the user to enter the values one at a time for the number of requested values into a vector Calculate and display the Mean and Standard Deviation The flow of your code should be similar to: int main() { /** Prompt the user and...

  • Write a program in C++. You need everything everythihng given You will create a function that...

    Write a program in C++. You need everything everythihng given You will create a function that validates input. It should accept only non-negative integers. If the input is incorrect the function should throw an exception. The exception will not be caught in the function. You will create a function that reads in 2 integer values. One is a number that will be raised to the power of the second. So if 5 and 6 are entered the result would be...

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • Part A) Write a C++ program that calculates the area under a curve. Here is the...

    Part A) Write a C++ program that calculates the area under a curve. Here is the equation: f(x) = x^2 +1.5x +4 You must prompt the user for the beginning and the ending x values. You are to assume, but not check, that the user will put in whole positive numbers. The units are inches. The program input/output should look something like this: This program calculates the area under a curve between two points on the x axis. The equation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT