Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-08-2009
rickajoie
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.

• 09-09-2009

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++)?
• 09-09-2009
rickajoie
Quote:

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..
• 09-09-2009
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.
• 09-09-2009
rickajoie
Quote:

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
• 09-09-2009
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. :)
• 09-09-2009
rickajoie
Quote:

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?
• 09-09-2009
Quote:

Originally Posted by rickajoie
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.
• 09-09-2009
rickajoie
Quote:

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...
• 09-09-2009
rags_to_riches
Quote:

Originally Posted by rickajoie
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?
• 09-09-2009
abachler
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.
• 09-10-2009
rickajoie
Quote:

Originally Posted by rags_to_riches
You're more than one person? Are you a conjoined twin?

conjoined twin?? no im not...>.<
• 09-10-2009
rickajoie
Quote:

Originally Posted by abachler
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.

• 09-10-2009
Elkvis
Quote:

Originally Posted by rickajoie

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.
• 09-10-2009
rickajoie
Quote:

Originally Posted by Elkvis
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...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last