Thread: cannot convert double to double*

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't divide a pointer (which is what &sum is) by anything.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    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. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    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. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    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. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by joeman View Post
    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.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 05:14 AM
  5. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM