-
C programming question
Can someone help me in this question? I already have the code but the given output seems wrong or my code is wrong?
The New Telephone Company has the following rate structure for long-distance calls:
a. The regular rate for a call is P8 per minute.
b. Any call started after 6:00P.M. (1800 hours) but before
8:00 A.M (0800 hours) is discounted 50%.
c. Any call longer than 60 minutes receives a 15% discount.
d. All calls are subject to 4% federal tax on the cost after any discounts.
Write a program that takes the start time for a call based on a 24-hour clock and the length of the call in minutes. The gross cost (before any discounts or tax) should be displayed followed by the net cost (after discounts are deducted and tax is added).
Input:
Input the start time of the call: 1800
Input the length of call in minutes : 61
Output :
Gross cost : 488.00
Net cost : 430.56
-
So, what is your code, your test input, expected output, and actual output?
-
this is the code ive been working
Input:
Input the start time of the call: 1800
Input the length of call in minutes : 61
Output :
Gross cost : 488.00
Net cost : 430.56
that should be the output but my code outputs this
Output :
Gross cost : 488.00
Net cost : 431.39
Code:
#include<stdio.h>#include<conio.h>
int main()
{
int time,min;
float priceperminute=8, discount=0, grosscost=0, netcost=0;
printf("Input the start time of the call:");
scanf("%d", &time);
printf("Input the length of the call in minutes:");
scanf("%d", &min);
grosscost=min*priceperminute;
if(time>=800 && time<=1800)
{
netcost=grosscost;
}
else
netcost=grosscost*0.5;
if (min>=60)
netcost-= netcost*0.15;
float tax= netcost*4/100;
netcost=netcost+tax;
printf("Grosscost:%.2f",grosscost);
printf("\nNetcost:%.2f", netcost);
return 0;
}
-
Have you tried to work it out by hand?
I ran the code through the debugger one line at a time.
The individual values of netcost, discount and tax all seem fine.
If you want a different answer, it needs a different calculation.
Code:
$ gcc -g -Wall foo.c
foo.c: In function ‘main’:
foo.c:6:29: warning: unused variable ‘discount’ [-Wunused-variable]
float priceperminute = 8, discount = 0, grosscost = 0, netcost = 0;
^
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b main
Breakpoint 1 at 0x4005fe: file foo.c, line 4.
(gdb) run
Starting program: ./a.out
Breakpoint 1, main () at foo.c:4
4 {
(gdb) n
6 float priceperminute = 8, discount = 0, grosscost = 0, netcost = 0;
(gdb)
8 printf("Input the start time of the call:");
(gdb)
9 scanf("%d", &time);
(gdb)
Input the start time of the call:1800
10 printf("Input the length of the call in minutes:");
(gdb)
11 scanf("%d", &min);
(gdb)
Input the length of the call in minutes:61
13 grosscost = min * priceperminute;
(gdb)
15 if (time >= 800 && time <= 1800) {
(gdb) p grosscost
$1 = 488
(gdb) n
16 netcost = grosscost;
(gdb)
20 if (min >= 60)
(gdb)
21 netcost -= netcost * 0.15;
(gdb)
23 float tax = netcost * 4.0 / 100;
(gdb) p netcost
$2 = 414.799988
(gdb) n
24 netcost = netcost + tax;
(gdb) p tax
$3 = 16.5919991
(gdb) n
26 printf("Grosscost:%.2f", grosscost);
(gdb) p grosscost
$4 = 488
(gdb) n
27 printf("\nNetcost:%.2f", netcost);
(gdb) p netcost
$5 = 431.391998
(gdb)
-
I just compiled it up.
What you have been show as the correct answer is 414.00 plus 4% tax, and the answer your program gives is 414.80 plus 4% tax.
So it seems the assignment setter's code calculated 488 - 488*0.15 to (remove the 15% discount) then somehow truncated the intermediate result from 414.80 to 414 before adding tax.