Calculating BMI with C Lang: A Guide

  • Thread starter Marcin H
  • Start date
  • Tags
    Lang
In summary: Your problem is truncation as I see it. So, take a careful look to your code for some typo.What is...
  • #1
Marcin H
306
6

Homework Statement



Write a few lines of code that will accomplish the following:

  1. Ask the user to first enter his/her weight (in kilograms) and second enter the height (in meters). For every field to be filled in, print on screen the messages "Type your weight: " and "Type your height: " respectively.
  2. Print on screen on a different line the message "Your BMI is: " and the BMI value of the user according to the following formula:
BMI= weight in kg/(height in m)^2

Note: for this problem, there is no need to write any #include preprocessor directives in the solution

Hint: the formula is easy to implement if you remember the definition of power of a number

Homework Equations


C language

The Attempt at a Solution


I tried running this program and everything works fine except the last step where it should do the calculation and output the BMI. I am not sure what I did wrong. This is my first time programming with C, so I am sorry if this is a dumb/easy mistake, but I am not sure what I am missing. Why is the program not outputting an answer? Using an online compiler I get this as a result:

sh-4.3$ main
Type your weight in kilograms:87
Type your height in meters:1.4
Your BMI is 44sh-4.3$

Here is my code:

Code:
#include <stdio.h>

int main()
{
    float weight, height, BMI;
   
    printf("Type your weight in kilograms:");
    scanf("%f", &weight);
   
    printf("Type your height in meters:");
    scanf("%f", &height);
   
    BMI = weight / (height*height);
   
    printf("Your BMI is %.f",BMI);

    return 0;
}
 
Physics news on Phys.org
  • #2
Marcin H said:
sh-4.3$ main
Type your weight in kilograms:87
Type your height in meters:1.4
Your BMI is 44sh-4.3$
What is that first line? Is it you invoking your program?
 
  • #3
Marcin H said:

Homework Statement



Write a few lines of code that will accomplish the following:

  1. Ask the user to first enter his/her weight (in kilograms) and second enter the height (in meters). For every field to be filled in, print on screen the messages "Type your weight: " and "Type your height: " respectively.
  2. Print on screen on a different line the message "Your BMI is: " and the BMI value of the user according to the following formula:
BMI= weight in kg/(height in m)^2

Note: for this problem, there is no need to write any #include preprocessor directives in the solution

Hint: the formula is easy to implement if you remember the definition of power of a number

Homework Equations


C language

The Attempt at a Solution


I tried running this program and everything works fine except the last step where it should do the calculation and output the BMI. I am not sure what I did wrong. This is my first time programming with C, so I am sorry if this is a dumb/easy mistake, but I am not sure what I am missing. Why is the program not outputting an answer? Using an online compiler I get this as a result:

sh-4.3$ main
Type your weight in kilograms:87
Type your height in meters:1.4
Your BMI is 44sh-4.3$

Here is my code:

Your problem is truncation as I see it. So, take a careful look to your code for some typo.
 
  • #4
berkeman said:
What is that first line? Is it you invoking your program?
I am not sure what that is. That is what the online compiler spits out when I try to execute
 
  • #5
QuantumQuest said:
Your problem is truncation as I see it. So, take a careful look to your code for some typo.
Which line exactly? I am not sure where I made the mistake. I the last printf line to this:

Code:
    printf("Your BMI is",BMI);

    return 0;
}

Someone told me that should fix my problem, but it did not.
 
  • #6
Marcin H said:
I am not sure what that is. That is what the online compiler spits out when I try to execute
I think it's just the Unix shell "sh" running the generated executable, named "main". It should not matter for your program.

As far as I can see, you are almost there. Try

Code:
printf("Your BMI is %.2f\n",BMI);

This prints the floating point variable BMI accurate to two decimal places and end the output with a newline. (Have a look at formatting specifiers for the "printf" function.)

What puzzles me more is the comment in the assignment about not needing #include directives. I do not see how you can do IO without them.
 
  • Like
Likes Marcin H
  • #7
Marcin H said:
Which line exactly? I am not sure where I made the mistake. I the last printf line to this:

First, it is absolutely necessary as berkeman asks you to understand what is this sh-4.3$. On what operating system are you trying to run your program? You have to fully understand such things, in order to run your programs smoothly.

For about the exact line that the typo lies, it will do no good to you and a violation of PF rules, to tell you exactly. My hint, with little thought, will lead to the appropriate line. Think about what your output misses and where the cause would be.
 
Last edited:
  • #8
QuantumQuest said:
For about the exact line that the typo lies, it will do no good to you and a violation of PF rules, to tell you exactly. My hint, with little thought, will lead to the appropriate line. Think about what your output misses and where the cause would be.
In view of what is mentioned in the OP about not needing #includes, could it be that the real crux of the problem is to write this without using IO library functions? I wouldn't know how to do that out of the top of my head, but most likely the solution will be quite low level and dependent on the system that runs the compiler.
 
  • #9
Krylov said:
I think it's just the Unix shell "sh" running the generated executable, named "main". It should not matter for your program.

As far as I can see, you are almost there. Try

Code:
printf("Your BMI is %.2f\n",BMI);

This prints the floating point variable BMI accurate to two decimal places and end the output with a newline. (Have a look at formatting specifiers for the "printf" function.)

What puzzles me more is the comment in the assignment about not needing #include directives. I do not see how you can do IO without them.
Krylov said:
I think it's just the Unix shell "sh" running the generated executable, named "main". It should not matter for your program.

As far as I can see, you are almost there. Try

Code:
printf("Your BMI is %.2f\n",BMI);

This prints the floating point variable BMI accurate to two decimal places and end the output with a newline. (Have a look at formatting specifiers for the "printf" function.)

What puzzles me more is the comment in the assignment about not needing #include directives. I do not see how you can do IO without them.
Yeah I am not sure about that hint either. Doesn't <stdio.h> contain all the functions like print and scan and others?

Also, can you explain why this does not work:

Code:
printf("Your BMI is" , BMI);

I was told that this should work and I have not learned what you wrote in that one bit. Do you always need to add %.2f\n after trying to print out a result? Is it the newline that makes the program work or the %.2f? Also if you want more accuracy how much more can you get using %.f? Will %.f just give you an integer? Will %.9 give you 9 digits of accuracy? Also why can't you just use %f to say that BMI is just a floating point and make it print as many decimals as a floating point can?
 
  • #10
Krylov said:
Could it be that the real crux of the problem is to write this without using IO library functions? I wouldn't know how to do that out of the top of my head, but most likely the solution will be quite low level and dependent on the system that runs the compiler.

Absolutely right. Because of this complexity and accepting the code that OP shows and if being accepted in this way, I just try to hint OP to make it run. Then, he/she can search the more difficult thing.
 
  • Like
Likes S.G. Janssens
  • #11
QuantumQuest said:
Absolutely right. Because of this complexity and accepting the code that OP shows and if being accepted in this way, I just try to hint OP to make it run. Then, he/she can search the more difficult thing.
This code won't work if you don't include #include <stdio.h> because then you won't be able to use printf or scanf, which is something really important that we learned. All my examples use those 2 functions so I have to use them here. Also I am using THIS just to test if the program works or not.
 
  • #12
Marcin H said:
This code won't work if you don't include #include <stdio.h> because then you won't be able to use printf or scanf, which is something really important that we learned. All my examples use those 2 functions so I have to use them here. Also I am using THIS just to test if the program works or not.

Yes, I know that this is the usual way of doing things. So, if there is no problem with your
Code:
#include
according to the Note under the problem statement, the problem is what I pointed out in #3. A little careful inspection line-by-line in conjunction with my hint, will give immediately the solution.
 
  • #13
Marcin H said:
I was told that this should work and I have not learned what you wrote in that one bit. Do you always need to add %.2f\n after trying to print out a result? Is it the newline that makes the program work or the %.2f? Also if you want more accuracy how much more can you get using %.f? Will %.f just give you an integer? Will %.9 give you 9 digits of accuracy? Also why can't you just use %f to say that BMI is just a floating point and make it print as many decimals as a floating point can?

Your questions are partially answered by the link that I gave to the page that explains formatting strings. The basic idea is that "printf" takes an arbitrary number of arguments, the first of which is a formatting string containing zero or more conversion specifiers. The arguments after that, if present, are matched with each conversion specifier and printed accordingly. It is really worthwhile reading the text in that link (or a similar text in your reference book) because you will see this pattern very often, even in other programming languages.

If you leave out the newline, your BMI will print correctly but the shell prompt will appear immediately after it, on the same line. This is not very pretty.

The amount of accuracy you can get depends on the data type of your variable. I suppose you can print a float with 9 or even 15 digits of accuracy, but because of the memory space usually reserved for a float everything beyond the 7th digit or so will not be meaningful.

Some of your other questions can be answered by simply adapting the code accordingly, recompiling and rerunning. Experimenting like this is a good thing. For example, as you can see in your own OP, "%.f" will indeed just give you an integer as output, without a newline. Some of this behavior is system dependent.
 
  • Like
Likes Marcin H

What is BMI and why is it important?

BMI, or Body Mass Index, is a measure of body fat based on a person's weight and height. It is important because it can indicate whether a person is at a healthy weight for their height and can be used as a screening tool for potential health issues.

How do I calculate my BMI using C Lang?

To calculate your BMI using C Lang, you will need to know your weight in kilograms and your height in meters. Your formula would be: BMI = weight (kg) / (height (m) * height (m)).

What is a healthy BMI range?

A healthy BMI range is typically between 18.5 to 24.9. A BMI below 18.5 is considered underweight, while a BMI above 24.9 is considered overweight.

Can BMI be used for everyone?

No, BMI is not an accurate measure for everyone, as it does not take into account factors such as muscle mass and body composition. It is best used for adults who are not pregnant or breastfeeding.

How often should I calculate my BMI?

It is recommended to calculate your BMI once a year or as advised by your doctor. If you are making significant changes to your weight or lifestyle, you may want to recalculate more frequently to track your progress.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
891
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top