-1

I tried reversing a vector through a divide et impera algorithm. Here's my code:

#include <iostream>
#include <vector>

using namespace std;

//prototype
void Reverse (std::vector<int> &v, int left, int right);

//helper function
void Switch(int &x, int &y)
{
int aux = x;
x = y;
y = aux;
}


int main()
{
vector <int> v;

int n; cout << "n="; cin >> n;
for (int i = 0; i < n; i++){
    cout << "v[" << i <<"]=";
    int input;
    cin >> input;

    v.push_back(input);
}

Reverse(v, 0, v.size()-1);

for (int i = 0; i < n; i++)
    cout << v[i] << " ";

return 0;
}


void Reverse (std::vector<int> &v, int left, int right)
{
int mid = (left + right) / 2;

if (left == right + 1){
    Switch(v[left], v[right]);
}
else{
    Reverse(v, left, mid-1);
    Reverse(v, mid, right);
}
}

It probably gives me an infinite loop because my program never returns when I compile it and I get a not responding error. What's the issue with it? Is it because I only switch the two elements at the base case and not two entire vectors?

Lastrevio2
  • 51
  • 6
  • You have been asking about this for the last few days, what makes the questions different from each other? Don't repost the same question over and over. – Some programmer dude Sep 18 '19 at 07:52
  • 1
    What happens if you call `Reverse` with `left=4` and `right=5` ? What happens if `left == right` ? If you step through your code with a debugger (or even just add trace printing statements to `Reverse`) you will see why it enters that infinite loop. – Botje Sep 18 '19 at 07:59
  • 1
    You may try it with a small input and single-step debugging to find out what's going on. At best, you note on paper first what you expect to happen and then check whether it really happens as expected. – Scheff's Cat Sep 18 '19 at 08:00
  • 1
    @Someprogrammerdude Unless the questions you think of were deleted, their similar questions did involve divide-and-conquer, but for different problems. That said, the resistance to debugging their own programs is really frustrating. – Max Langhof Sep 18 '19 at 08:03
  • 1
    Have you touched a debugger yet? I don't know what context you are getting all these questions from (divide-and-conquer is a terrible approach for most of them), but having stackoverflow debug your programs for you is entirely unsustainable. You have to learn how to do it yourself! – Max Langhof Sep 18 '19 at 08:05
  • 1
    You can't reverse a vector by reversing its two halves. – molbdnilo Sep 18 '19 at 08:32

1 Answers1

0

You should debug your program rather than ask the community to fix it for you.

The easiest way to do it as you seems to already know cout is to add more printing statement with the state of your program. Later you shall likely learn how to use a debugger with your IDE of choice.

Please notice that I didn't try to fix you program, but just added cout statements to help understand what is going on:

void Reverse (std::vector<int> &v, int left, int right)
{
int mid = (left + right) / 2;
cout << "Reverse called with left=" << left << " right=" << " mid=" << mid << endl;


if (left == right + 1){
    cout << "Switching " << endl;
    Switch(v[left], v[right]);
}
else{
    cout << "Recursive call " << endl;
    Reverse(v, left, mid-1);
    Reverse(v, mid, right);
}
}
Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18