Sequence comparing

I am writing a program that takes an array A and shuffles it and saves the shuffled elements into array B. This I have finished. Now I am writing a function that takes array B and according to the same rules that shuffled array A, shuffles B so that its elements are the same as A.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int A[8] = {0,1,2,3,4,5,6,7}
int B[8] = {7,5,3,1,0,2,4,6}

int same = 0;
int diff = 0;
int count = 0;
for(int i=0; i<value; i++)
{
  if(B[i] == A[i])
  same++;
  else
  diff++;
}
if (same < value) {
  int i = 0;
	while (i < value) {
	if (i % 2 != 0)
	        B[(value - (i + 1)) / 2] = A[i];
	else
		B[(value + i) / 2] = A[i];
	i++;
count++;
}	 
}

This should display 4, because it would take 4 shuffles to restore array B to be the same as array A. I do not see where I went wrong.
line 7
for(int i=0; i<value; i++)

line 15
int i = 0;

Did you mean to reset i every time it goes through the if loop on line 14?
And if so, why did you declare it twice? seems a bit pointless.
Last edited on
So I changed it to
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

int A[8] = {0,1,2,3,4,5,6,7}
int B[8] = {7,5,3,1,0,2,4,6}

int same = 0;
int diff = 0;
int count = 0;
for(int i=0; i<value; i++)
{
  if(source[i] == destination[i])
  same++;
  else
  diff++;

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

}
But still get the same answer. What this loop should do it take the array B, as long as it is not the same as A, and loop according to those rules, until the elements of B are the same as element A, and count how many times it had to loop.

So lets say array A is 0 1 2 3 4 5 6 7
and B is 7 5 3 1 0 2 4 6
if B loops 1 more time 6 2 1 5 7 3 0 4
1 more time 4 3 5 2 6 1 7 0
and one last time 0 1 2 3 4 5 6 7
as you see after four times it went back to the original.
can you link the entire program?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Monge's Shuffle

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

const int SIZE = 1000;

//determines where to put a number according to whether it is odd or even, starting backwards from the highest number to the lowest.
//PRE: takes two arrays, A filled with numbers 1 to n-1, and shuffles it according to monge's shuffle rules
//POST: Monge's shuffle is saved into array B
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];
		else
			destination[(count + i) / 2] = source[i];
		i++;
	}
	}

//Displays values for arrays chosen in the main function
//PRE: takes array A, or B depending on what is called from the main
//POST: displays that sequence
void display (int A[],int count) {
	for (int i = 0; i<count; i++)
		cout << setw(4) << A[i];
	cout << endl;
}

void totalShuffles (int destination[], int source[], int value) {

int same = 0;
int diff = 0;
int count = 0;
for(int i=0; i<value; i++)
{
  if(source[i] == destination[i])
  same++;
  else
  diff++;

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

//PRE
//POST: prompts to enter a value, calls functions, in order to display the initial array sequence then, Monge's shuffle with the entered values.
int main () {

	int A[SIZE] = {};
	int B[SIZE] = {};
	int value;
	
	cout << "How many items in the sequence?" << endl;
	cin >> value;
	
	if (value >= 2 && value <= 1000) {
		for (int i = 0; i<value; i++)
			A[i] = i;
	cout << "Initial Ordering: " << endl;
	

	display(A, value);}

	if (value >= 2 && value <= 1000) {
		for (int i = 0; i<value; i++)
			A[i] = i;
	cout << "Ordering after one shuffle: " << endl;
	

	shuffle(A, B, value);
	display(B, value);}

	totalShuffles(A, B, value);
	return 0;
}
Last edited on
This is the small version with preset arrays.
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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;
const int SIZE = 8;

void display (int A[],int count) {
	for (int i = 0; i<count; i++)
		cout << setw(4) << A[i];
	cout << endl;
}

int main () {

int A[SIZE] = {0,1,2,3,4,5,6,7};
int B[SIZE] = {7,5,3,1,0,2,4,6};

int same = 0;
int diff = 0;
int count = 0;
for(int i=0; i<SIZE; i++)
{
  if(A[i] == B[i])
  same++;
  else
  diff++;

if (same < SIZE) {

	while (i < SIZE) {
		if (i % 2 != 0)
			B[(SIZE - (i + 1)) / 2] = A[i];
		else
			B[(SIZE + i) / 2] = A[i];
		i++;
	count++;
	}	
	cout << count << endl;
}}
display(A, SIZE);
display(B, SIZE);
return 0;
}
It should basically tell me how many times I would have to loop array B to get back to A. Or I guess How many times to loop array A so that it gets back to original.
looping array A 4 times would return it to its original state.
original 0 1 2 3 4 5 6 7
1st loop 7 5 3 1 0 2 4 6
2nd loop 6 2 1 5 7 3 0 4
3rd loop 4 3 5 2 6 1 7 0
4th loop 0 1 2 3 4 5 6 7
Topic archived. No new replies allowed.