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?

  • 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](http://stackoverflow.com/q/1461432/509868) for details. – anatolyg Nov 03 '15 at 18:42
  • I know that it works, but I have to make 2 versions of it –  Nov 03 '15 at 18:43

3 Answers3

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

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
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);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You are missing parentheses when dereferencing arrName

Instead of

*arrName + x>max

You should

*(arrName + x)>max
vordhosbn
  • 333
  • 4
  • 16