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

This is a discussion on Homework Problem. Just Need Direction / Clue on How To Approach It. That is All. within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I was given this assignement to do. I am not asking anyone to do this for me at ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    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.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Yes, references will help you.

    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
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    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. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Homework Problem
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 04:43 AM
  2. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 08:21 AM
  3. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  4. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 06:32 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21