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;
}
|