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. - #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;
-
}
-
-
9 2752
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??
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
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. - float compute_bmi(float, float);
you're trying to do below, but it is incorrect for the function prototype and its definition. - 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.
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.
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 ?
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.
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 ?
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.
This 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: - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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 =...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |