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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int SIZE = 100;
while (i<count) {
if (i%2 != 0)
destination[count-(i+1)]=source[i];
i++;
}
void display (int A[],int count) {
for (int i=0; i<count; i++)
cout << A[i] << " " << endl;
}
int main () {
int A[SIZE] = {0,1,2,3,4,5};
int C[SIZE];
const int count = 6;
shuffle(A,C,count);
display(C,count);
return 0;
}
|
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
1 2 3
|
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