#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;
}
Comments
Leave a comment