Array of 52, error with swapping values of indexes

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
#include <iostream>
using namespace std;

// swap function
void xorSwap(int* x, int* y) {
	if (x != y) {
		*x ^= *y;
		*y ^= *x;
		*x ^= *y;
	}
}

int main() {
  int deck[52];
	for (int i=0; i<52; i++) {
		deck[i] = i;
	}
        // swapping values of index i and j
	int j;
	for (int i = 51; i > -1; i--) {
		j = rand() % 52;
		xorSwap(deck[i], deck[j]);
	}
	return 0;
}


Started learning c++ this week. Intent is to create a deck of cards (array of 52 ints) and swap values at different indexes around. Unsure why not working.

Last edited on
The errors I get when compiling are
main.cpp:21:12: error: ‘rand’ was not declared in this scope
   j = rand() % 52;
            ^
main.cpp:22:17: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
   xorSwap(deck[i], deck[j]);
                 ^
main.cpp:5:6: error:   initializing argument 1 of ‘void xorSwap(int*, int*)’ [-fpermissive]
 void xorSwap(int* x, int* y) {
      ^
main.cpp:22:26: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
   xorSwap(deck[i], deck[j]);
                          ^
main.cpp:5:6: error:   initializing argument 2 of ‘void xorSwap(int*, int*)’ [-fpermissive]
 void xorSwap(int* x, int* y) {


So, first "‘rand’ was not declared in this scope" means the compiler doesn't know what the rand function is. Most of the time this means you forgot to declare or include something. In this case, rand is included in cstdlib so you will need to add
#include <cstdlib>

Next, deck[i] refers to the int at deck[i], and your xorswap function is expecting a pointer, not an int. You can use & to denote that you want the address of the int instead of the int itself.
xorSwap(&deck[i], &deck[j]);

And that's it! Adding some print functions so we can see what's heppening and the code looks like this.
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
#include <iostream>
#include <cstdlib> //<---change 1

using namespace std;

// swap function
void xorSwap(int* x, int* y) {
	if (x != y) {
		*x ^= *y;
		*y ^= *x;
		*x ^= *y;
	}
}

int main() {
  int deck[52];
	for (int i=0; i<52; i++) {
		deck[i] = i;
	}
	
	cout << "Before: ";
	for (int i = 0; i < 52; i++) {
	    cout << deck[i] << ", ";
	}
	cout << "\n";
	
    // swapping values of index i and j
	int j;
	for (int i = 51; i > -1; i--) {
		j = rand() % 52;
		xorSwap(&deck[i], &deck[j]);//<--- cahnge 2
	}
	
	cout << "After: ";
	for (int i = 0; i < 52; i++) {
	    cout << deck[i] << ", ";
	}
	cout << "\n";
	
	return 0;
}


And the output
Before: 0, 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, 
After: 12, 11, 25, 44, 0, 45, 17, 4, 33, 28, 39, 48, 22, 43, 16, 37, 50, 5, 6, 36, 47, 9, 51, 23, 29, 41, 18, 30, 40, 21, 8, 7, 24, 14, 10, 13, 42, 34, 20, 26, 3, 2, 46, 32, 19, 38, 27, 1, 35, 15, 31, 49,
Last edited on
An object of struct Card should ideally have at least 2 data-members – suit and rank – where, in turn, suit might be an enum and rank could be std::string for e.g. see here:
http://www.cplusplus.com/forum/beginner/211044/#msg989293
You could covert this information to an int I suppose but the values would look like magic numbers

so, if you were to retain the suit and rank data members, to swap instances of Card you'd have to overload operator ^= for this type but that can be avoided by using std::swap outright provide struct Card is move assignable and move constructible: http://en.cppreference.com/w/cpp/algorithm/swap

an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <utility>

struct Person
{
    std::string m_name;
    int m_age;
};

int main()
{
    Person a{"John", 41};
    Person b{"Jane", 14};
    std::swap(a, b);
    std::cout << a.m_name << " " << a.m_age << "\n";//prints Jane 14
    std::cout << b.m_name << " " << b.m_age << "\n";//prints John 41

    //a ^= b;//error: no match for operator ^=
}
Last edited on
Topic archived. No new replies allowed.