0

My code is supposed to remove a specified value and shift the array. When I run the code it prints an address instead of the contents of the array. What is wrong with my code?

using namespace std;
void arrayShift(int arr[], int length, int value) {
  for(int i = 0; i<length; i++) {
    if(arr[i] == value) {
      for (int k = i; k<length-1 ; k++) {
        arr[k] = arr[k+1];
      }
      arr[length-1] = 0;
      i--;
    }
  }
  cout << arr;
}

int main() {
  int inputarr[]={9,8, 9, 9, 9, 9, 6};
  int length = 7;
  int value = 9;
  arrayShift(inputarr,length,value);
}
Yakk - Adam Nevraumont
  • 250,370
  • 26
  • 305
  • 497
Andrew
  • 15
  • 3

1 Answers1

3

The line

cout << arr;

Display an address because cout doesn't display array directly.

You should use a for to display all your values. Something like that:

for(int i = 0 ; i < length ; i++)
    cout << arr[i];

However you really should put the output (for with cout) in the main function.

AMDG
  • 945
  • 1
  • 9
  • 22
  • My code will still print the address when I make the changes. – Andrew Mar 13 '15 at 15:50
  • cpp.sh/7rbv There seems to be an error with the way I am calling it. Why cannot I not just cout my function? – Andrew Mar 13 '15 at 15:57
  • Your function return void, i.e. nothing. cout will try to display what your function will return, so cout will try to display nothing. – AMDG Mar 13 '15 at 16:00
  • What would be the correct way of calling the function then? – Andrew Mar 13 '15 at 16:02
  • The correct way is to display your array, not what your function return. See : http://cpp.sh/7mld – AMDG Mar 13 '15 at 16:03