Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Array with given sentinel value

Tags:

c++

FYI: I am new to programming.

I have an arraysize of 10 and the sentinel value is 0.

My original array is [1 2 3] ( user input) but my reverse is [0 0 0 0 0 0 0 3 2 1].

I need help to make my reverse array [3 2 1].

Here is my code:

    int temp;
    for (int i = 0; i < arraysize/2; i++) 
    {         
     temp = array[arraysize-1-i];
     array[arraysize - i - 1] = array[i];
     array[i] = temp;
    }

    cout << "The reverse array: ";
    for (int i = 0; i < arraysize; i++)
     cout << array[i]<< ' ';
    cout << endl;
like image 918
Siri Avatar asked Feb 18 '26 13:02

Siri


2 Answers

Just use the standard library algorithms

auto end = std::find(std::begin(array),std::end(array),0);
std::reverse(std::begin(array),end);
//And if you only want to print the non-zero values:
size_t effectiveArraySize = end - std::begin(array);

If the fixed size array is not part of your requirement, you should put your user data in a vector that automaticaly grows as large as you need, instead of using an array that might turn out to be too small:

std::vector<int> v;
while(true) {
    int t;
    cin >> t;
    if (t == 0) {
       break;
    }
    v.push_back(t);       
}
std::reverse(v.begin(),v.end());

That way, you don't have any sentinel values in your array / vector to begin with.

like image 115
MikeMB Avatar answered Feb 21 '26 03:02

MikeMB


Note: Using the respective functions from the STL (std::reverse and std::find) is better, I was just guessing that you are bound to implement this on your own.


Step one: Write a proper reverse function. One that takes (a pointer to) the beginning as well as (a pointer to) the end of the range that should be reversed.

Step two: Write a function to find (the first position of) your sentinel in an array (given via beginning and end, again)

Step three: Connect the two: Reverse from the beginning to the position of your sentinel.

Example without templates:

void reverse(int * from, int * to) {
  while ((to - from) > 1) {
    --to;
    int temp = *from;
    *from = *to;
    *to = temp;
    ++from;
  }
}

int const * find(int const * from,
                 int const * const to,
                 int const value) {
  while ((from != to) && (*from != value)) {
    ++from;
  }
  return from;
}

void reverse_until (int * const from,
                                  int * const to,
                                  int const sentinel) {
  int const * const position_sentinel = find(from, to, sentinel);
  reverse(from, from + (position_sentinel - from));
  // return the sentinel position from this function
  // if you want only the reversed part
}

Tested with:

int main() {
  int test[10];
  for (size_t i = 0; i < 10; ++i) {
    test [i] = i + 1;
  }
  reverse_until (test, test + 10, 6);
  copy(test, test + 10, ostream_iterator<int>{cout, " "});
  return 0;
}

(live here)

like image 45
Daniel Jour Avatar answered Feb 21 '26 03:02

Daniel Jour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!