Google Answers Logo
View Question
 
Q: C Program - Converting a height of a person from feet and inches to centimeters ( No Answer,   4 Comments )
Question  
Subject: C Program - Converting a height of a person from feet and inches to centimeters
Category: Computers > Programming
Asked by: misterpaul7-ga
List Price: $50.00
Posted: 27 Apr 2006 20:37 PDT
Expires: 28 Apr 2006 13:15 PDT
Question ID: 723576
I am trying to write a program that will convert a height of a person
from feet and inches to centimeters. I want to use the relationship: 1
foot = 30.48 cm and 1 inch = 2.54 cm. I am trying to get the output in
a form of a table where the left column would contain the height of
the person in feet and inches from like 4'5" to 6'5" (with an
increment of 1 inch), and the right column will contain the
corresponding height in centimeters. The program will perform a
conversion with the help of a function: void Conversion (int, int);
The function has to be called from the for loop from the function main
(). Since there is no return value, each portion of information will
be printed inside the function.

Any help with this program would be much appreciated. I am new to the
C programming language.  Here is what I have so far.  Some may be
right and some may be wrong.

#include<stdio.h>

int main ()
{
int feet = 4;
int inches = 5;
int total_inches = feet + inches;
scanf("%d", total_inches);

for (total_inches = 53; total_inches < 78; total_inches++) {
++total_inches; }
}

void Conversion(int feet, int inches) {
int total_inches = 12*feet + inches;
float total_centimeters = 2.54*total_inches;
printf("%d\'%d\" %.0f cm\n", feet, inches, total_centimeters);
}

Request for Question Clarification by hammer-ga on 28 Apr 2006 06:13 PDT
MisterPaul7,

You are not actually off to a bad start here. You're heading in the
right direction. Remember that each line of code is an instruction you
want the machine to carry out. It will follow your instructions one at
a time, in order.

In pseudocode (looking at your for loop) you seem to be trying to do the following:

Main
-----
Declare an int to hold total inches.

	For one inch increments from 53 to 78 total_inches
	{
		Call Conversion passing total_inches
	}

Conversion(total_inches)
-------------------------
Declare two ints to hold feet and inches
Declare a float to hold centimenters

	Split total_inches into feet and inches
	Multiple total_inches by 2.54 to get total_centimeters
	Print out formattted feet, inches and centimeters
    

Based on the above, read your code one line at a time and see if the
instructions make sense.

For example, look at the first four lines. 
int feet = 4;
int inches = 5;
int total_inches = feet + inches;
scanf("%d", total_inches);

Do they make sense? Especially in terms of how you are using the for loop?

You are using and initializing total_inches in the for loop, so there
is no reason to initialize it to feet + inches, which doesn't make
sense anyway. There is also no reason to initalize feet and inches to
4 and 5, respectively, based on how you are doing the for loop. You
also seem to be converting back and forth several times between
total_inches and feet/inches.

It looks like you started to do this one way, then changed your mind
when you got to the for loop and took a different approach.

Get your variable declarations and your plan of attack straightened
out and try again based on the pseudocode above. If you get stuck,
post your code again. I'll help you with the next part.

- Hammer

Request for Question Clarification by hammer-ga on 28 Apr 2006 06:18 PDT
Actually, my pseudocode may be off. Looking at your code, for part of
it seems like you want the person to enter their height in total
inches, which you will then convert to centimeters. In another part
(the for loop), you appear to be trying to simply print a conversion
table for all heights from 53 to 78 inches.

If you can clarify which you are trying to do, I can probably be more helpful.

- Hammer

Clarification of Question by misterpaul7-ga on 28 Apr 2006 07:04 PDT
That is not exactly what I was looking for.  The one things I did want
to use was the function Void Conversion.  I think that it would look
something like this:

void Conversion(int feet, int inches) {
int total_inches = 12*feet + inches;
float total_centimeters = 2.54*total_inches;
printf("%d\'%d\" %.0f cm\n", feet, inches, total_centimeters);
}

I also wanted to use a for loop to print out a table of heights from
4'5" to 6'5".  I do not want the user to enter their height.  I
already know how to write a program like the one written by
"hypertwat-ga".  I am really confused with how to write a for loop to
do this.

Request for Question Clarification by hammer-ga on 28 Apr 2006 07:26 PDT
Then my pseudocode does do as you asked. If you look, it is using a
Conversion function as you requested. The Conversion function is
called inside the for loop in Main. It is detailed below Main.

Again, you are on the right track.

Your for loop is shown as:
for (total_inches = 53; total_inches < 78; total_inches++)

This means that total_inches will be set to 53. It will then do the
code inside the curly braces over and over until total_inches is no
longer less than 78, incrementing total_inches by 1 each time it
loops. This is correct.

Since you want to convert each inch value, call your Conversion
function inside the for loop.

for (total_inches = 53; total_inches < 78; total_inches++)
{
	Conversion(total_inches);
}

The Conversion function will do the work of converting, formatting and
printing out each value from 53 to 77.

I recommend converting to feet and inches inside of the Conversion
function instead of passing them in. That way, you won't have to go
between total_inches and feet/inches twice.

Again, look at the pseudocode I gave you and instruct the machine to
do those things in that order. You are already on the right track.

- Hammer

Clarification of Question by misterpaul7-ga on 28 Apr 2006 07:44 PDT
Well, this helps me out a little, now the one thing I don't understand
is the whole "Conversion Void" function and trying to get that to work
in the program.  Below is a copy of what I have so far:

#include<stdio.h>

int main ()
{
int feet = 4;
int inches = 5;
int total_inches = feet + inches;
scanf("%d", total_inches);

for (total_inches = 53; total_inches < 78; total_inches++) {

	Conversion(total_inches); }
}
void Conversion(int feet, int inches) {
int total_inches = 12*feet + inches;
float total_centimeters = 2.54*total_inches;
printf("%d\'%d\" %.0f cm\n", feet, inches, total_centimeters);
}

I get errors like: conflicting types for 'Conversion' or previous
implicit declaration of 'Conversion' was here.
Can you see what I am doing wrong with trying to get my void
Conversion function to work?  Do I have to put some type of
declaration or prototype in in the beginning of the program underneath
the #include <stdio.h>?

Request for Question Clarification by hammer-ga on 28 Apr 2006 08:15 PDT
You have a number of problems.

1. You have an extra curly brace inside the for loop after the
semicolon on the line where you call conversion.

2. You still have a bunch of unnecessary stuff at the top of Main.

3. You need to either write the Conversion function above Main or
declare it inside Main.

4. You changed the call to Conversion to pass total_inches, but you
did nothing else. Conversion is still expecting two arguments - feet
and inches. Conversion has not changed behavior to handle receiving
total inches.

And so on...

You are flailing about. :) 

Stop typing. READ YOUR CODE. Look at the pseudocode I gave you. For
each line of pseudocode, there should be something in your C that is
carrying out that instruction.

Here is the pseudo for Conversion again. Make sure your function does these things.

void Conversion(total_inches)
-------------------------
Declare two ints to hold feet and inches
Declare a float to hold centimenters

	Split total_inches into feet and inches (look into mod % and fmod)
	Multiple total_inches by 2.54 to get total_centimeters
	Print out formatted feet, inches and centimeters
----------------------------------------------------------------

- Hammer

Request for Question Clarification by hammer-ga on 28 Apr 2006 08:17 PDT
Never mind #1. Your curly braces are okay. Your "bracing style"
confused my eye and I didn't realize one was the close brace for Main
itself.

- Hammer

Request for Question Clarification by hammer-ga on 28 Apr 2006 10:21 PDT
BTW, I see that you picked up your conversion function from a question
you expired earlier. This was provided to you as a quick example of
technique. To complete your project, you need to move past what the
general example literally says and focus on what you need your
specific code do.

- Hammer

Clarification of Question by misterpaul7-ga on 28 Apr 2006 10:57 PDT
It is now just getting much more worse...I am about to throw in the
towel and quit.  This is making me more confused.  This is the code I
have as of right now, I tried different things with the code and none
of them have even come close to working right.

#include<stdio.h>

int Conversion(total_inches);

int main()
{
   int total_inches;
   scanf("%d", total_inches);

for (total_inches = 53; total_inches < 78; total_inches++)
{
        Conversion(total_inches);
}
}
void Conversion(total_inches);

int main()
{
   int feet, inches;
   float centimeters;
   int total_inches = 12*feet + inches;
   float total_centimeters = 2.54*total_inches;
   printf("%d\'%d\" %.0f cm\n", feet, inches, total_centimeters);
}

Request for Question Clarification by hammer-ga on 28 Apr 2006 12:07 PDT
OK, now you're really just flailing around. Stop. Here's your code.
Note that the Conversion function is commented with the pseudocode I
gave you to follow. Note also that the usage, declaration and
prototype of Conversion all have the same argument types, return types
and number of arguments.

Now that I've done this for you, and without taking your money, do
something for me. Rather than simply turning it in and going to a
kegger, please take the time to look at what I've done and understand
how it works. I don't mind helping people with their homework, but I
really don't like doing it for them. Please use this opportunity to
learn from the example so you don't flunk your tests, which I won't be
taking for you. *grin*

- Hammer


---------------------------------
#include <stdio.h>

int main()
{
void	Conversion(int);
int	total_inches;

	for (total_inches = 53; total_inches < 78; total_inches++)
	{
        	Conversion(total_inches);
	}
}

void Conversion(int total_inches)
{
// Declare two ints to hold feet and inches
int 	feet;
int	inches;

// Declare a float to hold centimeters
float	centimeters;

	// Split total_inches into feet and inches
	feet = total_inches / 12;
	inches = total_inches - (feet * 12);

	// Multiple total_inches by 2.54 to get total_centimeters
	centimeters = total_inches * 2.54;

	// Print out formatted feet, inches and centimeters
	printf("%d\'%d\"\t%.2f cm\n", feet, inches, centimeters);
}
---------------------------

The output looks like this:
4'5"    134.62 cm
4'6"    137.16 cm
4'7"    139.70 cm
4'8"    142.24 cm
4'9"    144.78 cm
4'10"   147.32 cm
4'11"   149.86 cm
5'0"    152.40 cm
5'1"    154.94 cm
5'2"    157.48 cm
5'3"    160.02 cm
5'4"    162.56 cm
5'5"    165.10 cm
5'6"    167.64 cm
5'7"    170.18 cm
5'8"    172.72 cm
5'9"    175.26 cm
5'10"   177.80 cm
5'11"   180.34 cm
6'0"    182.88 cm
6'1"    185.42 cm
6'2"    187.96 cm
6'3"    190.50 cm
6'4"    193.04 cm
6'5"    195.58 cm

Request for Question Clarification by hammer-ga on 28 Apr 2006 12:39 PDT
MisterPaul7,

As an extra added bonus, I've commented your original code for why
it's giving you trouble.

-------------------------

// This is fine.
#include <stdio.h>

// This says that Conversion will return int. It doesn't.
// This also says that Conversion takes one argument of
// type total_inches. There is no such type.
int Conversion(total_inches);

// This says that main will return an int. Actually it
// returns nothing. You should change this to void. You
// can change this in the code I gave you also.
int main()
{
// This line is okay.
int total_inches;

// Why are you doing this when you don't want input from the user?
scanf("%d", total_inches);

// This part is fine.
for (total_inches = 53; total_inches < 78; total_inches++)
{
        Conversion(total_inches);
}
}

// You are redeclaring this function instead of defining it
void Conversion(total_inches);

// You already wrote your main. This one should say:
// void Conversion(int total_inches)
int main()
{
// OK
int feet, inches;

// OK except you never use it
float centimeters;

// You are multiplying 12 * feet. You have not yet set
// feet to any value. Same problem with inches.
int total_inches = 12*feet + inches;

// Same problem here. You are multiplying by a value
// you don't actually have.
float total_centimeters = 2.54*total_inches;

   // This is syntactically correct, but you don't have good
   // values to print out.
   printf("%d\'%d\" %.0f cm\n", feet, inches, total_centimeters);
}

-------------------------

- Hammer
Answer  
There is no answer at this time.

Comments  
Subject: Re: C Program - Converting a height of a person from feet and inches to centimeters
From: rajeshinindia-ga on 28 Apr 2006 01:53 PDT
 
Looks like a school homework problem. Try yourself to find the solution.
Subject: Re: C Program - Converting a height of a person from feet and inches to centimeters
From: hypertwat-ga on 28 Apr 2006 06:11 PDT
 
i have done the code for you its not excacly what u wanted but unless
it is a school project it shouldn't matter ;)
Subject: Re: C Program - Converting a height of a person from feet and inches to centimeters
From: hypertwat-ga on 28 Apr 2006 06:12 PDT
 
#include<stdio.h>


int main ()

{
	float feet; /* Variable  1 */
	float inches; /* Variable  2 */
	float centimetres; /* Variable  3 */


	feet = 0; /* resetting the Variable  */
	inches = 0; /* resetting the Variable  */


	printf("enter the total height in feet "); /* ask user total height in feet */
	scanf("%f",&feet);

	printf("enter the total height in inches "); /*ask user for total
height in inches*/
	scanf("%f",&inches);

	centimetres = feet * 30.48 ; /*work out the amount in centimetres of
the feet entered*/
	centimetres = centimetres + (inches * 2.54); /*work out the amount in
centimetres of the inches entered then add to the amount of
centimeters in the feet*/
	
	printf("\n\n\n"); /*creates a clear space */
	printf("the height in feet and inches is %3.2f feet ",feet); /* print
the amount of feet the user entered */
	printf("and %3.2f inches\n",inches); /* print the amount of inches
the user entered */
	printf("the total height in centimetres is %3.2f\n\n\n",centimetres);
/* print the amount of centremetres in the feet and inches the user
entered */

}
Subject: Re: C Program - Converting a height of a person from feet and inches to centimeters
From: hypertwat-ga on 28 Apr 2006 06:13 PDT
 
ps the web browser buggered the spacing up but should be allright when
u put it in c++

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy