1

I created a function which takes 2 parameters (Name of the array, Size of the array) and what I did was take the max element of the array minus the minimum. But what i want to do now is create the same function using pointers, but i always get a result which is 0 for some reason. Here's the code:

#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;

const void pntArray(int arrName[], unsigned sizeOfArray);//The first function which takes the size of the array and the array name and prints the sub of the max-min
void pntTheArray(int *(arrName), unsigned sizeOfArray);//Version of the first function, using pointers

int main()
{
    int someArr[3]={7,2,6};
    int pointerArray[5]={7,6,5,4,10};
    pntArray(someArr, 3);//Example of the function without the pointers
    pntTheArray(someArr, 3);//Example of the function with the pointers
}


 void pntTheArray(int *arrName, unsigned sizeOfArray){
int max = 0;
int min = 999999;
for (int x = 0;x<sizeOfArray;x++){
    if (*arrName+x>max){
        max  = *arrName;
    }
    if(*arrName+x<min){
        min = *arrName;
    }
}
cout<<"The maximum element minus the the minimum element is: "<<(unsigned)(max-min)<<endl;
}

const void pntArray(int arrName[], unsigned sizeOfArray){
    int max=0;
    int min = 999999;
    for (int x = 0;x<sizeOfArray;x++){
        if(arrName[x]>max){
            max = arrName[x];
        }
        if (arrName[x]<min){
            min = arrName[x];
        }
    }cout<<"The maximum element minus the the minimum element is: "<<(unsigned)(max-min)<<endl;}

I want to make a version of the first array, basically. So where's the mistake that i'm making in order to get only 0 as a result for the second function?

2
  • BTW int arrName[] means exactly the same as int *arrName when it appears as a function's parameter, so you have absolutely no reason to "change" your code from "array" to "pointer" - both versions work with pointers. See here for details. Commented Nov 3, 2015 at 18:42
  • I know that it works, but I have to make 2 versions of it Commented Nov 3, 2015 at 18:43

3 Answers 3

2

The expression you are using in the if statement:

*arrName+x>max

is equivalent to:

 arrName[0]+x > max

That's not what you need. You need to use:

 *(arrName+x) > max

or

 arrName[x] > max

You'll need to change a few more lines that suffer from the same error.

Change:

if (*arrName+x>max){
    max  = *arrName;
}
if(*arrName+x<min){
    min = *arrName;
}

to

if (*(arrName+x) > max){
    max  = *(arrName+x);
}
if(*(arrName+x) < min){
    min = *(arrName+x);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I still get 0 as a result :/
I added it both in the max and in the min expression under it. Thanks for the help! ^^
2

This does not do what you think it does:

 if (*arrName+x>max) {

The * (deereference operation) has a higher precedence than the + operation. See operator precedence. So what you are really doing is this:

 if ( (*arrName) + x > max) {

You should be using:

if (*(arrName + x) > max) {

or

if (arrName[x] > max) {

The reason you get zero is because you do it in several places. Try this:

void pntTheArray(int *arrName, unsigned sizeOfArray){
    int max = 0, min = 999999;
    for (int x = 0;x<sizeOfArray;x++){
        cout << "i: " << x << " val: " << *(arrName+x) << "\n";
        if (*(arrName+x)>max){
            max  = *(arrName+x); // Note you weren't setting this correctly either!!!
        }
        if(*(arrName+x)<min){
            min = *(arrName+x); // Note you weren't setting this correctly either!!!
        }
    }
    cout<<"The maximum element minus the the minimum element is: "<<(unsigned)(max-min)<<endl;
}

Live example

Comments

0

You are missing parentheses when dereferencing arrName

Instead of

*arrName + x>max

You should

*(arrName + x)>max

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.