arrays and card shuffling

so im taking an introductory compsci class and its not introductory. its pretty rough for a beginner coder like myself. so this is my hw: took me a while to get this far.
i have to do monge's shuffle.and a bit more. im sure you can look it up on google. and use these functions.
void mongeShuffle(int A[], int B[], int n) which accepts two equal size arrays and applies the Monge shuffle procedure to the firrst n elements of A and places them in the fi rst n positions of B. (Make sure that when calling this function B's elements are not important and can be overwritten during the shuffling.)
2. int cycleLength(int A[], int n) which returns the smallest number (greater than zero)of Monge shuffles that must be applied to A in order for the resulting order to be equal to A. I.e, how many shuffles does it take to get back to the original ordering?
3. bool isTotalShuffleCycle(int A[], int n) which returns true if all of the shuffles applied A are always total. A total shuffle is one in which every shuffled element is in di fferent position than its original starting position.
Your program should repeatedly ask the user for the number n (at least 2 and at most 1000) of items to shuffle and perform the tasks listed below. If they enter a number outside of this range the program terminates.

sample:
How many items in the sequence? 19
Initial Ordering:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Ordering after one shuffle:
17 15 13 11 9 7 5 3 1 0 2 4 6 8 10 12 14 16 18
It took 18 shuffles to restore array back to its original order.
Not all the shuffles in the cycle were total.

WHAT I HAVE. MY SHUFFLE WORKS BUT MY OTHER FUNCTIONS DONT. please help.
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <iomanip>
using namespace std;




void mongeShuffle(int A[], int B[], int n)
{
     int count = 0;

    
    /*for (int j=0; j<n; j++)
    {
        cout<<" Initial ordering " << A[j] << endl;
    } */    

    for (int k=n-1; k>=0; k--)
    {
        if(k%2 == 1)
        B[(n-k-1)/2]= A[k];
        else if(k%2 == 0)

                    B[(k/2)+n/2] = A[k];      
        
     
    }

   /* for (int l=0; l<n ; l++)
    {
        cout<<" new sequence " <<  B[l] << endl;
        
    }*/
      
      

}     

int cycleLength(int A[], int n)
{
    int count=0;
    int sameCheck = 0;
    bool isSame = false;
    int B[1000];
    
    while(!isSame)
    {
                count++;
                mongeShuffle(A, B, n);
                for(int i = 0; i < n; i++)
                {
                        if(A[i] != B[i])
                                sameCheck++;-
                }
                if(sameCheck == 0)
                             isSame = true;
                else
                    sameCheck = 0;
    }
    
    return (count);
}


bool isTotalShuffleCycle(int A[], int n)
{
	int B[1000];
	int count = 0;

	for(int k=0; k<n; k++)
	{
		B[k] = A[k];
	}

	mongeShuffle(A, B, n);

	for(int k=0; k < n; k++)
	{
		if(B[k] == A[k])
			count++;
	}

	return (count == n-1);
}

void displayResults ( int A[], int B[], int n)
{
     mongeShuffle(A,B,n);
     
     for (int j=0; j<n; j++)
    {
        cout<<" Initial ordering " << A[j] << endl;
    }     
    
    for (int l=0; l<n ; l++)
    {
        cout<<" new sequence " <<  B[l] << endl;
        
    }
}
    


int main()
{
    int n = 2;
	int p = 0;
    const int SIZE = 1000;  
        
        
  
   while(n>=2 || n<=1000)
   {
        cout << "How many items in the sequence?: ";
        cin >> n;
        if(n>=2 || n<=1000)
		{
			int A[SIZE];
			int B[SIZE];
    
		     for (int i=0; i<n; i++)
			{
				A[i]=i;
			}
			mongeShuffle(A, B, n);    
			displayResults(A, B, n);
			p=cycleLength(A, n);
			cout<<" It took "<< p <<" shuffles to restore array back to its original order." <<endl;
		}
    }
	return 0;
}

the problem is you are keep Shuffleing the original array again and again and it is not comming out of while loop.
you should store your original array to some C[SIZE] and after Shuffle copy the Shuffled containt from B[] to A[].
and then compair A[] and C[] in function cycleLength in for loop.


hope you got the point.
Last edited on
Topic archived. No new replies allowed.