470,851 Members | 1,168 Online

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

542 512MB
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?.

Expand|Select|Wrap|Line Numbers
1. #include<iostream>
2. using namespace std;
3. void print(int a[],int n);
4. void shuffle(int a []);
5. int main()
6. {
7. int a[8]={22,33,44,55,66,77,88,99};
8. cout<<"\nArray a[] is: ";
9. print(a,8);
10. shuffle( a);
11. cout<<"\nArray a[] is: ";
12. print(a,8);
13. cout<<"after shuffle";
14. cout<<"\n\n\n";
15. system("pause");
16. return 0;
17. void shuffle(int a[])
18. { int SIZE=8;
19.   int temp[8]={0};
20.   const int half=SIZE/2;
21.   for(int i=0;i< SIZE;i++)// i have tried using 'half' ILO 'SIZE' also deleting i++
22.       {   temp[2*i]=a[i];// and revising this line to temp[2*i++] to no avail
23.            temp[2*i+1]=a[half+i];
24.
25.           for(i=0;i<SIZE;i++)
26.           a[i]=temp[i];
27.
28.       }
29. }
30. void print(int a[],int n)
31. {
32.      for(int i=0;i<n;i++)
33.      cout<<a[i]<<" ";
34. }
/*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*/
Feb 27 '08 #1
11 12548
sicarie
4,677 Expert Mod 4TB
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?
Feb 27 '08 #2
whodgson
542 512MB
Its meant to interleave the lower value elements between the four higher value ones as shown below "it should be".
Feb 27 '08 #3
sicarie
4,677 Expert Mod 4TB
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...
Feb 27 '08 #4
whodgson
542 512MB
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.
Feb 27 '08 #5
sicarie
4,677 Expert Mod 4TB
Well, I think you will want to be careful about PM'ing questions to Moderators.
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.
Feb 27 '08 #6
whodgson
542 512MB
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.
Feb 27 '08 #7
sicarie
4,677 Expert Mod 4TB
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.
Feb 27 '08 #8
whodgson
542 512MB
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
Feb 27 '08 #9
sicarie
4,677 Expert Mod 4TB
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.
Feb 27 '08 #10
whodgson
542 512MB
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.
Expand|Select|Wrap|Line Numbers
1. void shuffle(int a[],int n)
2.   int temp [8];
3.   for(int i=0;i<=n/2;i++)
4.     {temp[2*i] = a[i];
5.      temp[2*i+1]= a[n/2+i];
6.     }
7.      for(int i=0;i<n;i++)
8.      a[i]=temp[i];
9. }
/*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
*/
Feb 28 '08 #11
romcab
108 100+
Hi,

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