Write a C program that calculates and prints the average of

Write a C program that calculates and prints the average of two or more sales amounts entered by the user. The program should first prompt the user to enter the number of sales to process (make sure: at least 2 sales must be entered by user! Give an appropriate polite error message of your choice, and re-prompt) . It should then prompt the user for each sales amount. As each sales amount is entered, the program should ensure that the sales entered are greater zero. If the amount is not greater than zero, an error message should be displayed (as shown below), and the user should be re-prompted for a sales amount. Any bad sales entered should not be included in the average calculation. Once all values are entered, the program should calculate and print the average of all of the valid values entered. At the end display a message for the appropriate sales total. This program will feature the if statement as well as programming techniques for data validation. I call these trap loops. The loop traps the user into entering valid data (see online notes for details). The dialog with the user will be as follows: Welcome to the Sears Sales Analyzer. This program calculates the average of as many sales you wish to enter. First, enter the number of sales to process: 4 Now enter the 4 sales to be processed. Enter sales amount #1: 900.00 Enter sales amount #2: 800.00 Enter sales amount # 3: -200 *** Invalid entry. Sales amount must be positive. *** Enter sales amount #3: 2500.01 Enter sales amount #4: 540.00 The average of the 4 salaries entered is $1185.00 Your total sales is $4740.01. Congratulations you made the Silver Club. Note that what the user types in is indicated by the blue area above. You should substitute your name in the welcome statement above. Notice the trap in salary #3. That would repeat continuously until the user enters a correct data value. After the average is displayed, give the final statement based on the following: If sales total < $1000 display “Your sales total needs improvement.” Else If sales total < $2000 display “Your sales total is adequate.” Else If sales total < $5000 display “Congratulations you made the Silver Club.” Else If sales total >= $5000 display “Congratulations you made the Elite Gold Club.” Also, a trap loop should be used so that at least two salaries are to be entered by the user.

Solution

#include <stdio.h>

#include <stdlib.h>

int GetNumber(int i) {

int value;

while () {

printf(\"Enter sales amount #%d\",i);

scanf(\"%d\",&value);

if(value > 0 ) {

return value;

}

else {

prinf(\"Invalid input please enter above 0 value\");

}

}

}

int InputNumber() {

while () {

printf(\"Enter the number of sales to process:\");

int number;

scanf(\"%d\",&number);

if(number >=2 ) {

return number;

}

else {

prinf(\"Please enter atleast 2 sales number to calculate average.\");

}

}

}

int main() {

printf(\"Welcome to the Sears Sales Analyzer. This program calculates the average of as many sales you wish to enter.\");

int number = InputNumber();

int i=0;

int value, sum=0;

printf(\"Now enter the 4 sales to be processed.\");

for(i=1; i<=number; i++) {

printf(\"Enter sales amount #%d\",i);

scanf(\"%d\",&value);

if(value > 0) {

   sum = sum+value;

}

else {

int val = GetNumber(i);

sum = sum+ val;

}

float avg = sum/num;

}

printf(\"Average of sales is: %f\",avg);

if(avg < 1000) {

printf(\"Your sales total needs improvement\");

}

else if(avg < 2000) {

printf(\"Your sales total is adequate.\");

}

else if(avg < 5000) {

printf(\"Congratulations you made the Silver Club.\");

}

else if(avg >= 5000) {

printf(\"Congratulations you made the Elite Gold Club.\");

}

}

Write a C program that calculates and prints the average of two or more sales amounts entered by the user. The program should first prompt the user to enter the
Write a C program that calculates and prints the average of two or more sales amounts entered by the user. The program should first prompt the user to enter the
Write a C program that calculates and prints the average of two or more sales amounts entered by the user. The program should first prompt the user to enter the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site