+ Reply to Thread
Results 1 to 7 of 7

Thread: Yet another C++ question     submit to reddit submit to twitter

  1. #1
    D. Ring
    Join Date
    Nov 2006
    Posts
    4,613
    BG Level
    7
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Proudmoore

    Yet another C++ question

    Hello everyone;

    I've got a project that's asking me to input binary numbers and create a bunch of functions that'll print those binary, reverse them, shift to left and right and convert it into decimal.

    I've managed to make the program print and reverse the numbers but I am a little lost on "shifting" the binary.

    3. ShiftLeft
    a. Shift the numbers in the array left (mimicking the shift left operator). Note: You will not use the
    << operator to do this. Remember to pad with 0’s. Only shift one spot per function call.
    4. ShiftRight
    a. Shift the numbers in the array right (mimicking the shift right operator). Note: You will not use
    the >> operator to do this. Remember to pad with 0’s. Only shift one spot per function call.


    I emailed prof. and she said;

    "We are mimicking
    one integer number by an 8 element array where each element holds a binary digit
    making up that number... so if want to shift left, we simply need to move each
    element in the array one space to the left."

    So I guess calling the "shift left" function will turn 11001100 into 10011001 (or 567890 to 678905).

    I wrote this ;

    int left = 0;
    int right = s-1;
    while(left < right){
    int temp = ar[left];
    ar[left] = ar[right];
    ar[right] = temp;
    left++;
    right--;
    }

    for reverse function and it works pretty good. Does anybody have an idea on how to handle this "shift left" thingy? I tried to store everything in temp and move them to left but...well, it failed. I suck at this thing :/

    Thanks in advance for replies.

  2. #2
    Salvage Bans
    Join Date
    Jun 2007
    Posts
    891
    BG Level
    5

    Re: Yet another C++ question

    Depends if he wants it to wrap or not, sounds like an in class assignment I did years ago lol.

    Does he want u to code it like 11100000 to 11000000 or 11100000 to 11000001?

    Easiest way probably for the former is just to allocate a temp array (with 8 slots), go though the original array from the 1-7, and tack a 0 at the end of the array that is being shifted left, if it's the latter, throw the original[0] into a temp variable and just tack it at the end.

  3. #3
    D. Ring
    Join Date
    Nov 2006
    Posts
    4,613
    BG Level
    7
    FFXI Server
    Quetzalcoatl
    WoW Realm
    Proudmoore

    Re: Yet another C++ question

    Both. She is expecting a menu like;

    1. Input Binary
    2. Print Binary
    3. ShiftLeft (one step to the left each time you call the function)
    4. Shift Right (one step to the right this time)
    5. Reverse
    6. Convert

    I coded menu, input, print and reverse but can't figure shift left and right ;_;

  4. #4
    Salvage Bans
    Join Date
    Jun 2007
    Posts
    891
    BG Level
    5

    Re: Yet another C++ question

    I'm assuming that she expects you to just use "8 bit" binary data, and not have it compatable with any kind of data. I expect she also just wants you to shift it like the following.

    11111111 to 11111110 for shift left

    and

    11111111 to 01111111 for shift right

    which is essentially the >> and << operators when it comes to binary data. So essentially, what I believe basing off these assumptions, is that you just really have to allocate and populate a temp array and translate it to the original array, making sure that you tack on 0s where it deems necessary while not throwing numbers outside the array size.

  5. #5
    Two Bards, One Cup
    Join Date
    Oct 2006
    Posts
    111
    BG Level
    3
    FFXI Server
    Ragnarok

    Re: Yet another C++ question

    Gonna pseudocode this so I'm not blatantly giving you a solution. Only helping with shift right.

    Shift Right:

    Code:
    let x = right-most index in array
    
    loop until x = 2nd left-most index in array
       copy value in array[x -1] into array[x]
       x--;
    end loop
    
    set array[0] = 0

  6. #6
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,223
    BG Level
    7

    Re: Yet another C++ question

    Quote Originally Posted by Mayareira
    So I guess calling the "shift left" function will turn 11001100 into 10011001 (or 567890 to 678905).
    That would be if it was a wrapping shift or padding with 1s, but you said it's padding with 0s, so that means 11001100 would shift left into 10011000, or shift right into 01100110. I don't remember whether that's a logical shift or an arithmetic shift.

    As for implementation, Jayhawk appears to have the correct approach.

  7. #7
    E. Body
    Join Date
    Jun 2006
    Posts
    2,182
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    Re: Yet another C++ question

    The correct approach is tell that bitch to get back in the kitchen and stop pretending she knows how to program.

Similar Threads

  1. yet another PS3 question - this time related to linux
    By BarthelloSylph in forum General Discussion
    Replies: 25
    Last Post: 2008-06-24, 23:17
  2. yet another ytmnd..
    By Daahan in forum General Discussion
    Replies: 13
    Last Post: 2006-01-24, 14:45
  3. [136] The dawn of yet another bad filler arc
    By Razz in forum General Discussion
    Replies: 7
    Last Post: 2005-05-26, 16:05
  4. Yet another fond childhood memory ruined
    By Septimus in forum General Discussion
    Replies: 1
    Last Post: 2005-02-18, 11:25