Hey guys, the code I\'m working on is giving me the incorrect output and I can\'
ID: 3631399 • Letter: H
Question
Hey guys, the code I'm working on is giving me the incorrect output and I can't seem to figure out where I'm messing up.. below is the actual assignment followed by the code Im working on._________________________________________________________________________
Write a C program that calculates and prints the average of several salaries entered by the user. The program should first prompt the user to enter the number of salaries to process (make sure: at least 2 salaries must be entered by user! Give an appropriate polite error message of your choice.) . It should then prompt the user for each salary. As each salary is entered, the program should ensure that the salaries entered are in the salary range of zero and above. If the salary is not in that range, an error message should be displayed (as shown below), and the user should be re-prompted for a salary. Any bad salaries 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 positive values entered, rounded off to the nearest whole cent. At the end display a message for the appropriate message outlined below. This program will feature and require the if statement as well as programming techniques for data validation. I call it a trap loop. It traps the user into entering valid data.
The dialog with the user could be as follows:
Welcome to the Sears Salary Analyzer.
This program calculates the average of as many salaries you wish to enter.
First, enter the number of salaries to process: 4
Now enter the 4 salaries to be averaged.
Enter salary #1: 900
Enter salary #2: 800
Enter salary # 3: -200
*** Invalid entry. Salary must be positive. ***
Enter salary #3: 2500
Enter salary #4: 540
The average of the 4 salaries entered is $1185.00
Your salaries are very generous.
###################################################################
#include <stdio.h>
main ()
{
int numSalaries=0, counter=0, salary[100], salarySum=0, averageSalary=0;
printf ("Enter the number of salaries to process: ");
scanf ("%i", &numSalaries);
while (numSalaries<2)
{
printf (" You must use at least two salaries. ");
printf ("Enter the number of salaries to process: ");
scanf ("%i", &numSalaries);
}
printf (" Now enter the %i salaries to be averaged ", numSalaries);
while (numSalaries>2 && counter<numSalaries)
{
counter++;
printf(" Enter Salary #%i: ", counter);
scanf ("%i", &salary[counter]);
salarySum=salary[counter]+salarySum;
if (salary[counter]<0)
{
printf (" *** Invalid entry. Salary must be positive. *** ");
printf("Enter Salary #%i: ", counter);
scanf ("%i", &salary[counter]);
}
}
averageSalary=salarySum/counter;
printf (" The average of the %i salaries entered is $%i.00 ", numSalaries, averageSalary);
if (averageSalary<1000)
printf ("Your salaries are low, how about a raise.");
if (averageSalary>=1000 && averageSalary<2000)
printf ("Your salaries are very generous.");
if (averageSalary>2000)
printf ("Your salaries are very high.");
printf (" ");
system ("pause");
}
Explanation / Answer
//changes shown in red colour ...
// now works perfectly.
#include <stdio.h>
main ()
{
int numSalaries=0, counter=0, salary[100];salarySum=0, averageSalary=0;
printf ("Enter the number of salaries to process: ");
scanf ("%i", &numSalaries);
while (numSalaries<2)
{
printf (" You must use at least two salaries. ");
printf ("Enter the number of salaries to process: ");
scanf ("%i", &numSalaries);
}
printf (" Now enter the %i salaries to be averaged ", numSalaries);
while (numSalaries>2 && counter<=numSalaries)
{
printf(" Enter Salary #%i: ", (counter+1));
scanf ("%i", &salary[counter]);
if (salary[counter]<0)
{
printf (" *** Invalid entry. Salary must be positive. *** ");
printf("Enter Salary #%i: ", (counter+1));
scanf ("%i", &salary[counter]);
}
counter++;
salarySum=salary[counter]+salarySum;
}
averageSalary=(float)salarySum/(float)counter;
printf (" The average of the %i salaries entered is $%i.00 ", numSalaries, averageSalary);
if (averageSalary<1000)
printf ("Your salaries are low, how about a raise.");
if (averageSalary>=1000 && averageSalary<2000)
printf ("Your salaries are very generous.");
if (averageSalary>2000)
printf ("Your salaries are very high.");
printf (" ");
system ("pause");
}