Bill TP
Bill TP

Reputation: 1097

Cannot covert from 'double' to 'double[1]' in C

I wrote a very small function in C. The function should read two [longitude], [latitude] location matrices. And my goal is to output distances between the two and a weighting array. The following is the code I wrote:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846

double hdist(double in1[][3], double in2[][3], double dout[][1], double wout[][1])
/* data input: in1, in2 */
/* output: dout, wout */
{
    int i,j,k,rowin1,rowin2;
    double lonH, latH;
    rowin1=(sizeof(in1)/sizeof(in1[0])); // The number of observations for in1
    rowin2=(sizeof(in2)/sizeof(in2[0])); // The number of observations for in2

    k=0;
    for(i=0;i<rowin1;i++) {
        for(j=0;j<rowin2;j++) {
            lonH = sin(0.5*(in1[i][0]-in2[i][0])*PI/180.0)
                 * sin(0.5*(in1[i][0]-in2[i][0])*PI/180.0);
            latH = sin(0.5*(in1[i][1]-in2[i][1])*PI/180.0)
                 * sin(0.5*(in1[i][1]-in2[i][1])*PI/180.0);
  /* distance in kilometers */
            dout[k] = 0.001*6372797.560856*2*asin(sqrt(latH+(cos(in1[i][1]*PI/180.0)
                    * cos(in2[j][1]*PI/180))*lonH)); 
  /* weights */
            wout[k] = in1[i][3]*in2[j][3];
            k++;
        }
    }
}

I got an error message in the compile time:

dout2.c(21):error C2440: '=' : cannot convert from 'double' to 'double [1]'
dout2.c(23):error C2440: '=' : cannot convert from 'double' to 'double [1]'

Would you please let me know how to revise the code --- in order to output the "dout" and "wout" arrays?

Upvotes: 0

Views: 78

Answers (5)

Bill TP
Bill TP

Reputation: 1097

EDIT: Thank you for all your comments:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846

void hdist(double in1[][3], double in2[][3], 
           int num1, int num2, 
       double dout[], double wout[])
/* data input: in1[lon,lat], in2[lon,lat],
           num1: The # of obs in1,
           num2: The # of obs in2 */
/* output: dout, wout */
{
int i,j,k,rowin1,rowin2;
double lonH,latH;

rowin1 = num1;
rowin2 = num2;

k=0;
for(i=0;i<rowin1;i++) {
    for(j=0;j<rowin2;j++) {
        lonH = sin(0.5*(in1[i][0]-in2[j][0])*PI/180.0)
        *sin(0.5*(in1[i][0]-in2[j][0])*PI/180.0);
        latH = sin(0.5*(in1[i][1]-in2[j][1])*PI/180.0)
        *sin(0.5*(in1[i][1]-in2[j][1])*PI/180.0);
/* distance in kilometers */
        dout[k] = 0.001*6372797.560856*2
        *asin(sqrt(latH+(cos(in1[i][1]*PI/180.0)
        *cos(in2[j][1]*PI/180.0))*lonH)); 
/* weights */
        wout[k] = in1[i][2]*in2[j][2];
        k++;
    }
}
return; // Do I need this "return;" statement??
}

int main()
{
/* lon, lat */
double loc1[3][3] = {{-75.547513,39.757077,679},
                     {-75.554342,39.749864,1199},
             {-75.555394,39.730672,626}};
double loc2[2][3] = {{-75.688608,39.284356,466},
                     {-75.530489,39.770331,320}};
double dout[6]; double wout[6];
int i,n1,n2;

n1 = sizeof(loc1)/sizeof(loc1[0]);
n2 = sizeof(loc2)/sizeof(loc2[0]);

printf("The number of rows loc1=%d\n", n1);
printf("The number of rows loc2=%d\n", n2);

hdist(loc1,loc2,n1,n2,dout,wout);

for(i=0;i<6;i++){
    printf("Distance = %.3f\n", dout[i]);
}

for(i=0;i<6;i++){
    printf("weight = %.1f\n", wout[i]);
}

return 0;
}

Question: Do I need the "return;" statement in the "void hdist" function?

Upvotes: 1

Joseph Quinsey
Joseph Quinsey

Reputation: 9962

There are at least three errors:

1) You need to change double dout[][1] to double dout[]. Or, alternatively, change dout[k] to dout[k][0]. Ditto for wout.

2) Change double hdist(...) to void hdist(...), because your function does not return a value.

3) The calculation rowin1=(sizeof(in1)/sizeof(in1[0])) is incorrect. You'll need to do this outside of hdist, and then pass rowin1 in as a parameter. Ditto for rowin2.

Upvotes: 2

Natasha Dutta
Natasha Dutta

Reputation: 3272

dout[k] and wout[k], you're missing the second index on these cases.

As per your case, dout[k] is of data type double[1] and in1[i][3] is of type double. Same for wout[k].

Your compiler is very right. :-)

Maybe you want to change dout and wout to 1D array?

Upvotes: 3

onemasse
onemasse

Reputation: 6584

The best way to solve this, I guess, is to remove the extra dimensions on dout and wout, like so:

double hdist(double in1[][3], double in2[][3], double dout[], double wout[])

Upvotes: 1

unwind
unwind

Reputation: 399753

This:

wout[i] = in1[i][3] * in2[j][3];

assigns the result of a multiplication, which is clearly a single double, to a variable declared as double wout[][2]. That doesn't make any sense, you really can't assign a single value to an array.

Upvotes: 2

Related Questions