Question

In: Computer Science

#include <stdio.h> int main() { float sum_salary=0,sum_raise=0,sum_newsalary=0; while(1) { float salary,rate,raise,newsalary; printf("Enter Salary :"); scanf("%f",&salary); if(salary==-1)...

#include <stdio.h>

int main()
{
float sum_salary=0,sum_raise=0,sum_newsalary=0;
while(1)
{
float salary,rate,raise,newsalary;
printf("Enter Salary :");
scanf("%f",&salary);
if(salary==-1)
{
if(sum_salary==0)
return 0;
else
{
printf("Total Salary :%f Total raise :%f Total New Salary: %f",sum_salary,sum_raise,sum_newsalary);
return 0;
}

}
else
{
if(salary>=0 &&salary<30000)
rate=7;
else if(salary>=30000 &&salary<=40000)
rate=5.5;
else if(salary>40000)
rate=4;
raise=rate*salary/100;
newsalary=salary+raise;
  
sum_salary=salary+sum_salary;
sum_raise=raise+sum_raise;
sum_newsalary=newsalary+sum_newsalary;
printf("Salary :%f rate :%f Raise :%f New Salary: %f\n",salary,rate,raise,newsalary);

}
}
}

Hi can you please make sure that the following program will ask the user the following question before the salaries are entered? I am not sure where I would have to add this in for it ask that. If you can fit this requirement in the program that will be greatly appreciated. Thank you

Here is the question that the program should ask, "How many salaries do you want to enter?"

Solutions

Expert Solution

Source code of the program after changes made and its explanation are given below.Changes are shown in bold case.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Changes made

  • Integer variable n is declared to hold number of salaries.
  • Loop control variable i is declared and initialized with 1
  • Prompt to enter number of salaries and scanf() statement to read the entered value is place just before while loop
  • Since the number of salaries going to enter is pre known,the condition of while loop is changed into i<=n.Loop will executes exactly n times.
  • Since the number of salaries going to enter is pre known,no need to enter -1 to stop user input.
  • So If statement to check salary is -1 and its else part are eliminated and the statement to display final results are placed after the while loop
  • statement to increment i by 1 is included inside loop body

Program as per the requirement

#include <stdio.h>
int main()
{
int n,i=1;
float sum_salary=0,sum_raise=0,sum_newsalary=0;
printf("How many salaries do you want to enter? ");
scanf("%d",&n);
//while loop executes as long as i less than or equal to n
while(i<=n)

{
float salary,rate,raise,newsalary;
printf("Enter Salary %d: ",i);
scanf("%f",&salary);
if(salary>=0 &&salary<30000)
rate=7;
else if(salary>=30000 &&salary<=40000)
rate=5.5;
else if(salary>40000)
rate=4;
raise=rate*salary/100;
newsalary=salary+raise;
  
sum_salary=salary+sum_salary;
sum_raise=raise+sum_raise;
sum_newsalary=newsalary+sum_newsalary;
printf("Salary :%f rate :%f Raise :%f New Salary: %f\n",salary,rate,raise,newsalary);
//updating i
i++;
}
printf("Total Salary :%f Total raise :%f Total New Salary: %f",sum_salary,sum_raise,sum_newsalary);
return 0;

}

Screen shot of the code

Screen shot of the output


Related Solutions

#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT