I'm making a card shuffling function for the card game. I created an array of Card objects. Then I tried to rearrange the objects in the array using random_shuffle. But it doesn't work.
char faces[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
char suits[4] = { char(3), char(4), char(5), char(6) };
int values[13] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
Card** deck = new Card*[52];
for (int row = 0; row <= 3; row++)
{
for (int column = 0; column <= 12; column++)
{
deck[Card::getCounter()] = new Card(suits[row], faces[column], values[column], true);
}
}
int size = sizeof(deck) / sizeof(deck[0]);
random_shuffle(*deck, *deck + size);
I mean, if I check with cout, like
cout << deck[0]->getFace()<< deck[0]->getSuit() << endl;
it shows 2(heart), like it was before using random_shuffle
The problem with your code is that operator size returns the size of a pointer (8 on a 64-bit machine) rather than the size of the array it points to. As a consequence, the expression
sizeof(deck) / sizeof(deck[0])
returns 1, and you only shuffle a single value, which means you don't shuffle.
The solution can be:
use the explicit size of the array
random_shuffle(*deck, *deck + 52);
Better, define
const int NUM_OF_CARD_IN_DECK= 52
and use it anywhere you need it
Better still, use an std::vector
Related
I'm trying to write a simple program creating a cluster of k-neighbours for some example data in C++. So far I'm by the first iteration of the algorythm. Would someone explain why do I have so strange output of the string point[] = {...} array? I expect just letters like {A B C D .. H}, but instead have output like this:
A C2 2
4B C1 2.23607
9C C1 2.82843
17D C1 3.60555
30E C2 1
31F C1 2.23607
36G C2 0
36H C1 0
36
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
Where do the numbers before Letters come from?
And here is the code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
char point[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
float x[] = {1, 3, 4, 5, 1, 4, 1, 2};
float y[] = {3, 3, 3, 3, 2, 2, 1, 1};
float m1[] = {x[(sizeof(x)/sizeof(float))-1], y[(sizeof(y)/sizeof(float))-1]};
float m2[] = {x[(sizeof(x)/sizeof(float))-2], y[(sizeof(y)/sizeof(float))-2]};
string group[8];
float sumdq =0;
for(int i=0;i<=sizeof(point)-1;i++)
{
float d1 = sqrt(pow(x[i]-m1[0],2) + pow(y[i]-m1[1],2));
float d2 = sqrt(pow(x[i]-m2[0],2) + pow(y[i]-m2[1],2));
if(d1<d2)
{
group[i] = "C1";
sumdq =sumdq + pow(d1,2);
cout<<point[i]<< " "<<group[i]<< " "<<d1<<endl;
}
else
{
group[i] = "C2";
sumdq =sumdq + pow(d2,2);
cout<<point[i]<< " "<<group[i]<< " "<<d2<<endl;
}
cout<<sumdq;
}
return 0;
}
OK, now I get it. I,ve put cout<< inside the loop, and that was the problem. Thank you 463035818_is_not_a_number. And thanks tadman for hint with std::vector. BTW, is the only option to show size of an array? isn't there anything like x.length() in Python?
Anyway thak you alot for answares
Please help me out as to why in below code outputs are different:
int z[][3] = { 1, 2, 3, 4, 5, 6 };
printf("\n**(z+1): %d", **(z + 1));
Output: (z+1): 4
char y[][3] = { "A", "F", "G", "J", "M", "P" };
printf("\n**(y+1): %c", **(y+1));
Output: (y+1): F
Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ?
Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ?
That's not actually close to describing what is happening.
To understand what is happening, write the examples into their actual meaning
int z[][3] = { 1, 2, 3, 4, 5, 6 };
printf("\n**(z+1): %d", **(z + 1));
is actually
int z[][3] = { {1, 2, 3}, {4, 5, 6} };
printf("\n**(z+1): %d", **(z + 1));
where z[0] is an array of three elements initialised with {1, 2, 3} and z[1] is an array of three elements initialised with {4,5,6}.
In this z + 1 is equal to &z[0] + 1 which is equal to &z[1] (the address of an array of three int). So *(z+1) is (a reference to) z[1] (an array of three elements) and **(z+1) is z[1][0]. Since z[1] is an array initialised as elements {4,5,6}, z[1][0] is the first element of that array. This has a value of 4.
In comparison,
char y[][3] = { "A", "F", "G", "J", "M", "P" };
printf("\n**(y+1): %c", **(y+1));
each of the string literals is initialised as an array of two elements e.g. "A" is initialised as {'A', '\0'}.
Now y is an array of arrays of three char. If an array of three elements is given an initialiser with two char, as is the case here, the values that are not explicitly initialialised are zero-initialised. So
char y[][3] = { "A", "F", "G", "J", "M", "P" };
is equivalent to
char y[][3] = { {'A', '\0', '\0'}, {'F', '\0', '\0'}, {'G', '\0', '\0'}, {'J', '\0', '\0'}, {'M', '\0', '\0'}, {'P', '\0', '\0'}};
So y is array of six elements, each of which is an array of three char.
Using the same logic as in the discussion of z above, y + 1 is equal to &y[1] where y[1] is an array of three char that is initialized as {'F', '\0', '\0'}.
So *(y + 1) is (a reference to) y[1], and **(y + 1) is y[1][0]. This has a value of 'F'.
If you do this
int z[][3] = { 1, 2, 3, 4, 5, 6 };
printf("\n**(z+1): %d", **(z + 1));
you actually get
int z[2][3] = { {1, 2, 3}, {4, 5, 6} };
printf("\n**(z+1): %d", **(z + 1));
With this definition, *(z+1) points to {4,5,6} and therefore **(z+1) accesses the integer value 4.
You access the first element of the second element of our array.
In your second version this happens:
char y[][3] = { "A", "F", "G", "J", "M", "P" };
printf("\n**(y+1): %c", **(y+1));
This will result in
char y[6][3] = { {'A',0,0}, {'F',0,0}, {'G',0,0}, {'J',0,0}, {'M',0,0}, {P',0,0}};
printf("\n**(y+1): %c", **(y+1));
Now *(y+1) points to {'F',0,0} and therefore **(y+1) accesses the character value 'F'.
You access the first element of the second element of our array.
This is in fact the same result as with first version.
You actually don't need two dimensional char array , if you use only one character in each array.
Like here:
char y[] = { 'A', 'F', 'G', 'J', 'M', 'P' };
printf("\n**(y+1): %c", *(y + 1));
Can anyone explain to me what is the meaning of this code :
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
How do they get the number of {9, 8, 7, 6} and {5, 4, 3, 2}. Here is the full code:
/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/
#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]= {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup() {
Serial.begin(9600);
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop() {
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
Serial.print(keypressed);
}
}
Everything in the code seems easy to understand. Along with the comment it is clear as a crystal. But as you said, you need an explanation, I will provide an answer :
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
In the above given piece of code, two byte vaiables would be declared named numRows and numCols and initialized each with the value 4.
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
So here is the code that you are stuck at. Two byte array would be declared named rowPins and colPins each of size 4 (since value of numRows and numCols are 4). Which would range from 0 to 3 (like array in c or java). In this numbers 9,8,7,6 would be assigned to array rowPins and 5,4,3,2 will be assigned to array colPins. Now how or where will these values be. They will be stored in a sequential way from index 0 to index 3. i.e
rowPins[0]=9
rowPins[1]=8
rowPins[2]=7
rowPins[3]=6
colPins[0]=5
colPins[1]=4
colPins[2]=3
colPins[3]=2
This is how they get those numbers.
They are not getting the numbers they are assigning it to the arduino pins 2,3,4,5,6,7,8,9
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
you can also use 4,5,6,7,8,9,10,11 pins too. Just check what pins you are assigning for the row and column inputs and then write the code according to it.
if (selecao > 0 && selecao < 5) {
if (assentosP[selecao] == 'O') {
printf("Test");
assentosP is a char array, that has a bunch of either D or O letters (D by default)
char assentosP[2][4] = {
{'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
{'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
};
What I need to know is a way to make AssentosP find out if the char in position selecao of the array is either D or O.
When I try it, it gives me a error
iso c++ forbids comparison between pointer and integer c
You defined a two dimensional array
char assentosP[2][4] = {
{'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
{'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
};
Thus expression assentosP[selecao] has type char[4]. You may not compare such an array with a character (or an integer after the integer promotion) like this
if (assentosP[selecao] == 'O') {
Take into account that if an array declared as having 4 elements then the valid range of indices is [0, 3]. Thus this statement
if (selecao > 0 && selecao < 5) {
looks incorrectly.
Hello I'm trying to print const * char string ended with '\0'
But when I rying to use cout
const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'};
const char * i = command;
cout<<i;
It give me some squares "[][][]"
So I tried to iterate whole chars
while(*i != '\0'){
cout << *i;
i++;
}
It give me : S[][][]. Progress?
When I print *i it give me 'S' but *(i+1) '[][]'.
Is problem with this that I had 8 bit chars, and int is 32 bits?
Or if I doing something wrong?
Solved:
char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values.
-Now I need a coffie..
This is the problem
const char command[9] = {'S', 'T', 'E', 11, 12, 13, 14, 15, '\0'};
since the ascii values 11, 12, 13, 14, 15 are not printable characters, try this
const char command[14] = {'S', 'T', 'E', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '\0'};
Image from http://www.asciitable.com/
Solved:
char 13 as a "vertical tab" retur carrage to the start and hide from console "STE", and then C++ print 3 other non printable values. -Now I need a coffie..
Sorry I stuck with this.