473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define MAXDATA 1000             /*Max number of lines allowed*/
  4.  
  5. #define FALSE 0
  6. #define TRUE 1
  7.  
  8.  
  9. int read_data(char[], float[], float[]);
  10. float compute_bmi(float, float);
  11. void sort(float[], float[], int);
  12. float mean(float[], int);
  13. float median(float[], int, int);
  14.  
  15.  
  16. int main() {
  17.   float height[MAXDATA];
  18.   float weight[MAXDATA];
  19.   float bmi[MAXDATA];
  20.   float height_sorted[MAXDATA];    /*sorted = in order from least-greatest*/
  21.   float weight_sorted[MAXDATA];
  22.   float bmi_sorted[MAXDATA];
  23.   float mean1, mean2, mean3;
  24.   float median1, median2, median3;
  25.  
  26.  
  27.   read_data("file.dat", height, weight);
  28.   bmi = compute_bmi(height, weight);
  29.  
  30.  
  31.   mean1 = mean(height, MAXDATA);
  32.   mean2 = mean(weight, MAXDATA);
  33.   mean3 = mean(bmi, MAXDATA);
  34.  
  35.   sort(height, height_sorted, MAXDATA);
  36.   sort(weight, weight_sorted, MAXDATA);
  37.   sort(bmi, bmi_sorted, MAXDATA);
  38.  
  39.   median1 = median(height_sorted, MAXDATA, TRUE);
  40.   median2 = median(weight_sorted, MAXDATA, TRUE);
  41.   median3 = median(bmi_sorted, MAXDATA, TRUE);
  42.  
  43.   FILE *out_fp;
  44.   out_fp = fopen("file.report", "w");
  45.  
  46.   fprintf(out_fp,"\nBMI Calculator Report \n");
  47.  
  48.   fprintf(out_fp,"\nHeight\n");
  49.   fprintf(out_fp,"    mean = %f   median = %f \n", mean1, median1);
  50.  
  51.   fprintf(out_fp,"\nWeight\n");
  52.   fprintf(out_fp,"    mean = %f   median = %f \n", median2, median2);
  53.  
  54.   fprintf(out_fp,"\nBMI\n");
  55.   fprintf(out_fp,"    mean = %f   median = %f \n", mean3, median3);
  56.  
  57.   fclose(out_fp);
  58.  return 0;
  59. }
  60.  
  61.  
  62.  
  63. int read_data (char file[], float height[], float weight[])  {
  64.   FILE *fp;
  65.   int i;
  66.   int numdata;
  67.  
  68.   fp = fopen(file, "r");
  69.   fscanf(fp, "%d", &numdata);
  70.  
  71.   if(numdata > MAXDATA){
  72.     printf("Error");
  73.     return 0;
  74.   }
  75. float compute_bmi(float height, float weight)  {
  76.   float bmi;
  77.  
  78.   bmi = (weight / (height*height)) * 730;
  79.  
  80.   return 0;
  81. }
  82.  
  83.  
  84. float mean(float x[], int len)  {
  85.   int i;
  86.   float ans = 0.0;
  87.  
  88.   for (i=0; i<len; i++) {
  89.     ans += x[i];
  90.   }
  91.  
  92.   ans = ans / len;
  93.  
  94.   return ans;
  95. }
  96.  
  97.  
  98. void sort(float x[], float y[], int len) {
  99.   int i;
  100.   int done = FALSE;
  101.   int swapped;
  102.   float tmp;
  103.  for (i=0; i<len; i++) {
  104.     y[i] = x[i];
  105.   }
  106.  
  107.   while (!done) {
  108.     swapped = FALSE;
  109.     for (i=0; i<(len-1); i++) {
  110.       if (y[i] > y[i+1]) {
  111.         tmp = y[i];
  112.         y[i] = y[i+1];
  113.         y[i+1] = tmp;
  114.         swapped = TRUE;
  115.       }
  116.     }
  117.     if (!swapped) {
  118.       done = TRUE;
  119.     }
  120.   }
  121. float median(float x[], int len, int sorted) {
  122.   float y[len];
  123.   float ans;
  124.  
  125.  
  126.   if (!sorted) {
  127.     sort(x, y, len);
  128.  
  129.     if (len % 2) {
  130.       ans = y[ (len-1) / 2 ];
  131.     } else {
  132.       ans = (y[ len/2 ] + y[ len/2 - 1 ]) / 2.0;
  133.     }
  134.  
  135.   } else {
  136.  
  137.     if (len % 2) {
  138.       ans = x[ (len-1) / 2 ];
  139.     } else {
  140.       ans = (x[ len/2 ] + x[ len/2 - 1 ]) / 2.0;
  141.     }
  142.   }
  143.  
  144.   return ans;
  145. }
  146.  
  147.  
May 14 '14 #1
9 2752
divideby0
131 128KB
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??
May 15 '14 #2
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
May 15 '14 #3
divideby0
131 128KB
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.

Expand|Select|Wrap|Line Numbers
  1. float compute_bmi(float, float);
you're trying to do below, but it is incorrect for the function prototype and its definition.
Expand|Select|Wrap|Line Numbers
  1. 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.
May 15 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
May 15 '14 #5
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 ?
May 15 '14 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
May 16 '14 #7
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 ?
May 16 '14 #8
divideby0
131 128KB
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.
May 16 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. int read_data (char file[], float height[], float weight[])  {
  2.   FILE *fp;
  3.   int i;
  4.   int numdata;
  5.  
  6.   fp = fopen(file, "r");
  7.   fscanf(fp, "%d", &numdata);
  8.  
  9.   if(numdata > MAXDATA){
  10.     printf("Error");
  11.     return 0;
  12.   }
  13.  
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:

Expand|Select|Wrap|Line Numbers
  1. int read_lines(FILE* fp, const char* file, int* numdata)
  2. {
  3.  
  4.     fp = fopen(file,  "r");
  5.     fscanf(fp, "%d", numdata);
  6.  
  7.     if (*numdata > MAXDATA){
  8.         return 1;     /* error */
  9.     }
  10.     return 0;
  11. }
  12.  
  13. int main()
  14. {
  15.     FILE* fp = 0;
  16.     int numdata = 0;
  17.     int rval;
  18.  
  19.     rval = read_lines(fp, "file.dat",  &numdata);
  20.     if (rval != 0)
  21.     {
  22.         printf("error");
  23.         return 1;
  24.     }
  25.  
  26.  
  27. }
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.
May 16 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
22
by: jamestuck21 | last post by:
Hi, I'm trying to work out a binary division problem 1100 / 101010101010111 Here is what I have so far but I'm not sure if I'm doing it correctly and I'm suppose to continue the division...
2
by: traineeirishprogrammer | last post by:
I want to develop a calculator in javascript, like the one in microsoft; I cant get around the logic of it. Here is my problem. If I type in a number then I have to wait for an operator then I...
3
by: Stolas | last post by:
Hello all, I'm attempting to create a Fraction Calculator. But for some reasons on my RationalTest program it is giving me an error that getAdd in Rational cannot be applied to (Rational). I'm very...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
1
by: danielmessick | last post by:
Hello, I'm trying to fix my Java applet to have a fully functional calculator. I managed to create the buttons and have the +, -, /, *, =, and clear buttons WORKING. But, I cant seem to figure out...
0
by: ceckerty | last post by:
I have a project I am working on and have hit a road block. This is what the program must accomplish: The Windows application opens with a text box where the user enters their SSN and 4 seperate...
0
Curtis Rutland
by: Curtis Rutland | last post by:
Have any of you ever used a Reverse Polish Notation calculator? I did in high school. It was easily the best calculator ever (the HP=32SII). RPN is great, because you don't have to use parenthesis....
4
by: Nala Flores | last post by:
im try to activate a tutorial code for wxPython, the calculator.py...but it gives me the next message File "C:\Users\AOFU\Desktop\A-PYTHON\calculator.py", line 184, in <module> app =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.