A pointer Problem

Hi i wrote some code to calculate the max value of an array or something like this but i couldnt find the error in the program but i think the pointer created in main function doesnt send the address to the fuction .Could you help me with this?
CODE IS HERE
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
#include <iostream>
using namespace std;

double max(double *x[],int n){
       double *maks;
       maks = *(&x[0]);
       cout << "maks " << maks << endl;
       cout << *(&x[2]) << endl;
       cout << *maks << endl;
       for(int i=0;i<n;i++) {
               if(*maks < **(&x[i])) maks = *(&x[i]);
               
               }
       return *maks;
       }

int main(){
    int n=3;
    double *ptr;
    
    ptr = new double[n];
    
    for(int i=0;i<n;i++) cin >> *(ptr+i);
    cout << *(ptr+1) << endl;
    double v;
    v = max(&ptr,n);
    cout << "max = " << v << endl;
    
    delete [](ptr);
    
    
    system("pause");
    return 0;
    }
 
Last edited on
You really should have a look at the pointer chapter again.

Fixed version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

double max(double* x,int n)
{
  double maxVal=x[0];
  for(int i=1;i<n;i++)if (x[i]>maxVal)maxVal=x[i];
  return maxVal;
}

int main()
{
  const int n=3;
  double vals[n];
  for (int i=0;i<n;i++)cin >> vals[i];
  cout << "max = " << max(vals,n) << endl;
}
Last edited on
Thank you for your advice but i can write code like this you wrote.However i am expected to write a program including a function which is in the form like double max(double *x[],int n).I wonder whether a function doing the same work can be wroten in thsi type.Thanks for your help...
Is there anyone who can help me with it??
Is there anyone there???Please help me with thsi...


For your parameter, double *x[], you can do:

1
2
3
4
5
6
7
8
9

double d = *(x[0]); // because x[0] is a pointer to a double

// So to visit every double you would need to dereference every double pointer in the array:

for(int i = 0; i < n; ++i)
{
    if(*(x[i]) > max) { max = *(x[i]); }
}


So all you need to do is figure how to create such an array.

Remember you need an array of double pointers. Pointers to type double:

 
double* x[size];


So you need to allocate memory to every pointer in the array in which to store your doubles:

 
x[0] = new double; // allocate a double to element 0 of the double pointer array. 


And you need to delete all those pointers afterwards:

 
delete x[0]; // delete element 0 of the double pointer array 
Last edited on
Thanks for your support but this isnt the answer of my question because i must use a function like
double max(double *x[],int n){ } .anyway.And Could you write your own code in the way of solution you describe??
Last edited on
I have explained how to use the kind of array you are expected to use but I am not going to do your assignement for you.

You have to learn these concepts for yourself.

So you try to write your code using the information I provided and I wall do my best to explain if you make a mistake.
I think your code for double max(double *x[],int n){ } works fine.

There is a problem in the way you create your array to sent to that function:

1
2
3
    double *ptr; // pointer to array of type double
    
    ptr = new double[n]; // array of type double 


That creates an array of type double. But the function max() expects an array of type double*
I edited the code there is still a mistake...
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
#include <iostream>
using namespace std;

double max(double *x[],int n){
   double max = *(x[0]); 
   for(int i = 0; i < n; ++i) 
   {
    if(*(x[i]) > max) { max = *(x[i]); }
    }
    return max;
    }

int main(){
    int n=3;
    double *ptr; 
    
    ptr = new double[n]; 
    cout << max(ptr,n) << endl;
    
    cin.ignore(2);
    return 0;
    }
 

Thsi is what i am trying to tell you...
The problem is that your function is like this: double max(double *x[],int n){ } :

So you need to be creating one of these:

 
double* x[]; // Array of double pointers OR array of double*  


BUT you are creating one of these:

 
double ptr[]; // Array of double 


Because that is conceptually the same as:

 
double* ptr; // Array of double 


So you need one of these:


 
double* ptr[]; // Array of double* 


Or one of these:

 
double** ptr; // Array of double* 

I recommend you do this:

 
double* ptr[n]; // Array of n double pointers 


And for each one allocate a double using new a bit like this:

 
double* ptr[0] = new double; // allocate a new double for the first pointer in the array 

Thanks this solved my problem exactly...By the way it is not a homework assignment.
systempause wrote:
By the way it is not a homework assignment.


Heh, so you're just torturing yourself for the fun of it? ;o)
Yep.There are several methods to write a program doing the same task but i want to explore new ones
Last edited on
Topic archived. No new replies allowed.