Check if something at position X of array is a char - c++

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.

Related

Difference of value of **(ptr+1) in char and int

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

Byte of word returns one less than what it should be

My professor is having us make our own version of the functions mystrcat, mystrlen and mystrcopy. I have a problem where the word is returning the wrong amount of bytes. Or, rather, I think it is.
The phrase: "HELLO WORLD!" , if I am not wrong, should return 13 bytes? Like in:
const char char_literal[] =
{ 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D', '!', '\0' };
// 1 2 3 4 5 6 7 8 9 10 11 12 13
I wrote the function myStrLen() that returns ({ 'H', 'E', 'L', 'L', 'O', ' '}) as 6, but because it is alone, there should be a '\0' at the end right?
So should I be returning i + 1 or is 6 correct?
int myStrLen(char stringInput[]){
for(int i = 0; ; i++){
if(stringInput[i] == '\0'){
return i;
If your professor tells you to write a function that behaves exactly as strlen, then it shall not count the string termination character:
std::size_t strlen( const char* str );
Returns the length of the given byte string, that is, the number of
characters in a character array whose first element is pointed to by
str up to and not including the first null character. ...
So returning i is correct, besides the fact that the "original" strlen takes a const input parameter and returns a size_t...

Rearranging objects in array in C++

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

how to print const char pointer in c++

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.

Generate "characters" based on a probability

I need a little help with my tetris game
that I am coding in C++ , here is my problem:
The list of tetris blocks are 7 types:
{'I', 'J', 'L', 'S', 'Z', 'O', 'T'}
and i need to pick ONE of the above character such that
S and Z are selected with probability 1/12 each, and the other blocks are selected with
probability 1/6 each.
What is my best way to generate the blocks according to these probabilities?
Declare and define a twelve-item array with a single occurrence of S and Z; two occurrences each of the rest; and pick a random element from that.
Create an array like so:
const char* block_distribution = {'I', 'I', 'J', 'J', 'L', 'L', 'S',
'Z', 'O', 'O', 'T', 'T'};
Then pick one element from that array using a uniform distribution.
put those characters in a character array and generate random values from 0 to 6 with srand()
than you can get a random
char myArray[] = {'I', 'J', 'L', 'S', 'Z', 'O', 'T'};
and then get values with
myArray[ (rand()%5)+1 ]
The easiest solution I can think of is to use numeric ranges and pseudo-random numbers.
So, first assign ranges to each letter:
I: 0 to 1
J: 1 to 2
L: 2 to 3
S: 3 to 3.5
Z: 3.5 to 4
O: 4 to 5
T: 5 to 6
then generate pseudo-random numbers within the range 0 to 6. Whichever part of the range that the number falls within, that's the letter you choose.
Unfortunately I do not know C++ so I cannot provide you with code, but I don't think it should be too difficult to implement.