# Thread: cannot convert double to double*

1. ## cannot convert double to double*

i'm trying to calculate the minimum, maximum, average, etc. of an array. using the calcbyaddr function. i keep getting this error and i don't know what it means. i'm getting it at line 27, 63, 64, 72, 74. do i need to change the array to double*ar[]? this is an assignment and in the assignment they specifically show double ar[], so i'm unsure if i'm supposed to be using a * for the array.
Code:
```#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;

int i;

void sortarrays (double ar[]);
void calcByAddr( double ar[], int, double*, double*, double*, double*, double* );

int main()
{

double ar[15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0, 7.8, 46.8, 1.3, 0.1};
int num;
double min, max, average, standarddev, median;
sortarrays(ar);
calcByAddr(ar, num, min, max, average, standarddev, median);

return 0;

}

void sortarrays (double ar[])

{

int i, j;
for (i=0; i<15; i++)
{
for(j=i+1; j<15; j++)
{
if(ar[i]>ar[j]) //CONDITION :
{
int temp;
temp = ar[i]; //save a copy of value in i
ar[i] = ar[j]; //copy value from j to i
ar[j] = temp; //copy saved value from i to j
}
}
}
}

void calcByAddr( double ar[], int num, double* min, double* max, double* average, double* standarddev, double* median )
{
int i;
double sum;
min=ar[0];
max=ar[14];

num=15;

for (i=0; i<15; i++)
{
sum+=ar[i];
};
average = sum/num;

median = ar[7];
}```

2. Notice all those stars in the declaration of calcByAddr? That means you can't pass a double in in those spots, but a double* instead. Those are your output variables, so they need to be passed either by reference (C++ preference) or by pointer (old-time C style).

3. thanks. i tried changing some things, have almost got it working i think. i'm still getting an error at line 72 that i can't figure out. invalid operand types of double and int to binary operator. what does that mean? also... i'm concerned i'm not using the function correctly. i have to use pointers, that's the point of this assignment. i don't know anything about them, but i have to make sure i'm still using them, or i'll get killed on points. here's my updated code.

so i've tried just putting 15 in place of num to solve for average, but it doesn't make any difference.

Code:
```#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;

int i;

void sortarrays (double ar[]);
void calcByAddr( double ar[], int, double*, double*, double*, double*, double* );

int main()
{

double ar[15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0, 7.8, 46.8, 1.3, 0.1};
int num;
double min, max, average, standarddev, median;
sortarrays(ar);
calcByAddr(ar, num, &min, &max, &average, &standarddev, &median);

return 0;

}

void sortarrays (double ar[])

{

int i, j;
for (i=0; i<15; i++)
{
for(j=i+1; j<15; j++)
{
if(ar[i]>ar[j]) //CONDITION :
{
int temp;
temp = ar[i]; //save a copy of value in i
ar[i] = ar[j]; //copy value from j to i
ar[j] = temp; //copy saved value from i to j
}
}
}
}

void calcByAddr( double ar[], int num, double* min, double* max, double* average, double* standarddev, double* median )
{
int i;
double sum=0;
min=&ar[0];
max=&ar[14];

num=15;

for (i=0; i<15; i++)
{
sum+=ar[i];
};
average = &sum/num;

median = &ar[7];
}```

4. You can't divide a pointer (which is what &sum is) by anything.

5. hm... that presents a challenge in calculating the average. i've noticed ( i took out the average for the moment to get the program to run) that i am getting very odd results for min, max, and median when i display them. thanks for your help, i'm making progress.

6. changed code, got rid of &'s
Code:
```#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;

int i;

void sortarrays (double ar[]);
void calcByAddr( double ar[], int, double*, double*, double*, double*, double* );

int main()
{

double ar[15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0, 7.8, 46.8, 1.3, 0.1};
int num;
double min, max, average, standarddev, median;
sortarrays(ar);
calcByAddr(ar, num, &min, &max, &average, &standarddev, &median);

return 0;

}

void sortarrays (double ar[])

{

int i, j;
for (i=0; i<15; i++)
{
for(j=i+1; j<15; j++)
{
if(ar[i]>ar[j]) //CONDITION :
{
int temp;
temp = ar[i]; //save a copy of value in i
ar[i] = ar[j]; //copy value from j to i
ar[j] = temp; //copy saved value from i to j
}
}
}
}

void calcByAddr( double ar[], int num, double* min, double* max, double* average, double* standarddev, double* median )
{

int i;
double sum=0;
*min=ar[0];
*max=ar[14];

num=15;

for (i=0; i<15; i++)
{
sum+=ar[i];
};
*median = ar[7];

*average=sum/15;

}```

7. ok thanks a lot man. of course i have another problem... as i mentioned, when i display the min, max, or anything, i'm getting very odd values. with the code i just posted, i get
min: 0x22ff20
max:0X22ff18
average:0x22ff10

i left out the display part of the code, obviously. but when i put it in, unhappy things happen.

what on earth is that?

8. Originally Posted by joeman
ok thanks a lot man. of course i have another problem... as i mentioned, when i display the min, max, or anything, i'm getting very odd values. with the code i just posted, i get
min: 0x22ff20
max:0X22ff18
average:0x22ff10

i left out the display part of the code, obviously. but when i put it in, unhappy things happen.

what on earth is that?
The addresses of your variables. You should go read about pointers and how to use them again and then take a good look at your code.

Popular pages Recent additions