BMI Calculator problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sprockets
    New Member
    • Apr 2014
    • 14

    BMI Calculator problem

    Okay so I'm almost finished, but when compiling, it says there is an error on lines 10 & 28. Not exactly sure how to fix it, so any help would be nice.

    Code:
    #include <stdio.h>
    
    #define MAXDATA 1000             /*Max number of lines allowed*/
    
    #define FALSE 0
    #define TRUE 1
    
    
    int read_data(char[], float[], float[]);
    float compute_bmi(float, float);
    void sort(float[], float[], int);
    float mean(float[], int);
    float median(float[], int, int);
    
    
    int main() {
      float height[MAXDATA];
      float weight[MAXDATA];
      float bmi[MAXDATA];
      float height_sorted[MAXDATA];    /*sorted = in order from least-greatest*/
      float weight_sorted[MAXDATA];
      float bmi_sorted[MAXDATA];
      float mean1, mean2, mean3;
      float median1, median2, median3;
    
    
      read_data("file.dat", height, weight);
      bmi = compute_bmi(height, weight);
    
    
      mean1 = mean(height, MAXDATA);
      mean2 = mean(weight, MAXDATA);
      mean3 = mean(bmi, MAXDATA);
    
      sort(height, height_sorted, MAXDATA);
      sort(weight, weight_sorted, MAXDATA);
      sort(bmi, bmi_sorted, MAXDATA);
    
      median1 = median(height_sorted, MAXDATA, TRUE);
      median2 = median(weight_sorted, MAXDATA, TRUE);
      median3 = median(bmi_sorted, MAXDATA, TRUE);
    
      FILE *out_fp;
      out_fp = fopen("file.report", "w");
    
      fprintf(out_fp,"\nBMI Calculator Report \n");
    
      fprintf(out_fp,"\nHeight\n");
      fprintf(out_fp,"    mean = %f   median = %f \n", mean1, median1);
    
      fprintf(out_fp,"\nWeight\n");
      fprintf(out_fp,"    mean = %f   median = %f \n", median2, median2);
    
      fprintf(out_fp,"\nBMI\n");
      fprintf(out_fp,"    mean = %f   median = %f \n", mean3, median3);
    
      fclose(out_fp);
     return 0;
    }
    
    
    
    int read_data (char file[], float height[], float weight[])  {
      FILE *fp;
      int i;
      int numdata;
    
      fp = fopen(file, "r");
      fscanf(fp, "%d", &numdata);
    
      if(numdata > MAXDATA){
        printf("Error");
        return 0;
      }
    float compute_bmi(float height, float weight)  {
      float bmi;
    
      bmi = (weight / (height*height)) * 730;
    
      return 0;
    }
    
    
    float mean(float x[], int len)  {
      int i;
      float ans = 0.0;
    
      for (i=0; i<len; i++) {
        ans += x[i];
      }
    
      ans = ans / len;
    
      return ans;
    }
    
    
    void sort(float x[], float y[], int len) {
      int i;
      int done = FALSE;
      int swapped;
      float tmp;
     for (i=0; i<len; i++) {
        y[i] = x[i];
      }
    
      while (!done) {
        swapped = FALSE;
        for (i=0; i<(len-1); i++) {
          if (y[i] > y[i+1]) {
            tmp = y[i];
            y[i] = y[i+1];
            y[i+1] = tmp;
            swapped = TRUE;
          }
        }
        if (!swapped) {
          done = TRUE;
        }
      }
    float median(float x[], int len, int sorted) {
      float y[len];
      float ans;
    
    
      if (!sorted) {
        sort(x, y, len);
    
        if (len % 2) {
          ans = y[ (len-1) / 2 ];
        } else {
          ans = (y[ len/2 ] + y[ len/2 - 1 ]) / 2.0;
        }
    
      } else {
    
        if (len % 2) {
          ans = x[ (len-1) / 2 ];
        } else {
          ans = (x[ len/2 ] + x[ len/2 - 1 ]) / 2.0;
        }
      }
    
      return ans;
    }
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    Your best bet would be to copy / paste any compiler errors in your topic. If the code you posted is the same as the compiler errors, then line 10 *looks* okay.

    line 28 has an issue. The prototype returns a float and not an array; however, I don't believe C allows aggregate assignment of arrays. wfc??

    Comment

    • sprockets
      New Member
      • Apr 2014
      • 14

      #3
      Line 10 says note: expected ‘float’ but argument is of type ‘float *’

      Line 28 says incompatible type for argument 2 of ‘compute_bmi’


      I've been sitting here trying to fix it for hours with no luck. I'd appreciate if anyone could help fix this


      EDIT: Maybe there could be a problem in the actual compute_bmi function? Can anyone check please

      Comment

      • divideby0
        New Member
        • May 2012
        • 131

        #4
        The errors occur because your prototype doesn't match your call. The prototype takes two floats as arguments and returns a float; neither the return value nor the argument list should be float arrays.

        Code:
        float compute_bmi(float, float);
        you're trying to do below, but it is incorrect for the function prototype and its definition.
        Code:
        array = compute_bmi(array, array);
        height, weight, and bmi have all been declared as float arrays. code the function to accept arrays as arguments if that's what you intend to do.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          An array of float is OK. But your array named height means that the name "height" is the address of element 0. Therefore, "height" is a float pointer: a float*.

          Your function prototype says height is a float.

          This is your error.

          Comment

          • sprockets
            New Member
            • Apr 2014
            • 14

            #6
            Hm Okay thanks, I fixed the program to have no errors, but I am getting the wrong mean and median. Anyone see what the errors are ?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This is a great opportunity for you to learn how to use your debugger. You step through the code function by function and verify the functions a preforming correctly.

              I do suggest a) you use double instead of float, and b) avoid mixing integer and float in the same calculation. For example, 3/2 is 1 (there is only one 2 in 3) whereas 3.0/2.0 is 1.5.

              Comment

              • sprockets
                New Member
                • Apr 2014
                • 14

                #8
                Okay I actually found my error, but I'm not sure how to fix it.. I'm dividing the mean by 1000(since I put maxdata) but what I really want to do is divide by the number of lines in the file.

                In the file I am reading from, I have the amount of lines as the first line, so how would I get that data and put it into my main ?

                Comment

                • divideby0
                  New Member
                  • May 2012
                  • 131

                  #9
                  You can use a variable to count the lines as you read the contents of the file. It sounds like it should be the index position for the array as you read the file.

                  the read_data function looks incomplete; what value does the return represent? the status of the file? the number of elements to read? some other value?

                  as a side note, like sscanf and scanf, fscanf returns the number of successfully converted and stored items; you can use this value to detemine whether or not the read succeeded.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    This code:
                    Code:
                    int read_data (char file[], float height[], float weight[])  {
                      FILE *fp;
                      int i;
                      int numdata;
                    
                      fp = fopen(file, "r");
                      fscanf(fp, "%d", &numdata);
                    
                      if(numdata > MAXDATA){
                        printf("Error");
                        return 0;
                      }
                    reads the number of lines but the numdata variable dies when the function has completed. Also, this function does not read anything into the height and weight arrays so unless there is another read function your data is never read.

                    All you need to do is rework this function to:

                    Code:
                    int read_lines(FILE* fp, const char* file, int* numdata)
                    {
                    
                    	fp = fopen(file,  "r");
                    	fscanf(fp, "%d", numdata);
                    
                    	if (*numdata > MAXDATA){
                    		return 1;     /* error */
                    	}
                    	return 0;
                    }
                    
                    int main()
                    {
                    	FILE* fp = 0;
                    	int numdata = 0;
                    	int rval;
                    
                    	rval = read_lines(fp, "file.dat",  &numdata);
                    	if (rval != 0)
                    	{
                    		printf("error");
                    		return 1;
                    	}
                    
                    
                    }
                    Here you pass in the address of numdata rather than have it as a local variable. This way the function can change the value of the variable in main().

                    Then you write another read function for the height and weight arrays.

                    Notice the function returns 0 when it works and not-zero when it fails. This is a fairly common practice since it allows for multiple error codes. Also, the function does not display anything. Displays should be in main(). Code in main() determines what to display. This keeps the screen control in one place.

                    Comment

                    Working...