#Write a 'void' function shiftLeft(a,b,c) that takes 3 numbers and shifts them circularly

34 messages · Page 1 of 1 (latest)

reef canopy
#

so I have this question in my homework which I don't really understand.
This is the main function that was given:

int main() {
    int a = 111;
    int b = 32;
    int c = 89;
    for(int i = 0; i < 10; i++){
        shiftLeft(a,b,c);
        cout << "(a,b,c) == (" << a << "," << b << "," << c << ")" << endl;
    }

    return 0;
}

and this is what I could only think off (it's wrong, but maybe it helps)

#include <array>
void shiftLeft(int a, int b, int c) {
    int shift=3;
    int x=shift-1;
    int temp=array[3];
    while (x >= 0 && shift >= 0) {
        array[shift]=array[x];
        x++;
        shift++;
    }
}
mossy stagBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

flat shore
#

So first, you need probably need to take the three arguments by reference (with an & in the argument definitions)
Basically this means that changes to the arguments also change the variables passed to it

#

;compile

#include <iostream>

void passByValue(int a) {
    a = 5;
}
void passByReference(int& a) {
    a = 5;
}

int main() {
    int c = 10;
    std::cout << c << '\n';
    passByValue(c);
    std::cout << c << '\n';
    passByReference(c);
    std::cout << c << '\n';
}
potent grailBOT
#
Program Output
10
10
5
flat shore
#

Second, you probably don't need to use an array, nor a for loop

reef canopy
flat shore
#

the main was just to be used as an example of pass by reference

#

notice how in one of the function calls, the value of c changed, but in the other it didn't

reef canopy
#

yeah, so that means I have to put the & in the arguments by a, b and c?

flat shore
#

Yep

reef canopy
#

like this, right

void shiftLeft(int& a, int& b, int& c) {
flat shore
#

yep

#

side question, have you written a swap function before?

reef canopy
#

yeah I have that in my book in front of me. That's what I tried to do first, but without success

flat shore
#

so this is just a slightly harder version of the swap function

#

So using the pass by reference, try writing a swap function

reef canopy
#

uhm

&a = &b;
&b = &c;
&c = &a;

I think, sorry if it's wrong. We started learning about this just a few weeks ago

flat shore
#

so for references, you only need to but the & on the argument, afterwords, you use them like any other variable

#

also you are close

#

but the issue is after
a = b
the original value of a is lost so
c = a will have c get the old value of b

reef canopy
#

thats- very confusing

#

i understand what you mean, i just dont understand how I could fix that

flat shore
#

All you need is one extra variable to hold the old value of a

reef canopy
#

so:

x = a;
a = b;
b = c;
c = a;
``` ?
flat shore
#

so close

reef canopy
#

or a = x?

flat shore
#

the first 3 lines are correct

reef canopy
#

hmmm

flat shore
#

you want c to hold the old value of a correct?

reef canopy
#

so c = x?

flat shore
#

yep

reef canopy
#

thank you so much!

#

!solved