how to re write this in C

This is a discussion on how to re write this in C within the C++ Programming forums, part of the General Programming Boards category; can someone put this into C please so i can use it in C++ Code: var day, night, weekend : ...

  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 11:58 AM. Reason: Fixed code tags

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    671
    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?
    I never put signature, but I decided to make an exception.

  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
    1,602
    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
    1,602
    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
    1,602
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 10: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, 03:00 PM
  5. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21