Analyze the below code. Find the errors in this program and fix them (note: add your comments for explanation). #include int main(void) { #include //Header files //declare variables that are used in program float cost1, cost2, cost3; float total; //asking from user to enter three items prices printf("Enter costs if three items you want to buy:"); //entered prices are stored in variables scanf("%f %f %f",&cost1,&cost2,cost3); //formula of total sum is used. total=(cost1+cost2+cost3) if(total>160); //if condition is true then display following output { printf("You're exceeding amount of money you have"); } else if(total=160) { //if condition is true then display following output printf("You have exactly the same amount of money you need"); } else if(total<160 & total>0) { //if condition is true then display following output printf("You will have a change after you buy these 3 items"); } else { //in the remaining cases display following output printf("Something wrong with calculations"); } } · Insert a copy of the correct code and explain the errors that you found. · Insert a clear screenshot of your output with your name in the black output console.
Analyze the below code. Find the errors in this program and fix them (note: add your comments for explanation).
#include <stdio.h>
int main(void) { #include<stdio.h> //Header files
//declare variables that are used in program float cost1, cost2, cost3; float total;
//asking from user to enter three items prices printf("Enter costs if three items you want to buy:"); //entered prices are stored in variables scanf("%f %f %f",&cost1,&cost2,cost3); //formula of total sum is used. total=(cost1+cost2+cost3)
if(total>160); //if condition is true then display following output { printf("You're exceeding amount of money you have"); } else if(total=160) { //if condition is true then display following output printf("You have exactly the same amount of money you need"); } else if(total<160 & total>0) { //if condition is true then display following output printf("You will have a change after you buy these 3 items"); } else { //in the remaining cases display following output printf("Something wrong with calculations"); } } |
· Insert a copy of the correct code and explain the errors that you found.
|
· Insert a clear screenshot of your output with your name in the black output console.
my name is Ali Faisal
In this step, we will identify the errors . I have added comment and made numbering of errors.
#include<stdio.h>
int main(void) {
#include<stdio.h> //Header files can't be written inside main, error1.
//declare variables that are used in program
float cost1, cost2, cost3;
float total;
//asking from user to enter three items prices
printf("Enter costs if three items you want to buy:");
//entered prices are stored in variables
scanf("%f %f %f",&cost1,&cost2,cost3); //before cost3 & sign is missing , error2
//formula of total sum is used.
total=(cost1+cost2+cost3) //semicolon is missing here, error3
if(total>160); //after if condition , semicolon is incorrect, error4
//if condition is true then display following output
{ printf("You're exceeding amount of money you have"); }
else if(total=160) //for comparison == operator is used, error5
{
//if condition is true then display following output
printf("You have exactly the same amount of money you need");
}
else if(total<160 & total>0) // for multiple condition && operator is used , error6
{
//if condition is true then display following output
printf("You will have a change after you buy these 3 items");
}
else
{
//in the remaining cases display following output
printf("Something wrong with calculations");
}
}
Step by step
Solved in 3 steps with 1 images