### arrays

My program is meant to shuffle a sequence of numbers 0,1,2,3,4,5 so that the ending result is 5,3,1,0,2,4.
it does this by first taking the initial array and flipping it so that it goes 5,4,3,2,1,0
This is called a mongean shuffle.
 ``1234567891011121314151617181920212223242526272829303132333435`` `````` #include #include #include using namespace std; const int SIZE = 100; while (i

This gives me an output of
 `5,huge negative number,3,huge negative number,1,huge negative number`
because the even numbers are divisible by 2, and that gives them negative values. I am trying to figure out how to make is so that if a number is divisible by 2 it is ignored, and when all the odd numbers are saved to the new array the even numbers are added to the end. I tried writing a code where
 ``123`` ``````if (i%2 != 0) destination[count-(i+1)]=source[i]; i++``````
, but I do not know how to make is so that the action i++ is not done when a number is divisible by 2.
How can I rewrite it so instead it give me
 `5,3,1, then the negative numbers`

I am unsure of how to make the odd numbers be saved in int destination [], in order, without the gaps where even numbers (huge negative numbers) should be
 I do not know how to make is so that the action i++ is not done when a number is divisible by 2.

Put both statements after the if in braces:
 ``1234`` ``````if (i%2 != 0) { destination[count-(i+1)]=source[i]; i++; }``````

BTW, lines 10-14 can not appear outside of a function.

Ok so I did what you said, however when I run the program nothing shows up.

Right sorry, forgot to write that part of the code in my post.
This is what it is right now.
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include using namespace std; const int SIZE = 100; void shuffle (int source[], int destination [], int count) { int i = 0; while (i

There is no output however
 ``1234567`` ``````int i = 0; while (i

It's getting stuck in this while loop because if (i % 2 == 0) then it isn't incrementing i.

EDIT: In other words, i only increases if it is not divisible by two, if it is then you have an infinite loop.
Last edited on
The real reason you are getting large negative numbers is the locations in the array haven't been initialized. If you instead declare C like:

`int C[SIZE] = {} `

then they will start as zero. Take i++ back out of your if statement and you will end up with an output of 5 0 3 0 1 0. If you want to store them to the first three indexes then you need to change

`destination[count-(i+1)]`

so that it points to the right index.
Ok I figured out the whole code. Thanks so much! It is now
 ``1234567891011`` ``````void shuffle (int source[], int destination [], int count) { int i = 0; while (i

and outputs the Mongean shuffle no matter the amount of items in the array.
Also, thanks for that neat trick with `int C[SIZE] = {}` my professor never mentioned that.