Thread: how to re write this in C

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    how to re write this in C

    can someone put this into C please so i can use it in C++
    Code:
    var day, night, weekend : int
    var planA, planB : real
    put "Number of daytime minutes?"
    get day
    put "Number of evening minutes?"
    get night
    put "Number of weekend minutes?"
    get weekend
    if day > 100 then
        planA := (day - 100) * .25 + night * .15 + .20 * weekend
    else
        planA := night * .15 + .20 * weekend
    end if
    if day > 250 then
        planB := (day - 250) * .45 + night * .35 + .25 * weekend
    else
        planB := night * .35 + .25 * weekend
    end if
    put "Plan A costs ", planA : 0 : 2
    put "Plan B costs ", planB : 0 : 2
    
    % abs needed because turing stores reals with a potential small error
    if abs (planA - planB) < 0.00005 then
        put "Plan A and B are the same price."
    elsif planA > planB then
        put "Plan B is the cheapest."
    else
        put "Plan A is the cheapest."
    end if
    Last edited by Salem; 02-22-2011 at 12:58 PM. Reason: Fixed code tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why can't you do it?

    Or at least, why can't you try to do it?
    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.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Dare i to make the assumption it's BASIC?

    EDIT: Maybe pseudo code...
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    i only know C and i need this in C but havent the slightest clue on how to do it could i please gets ome help

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What do you mean you don't have a clue.

    If you have no idea that replacing put with printf is probably a good start, then you should probably give up trying to be a programmer.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      var day, night, weekend : int
    var planA, planB : real
    printf ("Number of daytime minutes?");
    get day
    printf ("Number of evening minutes?");
    get night
    printf ("Number of weekend minutes?");
    get weekend
    if day > 100 then
        planA := (day - 100) * .25 + night * .15 + .20 * weekend
    else
        planA := night * .15 + .20 * weekend
    end if
    if day > 250 then
        planB := (day - 250) * .45 + night * .35 + .25 * weekend
    else
        planB := night * .35 + .25 * weekend
    end if
    printf ("Plan A costs ", planA : 0 : 2);
    printf ("Plan B costs ", planB : 0 : 2);
    
    % abs needed because turing stores reals with a potential small error
    if abs (planA - planB) < 0.00005 then
        put "Plan A and B are the same price."
    elsif planA > planB then
        put "Plan B is the cheapest."
    else
        put "Plan A is the cheapest."
    end if
    
    
    
      system("PAUSE");	
      return 0;
    }
    this is what i have gotten can you sort of walk me through it please ive only been programming for a week

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by jeromey55 View Post
    can someone put this into C please so i can use it in C++
    What about putting this into C++ so you can use it in C?

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    i did but it dosent compile

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by jeromey55 View Post
    i did but it dosent compile
    This one deserves a :lol: !
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    how?

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    If you knew C as you claim to, you'd know how to do it.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    i know a weeks worth which isnt much

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, ok. Guess i can help a little. Here goes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[])
    {
        int day, night, weekend;
        double planA, planB;
        
        printf ("Number of daytime minutes?\n");
        scanf("%d", &day);
        printf ("Number of evening minutes?\n");
        scanf("%d", &night);
        printf ("Number of weekend minutes?\n");
        scanf("%d", &weekend);
    
        if (day > 100)
           planA = (day - 100)*0.25 + night*0.15 + 0.2*weekend;
        else
           planA = night*0.15 + 0.2*weekend;
    
        if (day > 250)
           planB = (day - 250)*0.45 + night*0.35 + 0.25*weekend;
        else
           planB = night*0.35 + 0.25*weekend;
    
        printf ("Plan A costs %0.2f\n", planA);
        printf ("Plan B costs %0.2f\n", planB);
    
        /* abs needed because turing stores reals with a potential small error */
        if (abs (planA - planB) < 0.00005)
           printf("Plan A and B are the same price.\n");
        else if (planA > planB)
           printf("Plan B is the cheapest.\n");
        else
           printf("Plan A is the cheapest.\n");
    
        system("PAUSE");	
        return 0;
    }
    There! Happy?
    Devoted my life to programming...

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There are still plans like this? $0.25/min talk, $0.20 per text anytime, pay as you go - or pay $60/mo unlimited talk and text. Buy those.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is the C++ forum, not the C forum, ya know...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  2. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 06:00 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM
  5. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM