60 430
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #44180 in C++ for Arco

Question #44180
Body Mass Index
Write a program that calculates and displays a person s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is over- weight or underweight for his or her height. A person s BMI is calculated with the following formula:
BMI
=
weight
×
703 / height^2 ( to the 2 power)
where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is under- weight, or is overweight. A sedentary person s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.
Expert's answer
#include <stdio.h>
#include <string.h>
#define FORMULA_CONSTANT 730
#define SIZE 200
#define CHAR_SIZE 20
int read_data( char inputFileName[], float weightArray[], float heightArray[] ) {
int numberOfRecords, counter;
FILE *in = fopen(inputFileName, "r");
if ( !in ) {
  return 0;
}
fscanf( in, "%d", &numberOfRecords );
for ( counter = 0; counter < numberOfRecords; counter++ ) {
  fscanf( in, "%g,%g", &weightArray[counter], &heightArray[counter] );
}
return counter;
}
float compute_bmi( float weight, float height ) {
return ( weight / height ) * FORMULA_CONSTANT;
}
int main(int argc, char *argv[]) {
char inputFileName[CHAR_SIZE], outputFilename[CHAR_SIZE];
memset(inputFileName, 0, CHAR_SIZE);
memset(outputFilename, 0, CHAR_SIZE);
float weightArray[SIZE], heightArray[SIZE], BMI[SIZE];
int counter, i;
FILE *out;
if ( argc != 3 ) {
  printf( "usage:\n%s <input filename> <output filename>\n", argv[0] );
  return 1;
}
strcpy(inputFileName, argv[1]);
strcpy(outputFilename, argv[2]);
out = fopen(outputFilename, "w+");
if ( !out ) {
  printf("Can not find the output file.\n");
  return 1;
}
counter = read_data(inputFileName, weightArray, heightArray);
if ( counter <= 0 ) {
  printf("Can not open the input file\n");
  return 1;
}
for ( i = 0; i < counter; i++ ) {
  BMI[i] = compute_bmi(weightArray[i], heightArray[i]);
  fprintf(out, "%g\n", BMI[i]);
}
 
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions