Feyisayo Sonubi
Feyisayo Sonubi

Reputation: 1072

How can I pull this off in C?

I have a program simulating a BMI (Body Mass Index) Calculator that takes in inputs from 10 users asking each user for a weight and a height, the program then calculates the BMI and prints the result of the computation in a formatted output.

Something like this:

                  USER         BMI         STATUS
                  1
                  :
                  10

So I thought of this, I would take in the inputs from the users first, then try to display the computation of the inputs in the above formatted way. Here's what I've tried to come up with which keeps asking the users for their weight and height until the 10th user.

#include <stdio.h>
#include <math.h>

int main(void) {
    
    float weight_value = 0.0f;
    float user_height = 0.0f;
    float BMI = 0.0f;
    char BMI_Status[30];
    
    int i;
    
    for ( i = 1; i <= 10; i++)
    
    {
        printf("Please enter your weight: "); 
        scanf("%f", &weight_value);  //Reads in the user's weight
        printf("Now enter your height: ");
        scanf("%f", &user_height); //Reads in the user's height
        
        BMI = weight_value / (user_height * user_height);
        
    }
    
    return 0;
}

The BMI Status is a string that displays different states of the computed BMI values, which are "Underweight", "Normal", "Overweight" for when the BMI value is within a given range.

The code snippet above is still incomplete as I'm still finding it difficult to add the rest of the code. Here are the issues I'm trying to solve.

  1. How can I print out the BMI values after I have taken in the 10 inputs?

    I have tried having a nested for loop that prints out the values, but this is illogical as there will now be an output of the BMI for every iteration of the outer for loop, so I'm guessing need a way to make scanf() hold the BMI values after the for loop's block.

  2. How do I make the status strings appear?

    I know there's no string datatype in C, so I'll have to declare a char array that holds the string for the status, but these are different strings for different statuses, and it has to be printed out for every line. I thought of having if else if else conditions for this but I just can't get the char array to work.

I already successfully wrote a code that prints out the user's BMI at every iteration, but this isn't how its supposed to be done.

Another thing I thought of was keeping the BMI values in an array of floating point numbers then displaying the values from the array in the above formatted way, but I'm still a bit sceptical about how I intend doing this.

Upvotes: 0

Views: 363

Answers (4)

Igor Pejic
Igor Pejic

Reputation: 3698

Here the complete code to display the use of arrays to store the values. Your idea of using an if else for displaying how overweight a person is was good and so I implemented it.

#include <stdio.h>
#include <math.h>


int main(void) {

    float weight_value[10];  //two arrays of ten elements each
    float user_height[10];

    float BMI = 0.0f;
    char BMI_Status[30];

    int i;

    for ( i = 0; i < 10; i++)   //the array starts from zero in C
    {
        printf("Please enter your weight: "); 
        scanf("%f", &weight_value[i]);  //store the values in the array
        printf("Now enter your height: ");
        scanf("%f", &user_height[i]); //store the values in the array
    }
    printf("USER     BMI\t\tSTATUS\n");

    for( i = 0; i < 10; i++)  //pass the array once again and calculate the needed values
    {
        BMI = weight_value[i] / (user_height[i] * user_height[i]);
        printf("%d\t%f\t", i+1, BMI);
        if(BMI < 30)
            printf("Underweight\n");
        else if(BMI < 50)
            printf("Normal\n");
        else
            printf("Overweight\n");
    }
    return 0;
}

Upvotes: 2

Kir Chou
Kir Chou

Reputation: 3080

  1. Add an if statement in the for-loop
float BMI[10];
int j;

for ( i = 1; i <= 10; i++) 
{
  printf("Please enter your weight: "); 
  scanf("%f", &weight_value);  //Reads in the user's weight
  printf("Now enter your height: ");
  scanf("%f", &user_height); //Reads in the user's height
  
  BMI[i-1] = weight_value / (user_height * user_height);
  if (i==10)
      for (j=0; j<10; j++)
           printf ("%d\n", BMI[j]); 
}
  1. Use char **a or char *a[] for example:
char s[12] = "Hello World!"; 
char *p; 
p = &s[0];

char *BMI_Status[30];             //declare 30 char* 
BMI_Status[0] = "Fat"; 
BMI_Status[1] = "Thin";
...
//output
printf ("%s", BMI_Status[0]);            //Fat

Upvotes: 1

igon
igon

Reputation: 3046

The following code will store the weight and height of 4 users and print their BMI for an arbitrary number of thresholds (set to two in this case).

You can solve problem 1. by storing the weight and height into arrays (in the first loop).

You can then display a BMI status string by first defining a set of thresholds and a corresponding status string. In the second loop, for every user you compute and check whether the BMI value is under a given threshold and print its corresponding status string.

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

#define USERS 4
#define THRESHOLDS 2

int main(void) {

    float weight[USERS];
    float height[USERS];
    float BMI = 0.0f;
    char * BMI_Status[THRESHOLDS+1] = {"Normal","Overweight","Obese"};
    float thresholds[THRESHOLDS] = {10.0,20.0};

    int i,j;

    for ( i = 0; i < USERS; i++)

    {
        printf("Please enter your weight: ");
        scanf("%f", &(weight[i]));  //Reads in the user's weight                                                                                                                                                                             
        printf("Now enter your height: ");
        scanf("%f", &(height[i])); //Reads in the user's height                                                                                                                                                                              

    }

    for ( i = 0; i < USERS; i++)

    {
        BMI = weight[i]/(height[i]*height[i]);
        printf("%d\t%f\t",i,BMI);
        for ( j = 0 ; j < THRESHOLDS ; j++ ){
            if ( BMI < thresholds[j]){
                break;
            }
        }
        printf("%s\n",BMI_Status[j]);
    }


    return 0;
}

Upvotes: 1

bakriawad
bakriawad

Reputation: 345

edited: skip to last

you will need to save them somewhere before you can read them to display it, you need to use an array to store all 10 inputs, at the moment every time a user inputs a data it will rewrite the data that was in the variable, which had the last user's data.

it needs to look like this:

...
float weight_value[10];
float user_height[10];
float BMI =[10];
char BMI_Status[30][3];



for ( int i = 1; i <= 10; i++)

{
    printf("Please enter your weight: "); 
    scanf("%f", &weight_value[i]);  //Reads in the user's weight
    printf("Now enter your height: ");
    scanf("%f", &user_height[i]); //Reads in the user's height

    BMI[i] = weight_value / (user_height * user_height);

}
...

then to display you will need a loop like this

...
printf("user \t weight \t height \t BMi"); 

for ( int i = 1; i <= 10; i++)

{
    printf("%d \t%f \t%f \t%s ", i,user_weight[i],user_height[i],BMI[i]);

}
...

i just re-read the question and realised i misread, however the last bit shows how you can read them from the array and display them.

Upvotes: 1

Related Questions