-1

I am trying to move from arrays to vectors in cpp for problem-solving and its overall benefits. I am facing some issues here even though this logic works on arrays.

#include <iostream>
#include <vector>      
using namespace std;

void PrintArray(vector<int> v) { // O(n)
    for (int i=0; i<v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}

void LF1(vector<int> A) { // O(n)
    int temp = A[0],i;
    for (i=0; i<A.size()-1; i++)
        A.at(i) = A.at(i+1);
    A.at(i)=temp;
    // PrintArray(A); <-- shows updated array here
}

void LF(vector<int> A, int d) {
    d = d % (A.size());
    cout << "d%n: " << d << endl;
    for (int j=0; j<d; j++)
        LF1(A);
    PrintArray(A);
}

int main(int argc, char const *argv[]) {    
    vector<int> A;
    int d;
    for(int i=1; i<6; i++)
        A.push_back(i);
    PrintArray(A);
    cout << "Enter number of Left rotations to perform : ";
    cin >> d;
    LF(A,d);
    return 0;
}

Problem 1: When I am calling LF1 inside of LF it returns the same array without rotating but when I write the code for LF1 inside of LF it seems to rotate.

Problem 2: The PrintArray() prints the rotated array only when called from LF1() or just immediately after its code when written (instead of calling LF1()) in LF() when causes it to print the array d times. Where d is the required rotations.

Tusharj
  • 21
  • 1
  • 5
  • 6
    You are passing parameters by value. The function operates on a copy of the argument; the argument remains unchanged. Your functions are elaborate no-ops. Read about passing by reference in your favorite C++ textbook. – Igor Tandetnik Dec 04 '18 at 00:36
  • Welcome to StackOverflow. Your question isn't a bad one (I don't think)...and a reasonable confusion given C's [unusual array-decaying-behavior](https://stackoverflow.com/questions/19646512/passing-an-array-by-reference-in-c). But it's still a fairly *basic* question... so downvoters likely feel it would be better to read a foundational book than to go through trial-and-error on StackOverflow. There's some truth to that, so don't be afraid to work through a book, [there are some good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – HostileFork says dont trust SE Dec 05 '18 at 00:19

1 Answers1

1

Regarding what you're doing wrong...you are passing the vectors by value. You don't expect changes to an integer to affect it in the caller when you pass it as a value...

void SomeFunction(int i) {
    i = i + 1;
    printf("Inside SomeFunction %d\n", i); // changed, 11
}

int i = 10;
SomeFunction(i);
printf("Outside SomeFunction %d\n", i); // unchanged, 10

...if you wanted to see a change, you would have to pass a pointer, such as int *pi, and then update it as *pi = *pi + 1;

The same principle applies to vectors and other C++ classes. If you just pass it as a value, the whole vector is copied. (Well, if it needs to be, a temporary could just be reused). But for now think of it as being copied: just as there's a difference between passing an integer and a pointer-to-an-integer, there's a difference between a vector and a pointer-to-a-vector.

You could pass a pointer to the vector if you intend to change it...or... C++ offers another tool called the reference, where references are very much like pointers but with a few differences. If you just changed your arguments to vector<int> &A then your code should work, because the arrays would be "passed by reference" instead of getting copied when they are "passed by value", so changes would take effect. If you don't want a function to need to be able to modify an array but still want to avoid the copy, pass by const reference, e.g. const vector<int> &A (e.g. this is what your PrintArray() should use).

You might not want to get too hung up on the details of references for now, other than thinking of it as a "convenient kind of pointer where you don't have to put the * on all the places you want to dereference". But in case you want to know more specifics:

What are the differences between a pointer variable and a reference variable in C++?

I am facing some issues here even though this logic works on arrays.

And this is probably the source of your confusion. Which comes from the fact that C-style arrays decay into pointers under the hood:

Passing an Array by reference in C

I think that's something that it's reasonable to be confused by, given that other types (such as integers and vectors) don't. It's just a quirk of C, that C++ inherited. So when C++11 wanted to clean that up, a wrapper class called std::array was introduced:

https://embeddedartistry.com/blog/2017/6/28/an-introduction-to-stdarray https://en.cppreference.com/w/cpp/container/array

But C++ also has an algorithm to do rotation...

So if you want to see a good example of how this would be done, it's a place to start:

#include <vector>
#include <iostream>
#include <algorithm>

int main() {
    std::vector<int> v{1, 2, 3, 4}; 
    std::rotate(v.begin(), v.begin() + 1, v.end());
    for (auto &i : v)
        std::cout << i << " ";
    std::cout << "\n";
}

That will get you 2 3 4 1. The documentation has other examples, read through:

https://en.cppreference.com/w/cpp/algorithm/rotate

  • https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in This cleared the problem. – Tusharj Dec 06 '18 at 05:08