C++ Write shuffle function to shuffle an array - exercise

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    C++ Write shuffle function to shuffle an array - exercise

    I`m trying to write a shuffle function to shuffle a sorted array with an even number of elements but it won`t print past the first 2 elements as shown below.
    When i print the temp[] array it only prints the first 2 elements also. So i presume the loop is is stopping after 2 iterations.
    Can anyone see my error?.

    Code:
    #include<iostream>
    using namespace std;
    void print(int a[],int n);
    void shuffle(int a []);
    int main()
    {
    int a[8]={22,33,44,55,66,77,88,99};    
    cout<<"\nArray a[] is: ";
    print(a,8);    
    shuffle( a);
    cout<<"\nArray a[] is: ";
    print(a,8);
    cout<<"after shuffle";
    cout<<"\n\n\n";
    system("pause");
    return 0;   
    } 
    void shuffle(int a[])
    { int SIZE=8;
      int temp[8]={0};
      const int half=SIZE/2;
      for(int i=0;i< SIZE;i++)// i have tried using 'half' ILO 'SIZE' also deleting i++
          {   temp[2*i]=a[i];// and revising this line to temp[2*i++] to no avail
               temp[2*i+1]=a[half+i];
              
              for(i=0;i<SIZE;i++)
              a[i]=temp[i];
              
          }    
    }
    void print(int a[],int n)
    {
         for(int i=0;i<n;i++)
         cout<<a[i]<<" ";
    }
    /*This is the output i get.
    Array a[] is: 22 33 44 55 66 77 88 99
    Array a[] is: 22 66 0 0 0 0 0 0 after shuffle
    it should be
    Array a[] is: 22 66 33 77 44 88 55 99 after shuffle*/
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Okay, I'd go back to your algorithm here, I'm not entirely sure on what basis you are "shuffling" .

    What is the goal of a shuffle? Are you trying to get those values randomly assorted, or sorted in a specific fashion (ie, after a specific algorithm)? Or did I just totally miss your question?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Its meant to interleave the lower value elements between the four higher value ones as shown below "it should be".

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Okay, so it looks like you had in mind a mathematical representation of the shift? I'm seeing that the first one stays in its place, the second moves one places up, the third moves two places up, and the fourth moves three places up...

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Sorry I don`t know anything about `shift'
          I didn`t want to move anything.i wanted to copy the first element from a to temp position 0 (temp is initialized with 0`s) to replace the zero then the fifth element of a to temp position 1, then the third element from a to temp position 2 and so on...... so that the eighth and last element of a is copies to position 7 in temp.
          Then copy the temp array to a[].
          Also i would like to ask weaknessforcats a question about another matter but i don`t how to send him a private message. Do you know how to do this?
          Thanks for yor interest.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Well, I think you will want to be careful about PM'ing questions to Moderators.
            Originally posted by Posting Guidelines
            Do NOT PM questions to individual Experts, Moderators or Administrators

            This is not fair on them and we instruct our experts to ignore any such PMs completely, there are also some very good reasons why it is not a good idea:

            * You have no guarantee that the expert you have PM'd knows anything about the subject you are asking about.
            * You have no guarantee that the expert in question hasn't just gone on holiday for 4 weeks to Hawaii resulting in a delay to the answer to your question.
            * If you post the question in the forums then everyone gets to see it. This means that there is a far higher chance that someone who already knows the answer will see the question than if you just ask 1 person.
            * It's good to spread questions and knowledge. Remember if you have a question then it is likely that someone else in the world has a very similar question. By asking your question in the open forum and getting a reply there the information is available for everyone to read and learn from.
            However, as you seem to not desire my assistance, I will leave you with: your issue in your for loop is that you are not correctly calculating the space you want to move the array into. You are currently calculating the index * 2 or index * 2 + 1.

            Oh, and as you know this is going to run 4 times, why not hardcode 4 in there? At least until you get it working, then play around with setting that in the beginning or through another variable. Then you'll be sure it runs 4 times.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              I overlooked the requirement on personal messages and will in future cease and desist. Am not sure how u concluded that I didn`t want your help and emphasise that I do. Will play round with your suggestion on hard coding but can`t see how it can be anything less than 8. Will let you know.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                You want to move 4 elements. You can move all 8 if you want, but the first won't move, and the last won't move, so it's rather pointless to try to move them. (I did get it confused with vectors, you will probably want to iterate 8 times)

                What I was attempting to say above was that there is a mathematical relation to each array index, and how much you want it to move in the array. As soon as you can figure out that mathematical relationship, you can code your for loop. The key here is the modulus operator, % . You said you wanted the first half of the elements to become every other element. How do you figure out which place it is in? Obviously, you look at the index, but what about the index tells you that you want to put one of the first four elements in there?

                As we are dealing with arrays, the first element is actually at number 0, and then you will want your second element to be at number 2. Your third at number 4, and so on. There is a pattern there. There's also a pattern where you want your last four - you want the first of the last four in array[1] spot, the second in the array[3] spot, and so on ending at array[7].

                Are you familiar with the modulus operator and what it does?

                And you show that you do not want a certain person's help when you ask them how to get another person to help on an issue the first person is already attempting to help you with.

                Comment

                • whodgson
                  Contributor
                  • Jan 2007
                  • 542

                  #9
                  I said that I wanted to get in touch with the person "on another matter". That means nothing to do with the matter (read thread) which you were helping me with. I have also taken your warning in relation to messaging guide lines on board and scrubbed my question your QED

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by whodgson
                    I said that I wanted to get in touch with the person "on another matter". That means nothing to do with the matter (read thread) which you were helping me with. I have also taken your warning in relation to messaging guide lines on board and scrubbed my question your QED
                    Okay. So you are done with this thread?

                    It's really just about 10 lines inside your main. Check if the index is a multiple of two, if so, assign it to the value of the proper index of the original array. Increment the second index properly.

                    Comment

                    • whodgson
                      Contributor
                      • Jan 2007
                      • 542

                      #11
                      yes i`m familiar with the mod operator.
                      I`ve rewritten the shuffle function along the lines you suggested and it works fine now. I got mixed up on the function arguements as well. Anyway thanks for your advice and help.
                      Code:
                      void shuffle(int a[],int n)
                      { 
                        int temp [8]; 
                        for(int i=0;i<=n/2;i++)
                          {temp[2*i] = a[i];
                           temp[2*i+1]= a[n/2+i];
                          }
                           for(int i=0;i<n;i++)
                           a[i]=temp[i];
                      }
                      /*This demonstrates the perfect shuffle

                      Array a[] is: 22 33 44 55 66 77 88 99
                      Array a[] is: 22 66 33 77 44 88 55 99 after shuffle
                      */

                      Comment

                      • romcab
                        New Member
                        • Sep 2007
                        • 108

                        #12
                        Hi,

                        Based on your example, I think you just need to sort the array, get the middle number and print alternately. Hope this help.

                        Comment

                        Working...