still have no idea how to call function

working on my project which reading the file to get weight and height and do max and min and average my teach required to use call function when I set the implementation function, I can not call it said "can not convert double to float* don;t know why! all the syntax is right I think I just can call my void function please tell me where did no do wrong!!!! it is duo midnight today!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 #include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#define N 500
float a,b;
float BMI,sumx,x;
int k=0;
int l=0;
int c,d,m,p;
double w[N], h[N];
float high_w,high_h,aveg_w;
void ComputeMetrics(float *weight, float w_count, float &high_w);


void ComputeMetrics(float *weight, float w_count, float &high_w)
{
    for(c = 0; c<(k-1);c++)
{
    for(d = 0;d<k-c-1;d++)
    {


        if(w[d]>w[d+1])
    {
        m = w[d];
        w[d] = w[d+1];
        w[d+1] = m;
    }
    }
}

    printf("%lf\n",w[c]);
}



int main()
{
    FILE *weight;
    weight =fopen("1.dat","r");
    while(fscanf(weight,"%lf",&w[k])==1)
    {
        k++;
    }
ComputeMetrics(w,k,high_w);


}
replace double with float in line 11
it does not work something wrong with my array!
Hi,

Some problems:

Don't use global variables

ALWAYS initialise your variables to some thing when declaring them. Comment on the same line if necessary

Don't use #define macros . Make them a const variable instead.

Don't mix types - w_count is a float, but you send an int.

Prefer double to float

Use meaningful names for your variables

Set your compiler warnings to their maximum level, so you can identify potential problems.

When opening files, check to see that it worked - the return value from fopen.


Try not to mix C and C++ code
TheIdeasMan wrote:
ALWAYS initialise your variables to some thing when declaring them. Comment on the same line if necessary
Only initialize your variables if you're not going to immediately use them; it's a waste of time.

Prefer double to float
This doesn't really make any sense. You use the right one when you need to. With graphics programming you often find yourself dealing with floats, and avoid doubles whenever possible (you never use them). If anything, it's the other way around.

Only initialize your variables if you're not going to immediately use them; it's a waste of time.


Yes, but it is one of the biggest sources of errors. People have a nasty habit of forgetting - then wonder why they have a seg fault. Sometimes people forget to change the value of their variable at all.

It is just an act of defensive programming, there are lots of examples of defensive programming that waste much more resources than that, so that the function is unlikely to fail.

This doesn't really make any sense. You use the right one when you need to. With graphics programming you often find yourself dealing with floats, and avoid doubles whenever possible (you never use them). If anything, it's the other way around.


I was a little brief on that comment, I should have if one needs to, because of a graphics library, or you have billions of them.

If anything, it's the other way around.


Perhaps if you always do graphics: I never use float, because I always need more precision.

However, precision issues are another big problem - mainly because people aren't aware of the lack of precision floats have. I am not saying you aren't aware :+)

I first learnt this from K&R C programming about 25 years ago. Scott Myers also says this. Also C/C++ defaults to double as a FP type to help avoid this issue.

:+)
Topic archived. No new replies allowed.