pass by reference using pointer.........need help

//program to form a header file
/* using pass by reference in pointer */


#include <iostream>
#include<math.h>

using namespace std;


double getAcorr(double *arr, double *arr1, int size1, int size2);
double getCcorr(double *arr, double *arr1, int size1, int size2);
double getConv(double *arr, double *arr1, int size1, int size2);
double *p;

int main (void)
{
int size1, size2, index,index1, i,j;
double input;
int operation;
double *y, z[100];
double x[10]={1,2,3};
double h[10]={1,2,1};
int m=3,n=3;



cout <<"enter the operation:\n" << endl;
cin >> operation;
switch (operation)
{
case 1:
*p=getAcorr(x, x, m,n);
for (i=0;i<=m+n-2;i++)
{

cout<< *(p+i) << endl;
}


break;

case 2:
*p=getCcorr(x, h, m,n);
for (i=0;i<=m+n-2;i++)
{
cout<< *(p+i) << endl;
}

break;

default :
*p=getConv(x, h, m,n);
for (i=0;i<=m+n-2;i++)
{
cout<< *(p+i) << endl;
}


}

return 0;
}

double getAcorr(double *arr, double *arr1, int size1, int size2)
{
int i,j;
int m=size1,n=size2;
double h[size2];
double x[size1];
double *y, z[100];

for(i=0,j=size1-1; i<size1; i++,j--)
{
z[i]=x[j];

cout <<" reverse vector's elements are=\n" << i <<' ' << z[i] << endl;
}

for(i=0;i<=m+n-2;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(z[i-j]*x[j]);
*p = *y;
}
cout<< "auto corelation sequences are= " << i << " = " << *p << endl;
}

return *p;
}

double getCcorr(double *arr, double *arr1, int size1, int size2)
{
int i,j;
int m=size1,n=size2;
double h[size2];
double x[size1];
double *y, z[100];

for(i=0,j=size1-1; i<size1; i++,j--)
{
z[i]=x[j];

cout <<" reverse vector's elements are=\n" << i <<' ' << z[i] << endl;
}

for(i=0;i<=m+n-2;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(z[i-j]*h[j]);
*p = *y;
}
cout<< "cross corelation sequences are= " << i << " = " << *p << endl;
}

return *p;
}

double getConv(double *arr, double *arr1, int size1, int size2)
{
int i,j;
int m=size1,n=size2;
double h[size2];
double x[size1];
double *y, z[100];

for(i=0;i<=m+n-2;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(x[i-j]*h[j]);
*p = *y;
}
cout<< "convolution is= " << i << " = " << *p << endl;

}
return *p;
}
Last edited on
Please use code tags. Also, tell us what the problem you are having is.
Do you want to:
- Pass an object by reference? Object &obj
- Pass a pointer to an object by value? Object *obj
- Pass a pointer to an object by reference? Object *&obj
i want to use pass by reference i.e Object *&obj
Last edited on
performing auto correlation, cross correlation and convolution as function and call it in main function....but i m unable to achieve it
As far as I can see you don't perform operations within functions that require a reference. Then again, your code does not even use the pointer parameters. Of all the code the global 'p' is perhaps the most baffling.

Side note: VLA (variable length arrays) are not in C++ standard.


Please add the code tags with sensible indentation. It improves readability and it is easier to refer to the code that shows line numbers.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//program to form a header file


#include <iostream>
#include<math.h>

using namespace std;


double getAcorr(double *arr, double *arr1, int size1, int size2); //defination for the autocorrelation using two pointer array of type double
double getCcorr(double *arr, double *arr1, int size1, int size2); //defination for the correlation using two pointer array of type double
double getConv(double *arr, double *arr1, int size1, int size2); //defination for the convolution using two pointer array of type double
double *p;

int main (void)
{
int size1, size2, index,index1, i,j;
   double input;
   int operation;
    double *y, z[100];
	double x[]={1,2,3};
	double h[]={1,2,1};
	int m,n;
	
	//finding length of the given array

  m= (sizeof(x)/sizeof(*x));
  
  n= (sizeof(h)/sizeof(*h));
  
  cout << "length of array=" << m << endl;
  
  cout << "length of array=" << n << endl;
  
  //operation to be selected using switch condition
  
  cout <<"enter the operation:\n" << endl;
  cin >> operation;
	
	switch (operation)
	{
		case 1:
			*p=getAcorr(x, x, m,n); 
			for (i=0;i<=m+n-2;i++)
		       {
		       	cout<< *(p+i) << endl;  // ouput to be display as incementing ith integer
			   }
			
			
			break;
			
			case 2:
				*p=getCcorr(x, h, m,n); 
				for (i=0;i<=m+n-2;i++) 
				{
					cout<< *(p+i) << endl; // ouput to be display as incementing ith integer
				}
			     
			     break;
				 
				 default :
				 	*p=getConv(x, h, m,n);  
				 	for (i=0;i<=m+n-2;i++) 
				{
					cout<< *(p+i) << endl; // ouput to be display as incementing ith integer
				}
			     		
	}
	                      
	return 0;	
 } 
 
 
 // operatin to be carried out in the autocorreletion
 
 double getAcorr(double *arr, double *arr1, int size1, int size2)
 {
 	int i,j;
 	int m=size1,n=size2;
 	 double h[size2];
 	 double x[size1];
 	double *y, z[100];
 	
 	// reversing one the vector before its correletion i.e x[size1]
 	
 	              for(i=0,j=size1-1; i<size1; i++,j--)
	                {
                     z[i]=x[j];
                   
       	              cout <<" reverse vector's elements are=\n" << i <<' ' << z[i] << endl;
	                }  
   // convolving reversed array with other array i.e z[size1] and h[size2]
   
                       for(i=0;i<=m+n-2;i++)
                        {
                           y[i]=0;                                  
                             for(j=0;j<=i;j++)
                             {
                               y[i]=y[i]+(z[i-j]*x[j]);
                               *p = *y;  
                             }
                               cout<< "auto corelation sequences are= " << i << " = " << *p << endl;
                        }
                        
                        return *p;
 }
 
 //// operatin to be carried out in the crosss correletion
 
 double getCcorr(double *arr, double *arr1, int size1, int size2)
 {
 	int i,j;
 	int m=size1,n=size2;
 	 double h[size2];
 	 double x[size1];
 	double *y, z[100];
 	
 	// reversing one the vector before its correletion i.e x[size1]
 	
 	              for(i=0,j=size1-1; i<size1; i++,j--)
	                {
                     z[i]=x[j];
                    
       	              cout <<" reverse vector's elements are=\n" << i <<' ' << z[i] << endl;
	                }
                     
   // convolving reversed array with other array i.e z[size1] and h[size2] 

                       for(i=0;i<=m+n-2;i++)
                        {
                           y[i]=0;                                  
                             for(j=0;j<=i;j++)
                             {
                               y[i]=y[i]+(z[i-j]*h[j]);
                               *p = *y;    
                             }
                               cout<< "cross corelation sequences are= " << i << " = " << *p << endl;
                        }
                        
                        return *p;
 }
 
 
 // operatin to be carried out in the convolution
 
 double getConv(double *arr, double *arr1, int size1, int size2)
 {
 	int i,j;
 	int m=size1,n=size2;
 	 double h[size2];
 	 double x[size1];
 	double *y, z[100];
 	
	              for(i=0;i<=m+n-2;i++)
	              {
		            y[i]=0;
		             for(j=0;j<=i;j++)	
			          {
				         y[i]=y[i]+(x[i-j]*h[j]);
				         *p = *y; 
			          }
	                    cout<< "convolution is= " << i << " = " << *p << endl;
	
                  } 
                  return *p;
 }
 
here it is kindly give your suggetion what i suppose to do
Why do you want to pass those pointers at all? Your getConv, getAcorr, getCcorr functions don't use those parameters.
Could you explain the math behind of any one of those functions? For example, is the "convolution" the same as in this: http://stackoverflow.com/questions/8424170/1d-linear-convolution-in-ansi-c-code
The problem I see is that you aren't clear about where the result of getAcorr(), getCCorr() and getConv() are stored. At line 10 -12 you say that they return a double, but line 43 acts like they return a pointer to the resulting array. And in getAcorr(), you're using *p as though p already points to something, which it doesn't. The parameter names are also confusing because it's arr & arr1, but size1, and size2. This leads to the very confusing fact that the size of arr1 is size2.

I think if you straighten all this out then things will make a lot more sense. I suggest that you get getAcorr() working first. Comment out the other functions. Once getAcorr() is working you can use it as a model for the others.

First, lets pass the result array into the function. Second, straignten out the names:
1
2
3
// Calculate auto-correlation. size1 and size2 are the sizes of arr1 and arr2 respectively.
// Result must be large enough to hold size1+size2-2 elements.
void getAcorr(double arr1[], double arr2[], size_t size1, size_t size2, double result[]);

You don't need to reverse the vector, just work your way backwards through it.

I'm not sure what the right code is for getAcorr(). The code that you have doesn't use the parameters arr1 and arr2 at all. See if you can straighten that out, or describe what the formula is and maybe we can help you.
how can i pass a pointer array to function and call it back to main function?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include<math.h>

using namespace std;


void getAcorr(double *arr1[], double *arr2[], int size1, int size2, double *result[]); //defination for the autocorrelation using two pointer array of type double
double *p;

int main (void)
{
int size1, size2, i,j;
   double input;
   //int operation;
    double result[size1+size2-2], z[100];
	double x[]={1,2,3};
	double h[]={1,2,1};
	int m,n;
	
 //finding length of the given array

  m= (sizeof(x)/sizeof(*x));
  
  n= (sizeof(h)/sizeof(*h));
  
  cout << "length of array=" << m << endl;
  
  cout << "length of array=" << n << endl;
  
  //operation to be selected using switch condition
  
			getAcorr(x, x, m,n,result); 
			
	                      
	return 0;	
 }  
 
 
 //operatin to be carried out in the autocorreletion
 
 void getAcorr(double *arr1[], double *arr2[], int size1, int size2, double *result[])
 {
 	int i,j;
 	int m=size1,n=size2;
 	 double h[size2];
 	 double x[size1];
 	double z[100];
 	
 	// reversing one the vector before its correletion i.e x[size1]
 	
 	              for(i=0,j=size1-1; i<size1; i++,j--)
	                {
                     z[i]=x[j];
                   
       	              cout <<" reverse vector's elements are=\n" << i <<' ' << z[i] << endl;
	                }  
	                
	                
   // convolving reversed array with other i.e x[size1]*z[size1] to get its auto correletion
   
                       for(i=0;i<=m+n-2;i++)
                        {
                           result[i]=0;                                  
                             for(j=0;j<=i;j++)
                             {
                               result[i]=result[i]+(z[i-j]*x[j]);
                                
                             }
                               cout<< "auto corelation sequences are= " << i << " = " << result[i] << endl;
                        }                     
                    
 }
its my piece of program which i had done after getting suggestions but still i m getting error



[Error] cannot convert 'double*' to 'double**' for argument '1' to 'void getAcorr(double**, double**, int, int, double**)'

[Error] invalid operands of types 'double*' and 'double' to binary 'operator+'
Last edited on
how can i pass a pointer array to function and call it back to main function?

Why do you need to pass an array of pointers?

Line 32:
[Error] cannot convert 'double*' to 'double**' for argument '1' to 'void getAcorr(double**, double**, int, int, double**)'

You try to call a function that takes an array of pointers with a parameter that is an array of doubles.

Line 66:
[Error] invalid operands of types 'double*' and 'double' to binary 'operator+'

An element in 'result' is a pointer. Elements in x and z are doubles and thus their product is a double.

Line 15:
The size of result is undefined, because the values of size1 and size2 have not been set.

Why does the main variables i, j, z, input? They are not used. Why both m and size1?

Line 44. Why the unnecessary copies m an n of the size1 and size2?
Line 45. h is not used.

Lines 45-47: C++ standard does not support array size that is not known at compilation time.

Line 53: The x has not been initialized, so its content is undefined.

Line 41: The parameters arr1 and arr2 are not used in this function.

Line 66: The j will reach the value of i. The value of i will reach size1+size2-2. The x has only size1 elements. An out of range error.

Lines 55 and 69: A function should have single task. Here it should be a computation. Do output elsewhere. (It is useful for debugging purposes to occasionally print info, but we are not that far yet.)


I have to ask again: What is the exact (mathematical) formula for computing Acorr?
Topic archived. No new replies allowed.