Thread: C programming question

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    2

    Post 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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what is your code, your test input, expected output, and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    2
    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;    
    }
    Last edited by kamuna69; 05-20-2021 at 03:10 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Question
    By Buddha in forum C Programming
    Replies: 11
    Last Post: 05-17-2010, 03:02 PM
  2. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM
  3. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  4. Web Programming Question
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-24-2002, 04:28 AM

Tags for this Thread