Sunday 15 March 2015

arrays - How can I pull this off in C? -



arrays - How can I pull this off in C? -

i have programme simulating bmi (body mass index) calculator takes in inputs 10 users asking each user weight , height, programme calculates bmi , prints result of computation in formatted output.

something this:

user bmi status 1 : 10

so thought of this, take in inputs users first, seek display computation of inputs in above formatted way. here's i've tried come keeps asking users weight , height until 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; ( = 1; <= 10; i++) { printf("please come in weight: "); scanf("%f", &weight_value); //reads in user's weight printf("now come in height: "); scanf("%f", &user_height); //reads in user's height bmi = weight_value / (user_height * user_height); } homecoming 0; }

the bmi status string displays different states of computed bmi values, "underweight", "normal", "overweight" when bmi value within given range.

the code snippet above still incomplete i'm still finding hard add together rest of code. here issues i'm trying solve.

how can print out bmi values after have taken in 10 inputs?

i have tried having nested loop prints out values, illogical there output of bmi every iteration of outer loop, i'm guessing need way create scanf() hold bmi values after loop's block.

how create status strings appear?

i know there's no string datatype in c, i'll have declare char array holds string status, these different strings different statuses, , has printed out every line. thought of having if else if else conditions can't char array work.

i wrote code prints out user's bmi @ every iteration, isn't how supposed done.

another thing thought of keeping bmi values in array of floating point numbers displaying values array in above formatted way, i'm still bit sceptical how intend doing this.

here finish code display utilize of arrays store values. thought of using if else displaying how overweight person , implemented it.

#include <stdio.h> #include <math.h> int main(void) { float weight_value[10]; //two arrays of 10 elements each float user_height[10]; float bmi = 0.0f; char bmi_status[30]; int i; ( = 0; < 10; i++) //the array starts 0 in c { printf("please come in weight: "); scanf("%f", &weight_value[i]); //store values in array printf("now come in height: "); scanf("%f", &user_height[i]); //store values in array } printf("user bmi\t\tstatus\n"); for( = 0; < 10; i++) //pass array 1 time 1 time again , calculate 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"); } homecoming 0; }

c arrays for-loop

No comments:

Post a Comment