Question

C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

C Programming

QUESTION 9

  1. Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style.

    Your program will calculate and print a new value based on the command that's entered, as follows:

    • 'A' or 'a': Calculate the average of the four input values
    • 'S' or 's': Calculate the standard deviation of the four input values
      • Your program must include the math library and use the sqrt() function, which returns the square root of its input argument, to calculate standard deviation
    • 'M' or 'm': Calculate the maximum value among the four input values

    If the user enters any other command, your program should print an error message that contains the invalid character. This program does not need to test for any other errors.

    If you don't know how to calculate any of those statistics, a quick web search should help you find the answers.

    Your output should match the format of the test cases shown below exactly. In each test case, the sample input values are underlined.

    TEST CASE 1:

    Enter input vals: 6 16 20 19
    Enter command: A
    Average: 15.250
    TEST CASE 2:
    
    Enter input vals: 5 -5 10 -2
    Enter command: s
    Standard deviation: 5.874
    
    
    TEST CASE 3:
    
    Enter input vals: 9 12 -1 3
    Enter command: M
    Maximum value: 12
    
    
    TEST CASE 4
    
    Enter input vals: 15 9 27 3
    Enter command: T
    Invalid command T
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The below is the c code for the given requirement.

#include <stdio.h>
int main()

{


   int a,b,c,d,sum,max;
   int avg,i;
   int i1,b1,c1,d1;
   int count,mean,sod,variance,standev;
   char val;   

   printf("Enter input vals:");
   scanf("%d%d%d%d",&a,&b,&c,&d);
   i=a;
   printf("Enter command:");
   scanf("%s",&val);
  
   switch (val)
   {
      
       case 'A':   sum=(i+b+c+d);
           avg=(sum/4);
               printf("Average:%d\n",avg);
               break;

       case 'S':  
              
               count=4;
               sum=(i+b+c+d);
               mean=sum/4;

               i=i-mean;
               b=b-mean;
               c=c-mean;
               d=d-mean;

               i1=i*i;
               b1=b*b;
               c1=c*c;
               d1=d*d;

               sod=i1+b1+c1+d1;
               variance=sod/4;
               standev=sqrt(variance);

               printf("Standard deviation:%d\n",standev);
               break;

       case 'M':  
               max=((i>b? i:b) > (c>d? c:d) ? (i>b? i:b) : (c>d? c:d));
               printf("Maximum value:%d\n",max);
               break;
      
       default:  
               printf("Invalid command\n");
               break;
   }


}

Add a comment
Know the answer?
Add Answer to:
C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...
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
  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • Question 01: Write a Java program that prompts for and reads the latitude of a location...

    Question 01: Write a Java program that prompts for and reads the latitude of a location on earth in the format:                    integerDegrees           integerMinutes       doubleSeconds     characterPosition where characterPosition is either N, n, S or s. If the input is valid your program converts the input latitude to decimal degrees where North latitudes are +ve, and South latitudes are –ve. 26 13 15.272400 N 26.220909 50 11 55.024800 S -50.198618 25 44 36.97 s -25.743603 43 10 23.49 n 43.173192 :...

  • Programming language is C++. 9. Write a program that reads digits and composes them into integers....

    Programming language is C++. 9. Write a program that reads digits and composes them into integers. For example, 123 is read as the characters 1, 2, and 3. The program should output 123 is 1 hundred and 2 tens and 3 ones. The number should be output as an int value Handle numbers with one, two, three, or four digits. Hint: To get the integer value 5 from the character '5' subtract '0' that is, '5'-'0'

  • CODE IN C++ 13.5 Alphabet Histogram Write a program that reads input character by character from...

    CODE IN C++ 13.5 Alphabet Histogram Write a program that reads input character by character from the given data file "data.txt" The program should be case insensitive and count the number of occurances of each character in the alphabet. It should print a histogram of the results as follows: A : B : C : * D : * E : * F : G : H : I : J : * K : L : M : N...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Question 1: Consider the following specification for a program. The program reads two inputs from the...

    Question 1: Consider the following specification for a program. The program reads two inputs from the user: a string s and an integer number n. The program concatenates s with itself n-1 times, if the following conditions are satisfied: 1- s consists of 3 characters. 2- The first character in s is a letter. 3- s consists of letters (A-Z a-z) and digits only (0-9) 4-0< n<5 If any of these conditions is not satisfied, the program terminates and prints...

  • In Java Programming: Create a flow chart for a program that prompts for an integer N...

    In Java Programming: Create a flow chart for a program that prompts for an integer N (number of students) and M (number of test scores for each students), and allows the user to N*M real numbers. The program then calculates the average for each student and class average. (1) Prompt the user to input N students and M test scores, then display the inputs. Enter number of students: You entered: 2 Enter number of test scores: You entered: 3 Enter...

  • C++ programming write a program that asks a user to enter a random IP number (IP...

    C++ programming write a program that asks a user to enter a random IP number (IP number is nothing but 4 numbers with dots in between and each number is between 0 and 255) (example: 1.2.3.4 or 255.255.255.255) The program you will write (Write the program with string function) checks the entered number for 1) making sure each part is between 0 and 255 (if it's 256 or more, it's going to print a message saying one of the values...

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