I need help please...

This is a discussion on I need help please... within the C++ Programming forums, part of the General Programming Boards category; I have trouble doing this. I try making it but it wont work. This is in while loop. 1. Write ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Unhappy I need help please...

    I have trouble doing this.
    I try making it but it wont work.
    This is in while loop.

    1. Write a C program that converts gallons to liters. The program should display gallons from 10 to 20 in 1-gallon increments and the corresponding liter equivalents. Use the relationship that 1 gallon contains 3.785 liters.

    2. Write a C program that converts feet to meters. The program should display feet from 3 to 30 in 1-foot increments and the corresponding meter equivalents. Use the relationship that 1 meter equivalent to 3.728 feet.

    please help me...im still a newbie...and still new to this...
    Last edited by rickajoie; 09-09-2009 at 01:05 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243

    WHAT HAVE YOU TRIED?!


    hopefully that stands out. here is an example of counting from -42 to 42:

    Code:
    int start = -42;
    int end = 42;
    
    while ( start <= end )
    {
       cout << start << endl;
       start++;
    }
    this is basically giving you the answer, apart from the logic you must add to suit your requirements. i.e., what number do you have to start at? end at? inside the loop you print something (cout)? inside the loop you do some calculation (start++)?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by nadroj View Post

    WHAT HAVE YOU TRIED?!


    hopefully that stands out. here is an example of counting from -42 to 42:

    Code:
    int start = -42;
    int end = 42;
    
    while ( start <= end )
    {
       cout << start << endl;
       start--;
    }
    this is basically giving you the answer, apart from the logic you must add to suit your requirements.
    but how do i do it in converting the questions...its confusing..

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    1. Write a C program that converts gallons to liters. The program should display gallons from 10 to 20 in 1-gallon increments and the corresponding liter equivalents. Use the relationship that 1 gallon contains 3.785 liters.
    ok so i gave you an example. your program must start at 10, and end at 20 (inclusive). directly from your pasted assignment, it says that 1g = 3.785l. therefore 10g = 10*3.785l, 15g = 15*3.785g, etc. inside your loop you need to convert your number of gallons (starting at 10) to litres and print it.

    give that a shot or post actual work/attemps or ask what you dont understand more specifically.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by nadroj View Post
    ok so i gave you an example. your program must start at 10, and end at 20 (inclusive). directly from your pasted assignment, it says that 1g = 3.785l. therefore 10g = 10*3.785l, 15g = 15*3.785g, etc. inside your loop you need to convert your number of gallons (starting at 10) to litres and print it.

    give that a shot or post actual work/attemps or ask what you dont understand more specifically.
    is this right?



    int start = 10;
    int end = 20;
    liters = (3.785);

    printf("Input gallons from 10 to 20:\n");

    while ( start <= end )
    {
    10; start = 10 * 3.785;
    start++;

    and the problem now too is converting int to double
    Last edited by rickajoie; 09-09-2009 at 01:43 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
        int start = 10;
        int end = 20;
        const double litersPerGallon = 3.785;  //edit: not constant, const 
        double liters = 0.0;
    
        printf("gallons to liters, from 10 to 20:\n");
    
        while ( start <= end )
        {
            liters = start * litersPerGallon;
            printf("\n %d gallons is %4.3lf liters", start, liters);        
            start++;
        }
    This is a bit of a gift, but take it and use a similar logic, to do the second part of your assignment.

    You need to "marinate" in this kind of logic, because it's quite basic, and used all the time. Let it soak in, very well.
    Last edited by Adak; 09-09-2009 at 02:07 PM.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by Adak View Post
    Code:
        int start = 10;
        int end = 20;
        constant double litersPerGallon = 3.785;
        double liters = 0.0;
    
        printf("gallons to liters, from 10 to 20:\n");
    
        while ( start <= end )
        {
            liters = start * litersPerGallon;
            printf("\n %d gallons is %4.3lf liters", start, liters);        
            start++;
        }
    This is a bit of a gift, but take it and use a similar logic, to do the second part of your assignment.

    You need to "marinate" in this kind of logic, because it's quite basic, and used all the time. Let it soak in, very well.
    is the constant included?
    ok thank you very much~ thank so much~
    can i ask? what is 4.31f for? how did you get it?
    Last edited by rickajoie; 09-09-2009 at 03:22 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rickajoie View Post
    is the constant included?
    ok thank you very much~ thank so much~
    can i ask? what is 4.31f for? how did you get it?
    The constant data type modifier, just tells your compiler "this variable should never be changed by the program". It's optional, but good to use, although I frequently forget to use it, myself.

    %4 in printf(), tells printf() that this variable number should be printed in a field that is 4 digits wide. The .3 tells printf() that this variable should have 3 digits printed, after the decimal point.

    "lf" tells printf() that the data type here, is a double (think of it as a long float).

    The joy and the curse of C (and programming in general), is that you have to keep practicing with it, or you'll forget it. If you only have a year or so of programming in C, and you step away from it for 3 years, you'll be *amazed* at all the things that you've forgotten.

    You're welcome.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Talking

    Quote Originally Posted by Adak View Post
    The constant data type modifier, just tells your compiler "this variable should never be changed by the program". It's optional, but good to use, although I frequently forget to use it, myself.

    %4 in printf(), tells printf() that this variable number should be printed in a field that is 4 digits wide. The .3 tells printf() that this variable should have 3 digits printed, after the decimal point.

    "lf" tells printf() that the data type here, is a double (think of it as a long float).

    The joy and the curse of C (and programming in general), is that you have to keep practicing with it, or you'll forget it. If you only have a year or so of programming in C, and you step away from it for 3 years, you'll be *amazed* at all the things that you've forgotten.

    You're welcome.
    ok thank you~ ehehe~ actually im just new in programming im still a freshmen...

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,656
    Quote Originally Posted by rickajoie View Post
    ok thank you~ ehehe~ actually im just new in programming im still a freshmen...
    You're more than one person? Are you a conjoined twin?

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    I think we need to revise our homework policy and start doing these peoples homework for them, so when they get out in the world they utterly fail right out of the gate and never plague us with their inane code.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by rags_to_riches View Post
    You're more than one person? Are you a conjoined twin?
    conjoined twin?? no im not...>.<

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by abachler View Post
    I think we need to revise our homework policy and start doing these peoples homework for them, so when they get out in the world they utterly fail right out of the gate and never plague us with their inane code.
    uhhh...are you mad for me asking help??

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by rickajoie View Post
    uhhh...are you mad for me asking help??
    not so much mad as annoyed.

    it's pretty obvious that you did not read the forum rules before posting. you basically came on here and asked us to write your code for you without showing any evidence that you had already tried to do it yourself or search for answers from any of the MANY sources of information on this subject.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by Elkvis View Post
    not so much mad as annoyed.

    it's pretty obvious that you did not read the forum rules before posting. you basically came on here and asked us to write your code for you without showing any evidence that you had already tried to do it yourself or search for answers from any of the MANY sources of information on this subject.
    i didn't ask to do it for me...i just asked help on how to do it...on what way cus i was confused...and i was doing it over and over again for days and i keep on scanning my book every time...and then someone was kind enough to just give me the answer...please dont get me wrong for my intentions that you have misunderstood...did you not even try reading the previous replies...to tell you the truth i am seriously hurt by what you have said...
    Last edited by rickajoie; 09-10-2009 at 07:32 AM.

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

Tags for this Thread


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