Reputation: 49
This a question I have to submit for an assignment, hence it has to be evaluated online. My program is running correctly for 6 out of 7 test cases. Only 3 test cases are provided and they are as shown:
Sports or Economy Car
Help Mr.Kamath to check whether his vehicle is an economy car or not. The program should display “There is a gas hog” if the mileage is less than or equal to 15 Km and the program should display “It is an economy car” if the mileage is not less than 30 Km. Otherwise display "Fuel Economy". Write a C program to get the values of a Car variables from the user. Create a structure called Car. Order of the input values entered should be according to the structure variables
struct Car{
float startKm;
float endKm;
float litres;
};
Test Case Input 1
30
50
5
Output 1
There is a gas hog
Input 2
40.5
80.5
1.5
Output 2
Fuel Economy
Input 3
30
0
5
Output 3
You have entered 0
My code:
#include<stdio.h>
struct Car
{
float startKm;
float endKm;
float litres;
};
int main()
{
struct Car c;
float m;
scanf("%f",&c.startKm);
scanf("%f",&c.endKm);
scanf("%f",&c.litres);
m=(c.endKm-c.startKm)/c.litres;
if(c.startKm<=0||c.endKm<=0||c.litres<=0)
{
printf("You have entered 0"); return 0;
}
else if(m<=15)
{
printf("There is a gas hog");
}
else if(m>=30)
{
printf("It is an economy car");
}
else
{
printf("Fuel Economy");
}
return 0;
}
These are the test cases(unknown):
These is my evaluation output:
PS: I am facing similar problems in many such programs with several test cases.
I also asked a similar question Cannot identify error with code, failing a test case
It would be helpful if someone suggests how to approach such unknown test cases.
Upvotes: 0
Views: 466
Reputation: 304
If I'm not mistaken you should be able to start from 0 km. I went ahead and tried my own test case: Starting km 0, Ending km 25, Liters used 1
if(c.startKm < 0 || c.endKm <= c.startKm || c.litres <= 0)
Upvotes: 1