C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 09-08-2009, 11:56 PM   #1
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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 12:05 AM.
rickajoie is offline  
Old 09-09-2009, 12:18 AM   #2
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848

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++)?
nadroj is offline  
Old 09-09-2009, 12:18 AM   #3
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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..
rickajoie is offline  
Old 09-09-2009, 12:23 AM   #4
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
Quote:
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.
nadroj is offline  
Old 09-09-2009, 12:31 AM   #5
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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 12:43 AM.
rickajoie is offline  
Old 09-09-2009, 01:36 AM   #6
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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 01:07 PM.
Adak is offline  
Old 09-09-2009, 02:01 AM   #7
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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 02:22 AM.
rickajoie is offline  
Old 09-09-2009, 02:45 AM   #8
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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.
Adak is offline  
Old 09-09-2009, 02:51 AM   #9
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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...
rickajoie is offline  
Old 09-09-2009, 06:23 AM   #10
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 731
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?
rags_to_riches is offline  
Old 09-09-2009, 08:51 AM   #11
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,929
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.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline  
Old 09-10-2009, 05:07 AM   #12
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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...>.<
rickajoie is offline  
Old 09-10-2009, 05:07 AM   #13
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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??
rickajoie is offline  
Old 09-10-2009, 06:12 AM   #14
Registered User
 
Join Date: Oct 2006
Posts: 263
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.
Elkvis is offline  
Old 09-10-2009, 06:30 AM   #15
*depressed*
 
Join Date: Sep 2009
Location: Philippines
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 06:32 AM.
rickajoie is offline  
Closed Thread

Tags
program, while statement

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 09:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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