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...
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 <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); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT