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.
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

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:
1
2
3
4
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.
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;


void shuffle (int source[], int destination [], int count) {

	int i = 0;
	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;
}

There is no output however
1
2
3
4
5
6
7
int i = 0;
	while (i<count){
		if (i%2 != 0){
			destination[count-(i+1)]=source[i];
		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
1
2
3
4
5
6
7
8
9
10
11
void shuffle (int source[], int destination [], int count) {

	int i = 0;
	while (i<count){
		if (i%2 != 0)
		destination[(count-(i+1))/2]=source[i];
		if (i%2 == 0)
			destination[(count + i)/2]=source[i];
		i++;
	}
	}

and outputs the Mongean shuffle no matter the amount of items in the array.
Glad to be of help!
Also, thanks for that neat trick with int C[SIZE] = {} my professor never mentioned that.
Topic archived. No new replies allowed.