Thread: Homework Problem. Just Need Direction / Clue on How To Approach It. That is All.

1. Homework Problem. Just Need Direction / Clue on How To Approach It. That is All.

Hello everyone.

I was given this assignement to do. I am not asking anyone to do this for me at all. I am just looking for some direction on how to solve it. That is all. Any hints are greatly appreciative.

The assignment.

Write a program which includes a definition of the function SHIFT(), described as follows: SHIFT() shifts the stored values of five character variables in a circular fashion. The program initializes the variables c1,c2,c3,c4 and c5 to the values 'A', 'B', 'C', 'D', and 'E' respectively. Then the function call

SHIFT(c1,c2,c3,c4,c5) ;

causes the varaibles c1, c2, ...c5 to have the values 'B', 'C', 'D', 'E' and 'A', respectively.

Your program should call SHIFT() five times, printing out, in turn, BCDEA, CDEAB, DEABC, EABCD and ABCDE. Include SHIFT() in a program containing the following main():

Code:
```int main()
{

char c1 = 'A', c2 = 'B', c3 = 'C', c4 = 'D', c5 = 'E';

for (int i = 1; i <= 5; i++){
SHIFT(c1, c2, c3, c4, c5);
cout << c1 << c2 << c3 << c4 << c5 << ' ';
}

return 0;
}```

Ok, I am vairly confident with my C++ skills and understand how functions work and all and even how to code them.

Without using a computer the way I would do this on paper is that I have is that I will record the the initial order of the letters A,B,C,D, E and each time through the loop I will just shift them by taking A and moving at the end of the list to be after E. Then, the next round would be B,C,D,E,A and then I would repeat by taking B and putting it at the end which would be after A.

I guess the reason I am posting this message here is that I am asking what C++ datatypes or functions can I use to help create these shift behavior? Would using reference ( the & ) assist me?

Again, I am not asking for anyone to do this for me, and I don't want anyone too. I just need some direction on how to approach this problem and that is all.

Thanks for your time and any hints given.

As to design the algorithm, think about what you need to do to make shift work the first time (shifting ABCDE to be BCDEA). What do you need to do with the variables and so on to make this happend. When you have come up with that solution the rest will be obvious

3. Step 1 is to come up with the prototype for the function. You will need to modify the values of all five variables passed to the function, so that should give you a hint as to what the function parameters will be.

>> Would using reference ( the & ) assist me?
Probably.

Once you've got the return type (if any) and function parameter types figured out, you can actually leave the function empty and compile and run the program. Obviously the characters won't be shifted, but you always want to compile as you go to catch errors early.

Then you have to figure out how to actually do the shifting.

>> I will record the the initial order of the letters A,B,C,D,E.
This might not be necessary, since it doesn't matter what the inital order is, as long as you rotate what is passed to the function.

>> each time through the loop I will just shift them by taking A and moving at the end of the list to be after E.
Think of these in terms of c1, c2, ..., c5 instead of the actual letters. It is more general that way, and the function should not need to know which characters are in which place.

Lastly, you've got to figure out how to actually move the value from c1 to the c5 spot without actually losing the value that was in c5.

4. I guess the reason I am posting this message here is that I am asking what C++ datatypes or functions can I use to help create these shift behavior?
Yes, an array or vector would be a good data type to use along with a for-loop to traverse it, but even better would be a queue and the functions front() to retrieve the first element, pop() to remove the first element, and push() to add the first element onto the end, but alas your assignment is fairly specific and precludes their use.

5. Thanks everyone taking your time out by posting useful suggestions.

I will use your recommendations and do my best to figure it out. From re-reading what is posted I now have more confidence that I am able to do this.

I guess when developing or learning code it is best to throw ideas off each other. It really does work.

Again, thanks.

6. If your teacher/lecturer allows you to use C++ standard functions to do this, consider using std::deque. A linked list (std::list) is also helpful. Otherwise, you'll have to do it manually, shifting each element to the left/right one by one.

If you only want to display the shifted values and not shift them in memory, there's also a quick trick to it

7. Normally I would had recommended using a deque too or some other stl container, however considering that the function takes 5 seperate parameters the easiest way is to just work with them( you'd spend as much effort just pushing_back the values into a container). Put the first variable in a temp. copy 2 to1, 3 to 2, 4 to 3, 5 to 4 and temp to 5 and return. Make sure you are passing by reference.

8. You can make it with a struct with the variables and copies of them. Use 3 methods:

initiate() = give c1, c2, ... the standard values
show_all() = cout all c1, c2, ... variables
SHIFT() = change the values with the use of the copies

9. Wowsers!

I never expected this much assistance on this.

I have a previous programming background in PERL and understand abstract data types such as queues and the like.

My teacher never said we can use these ADT. All he said is just do the program and make sure it works.

Even though I have been programming for some time now the answer to a problem doesn't always stick out to me. I just need time to think about it or need people to bounce ideas off of each other. You know actually for some reason I always have problems what I consider the easy problems such as this one. I think I overthink them too much.

Am I the only one that experiences that problem or is it common?