I receive a matrix with elements of type unsigned char from another function and I am trying to find its max value.
boost::multi_array<unsigned char, 2> matrix;
All elements are integers and so I was hoping to recast matrix as type <int, 2> then perform std::max_element() operation, but unsure how to recast type of a boost multi array.
You don't need that to use max_element. char is an integral type just like int:
Live On Compiler Explorer
#include <boost/multi_array.hpp>
#include <fmt/ranges.h>
#include <algorithm>
int main() {
using boost::extents;
boost::multi_array<unsigned char, 2> matrix(extents[10][5]);
std::iota( //
matrix.data(), //
matrix.data() + matrix.num_elements(), '\x30');
fmt::print("matrix: {}\n", matrix);
auto [a, b] = std::minmax_element(matrix.data(),
matrix.data() + matrix.num_elements());
// as integers
fmt::print("min: {}, max {}\n", *a, *b);
// as characters
fmt::print("min: '{:c}', max '{:c}'\n", *a, *b);
}
Program stdout
matrix: {{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}}
min: 48, max 97
min: '0', max 'a'
Reinterpreting View
If you must (for other reasons thatn using max_element) you can use a multi_array_ref:
// reinterpreting view:
boost::multi_array_ref<const char, 2> view(
reinterpret_cast<const char*>(matrix.data()),
std::vector(matrix.shape(), matrix.shape() + 2));
fmt::print("view: {}\n", view);
Which prints Live On Compiler Explorer
view: {{'0', '1', '2', '3', '4'}, {'5', '6', '7', '8', '9'}, {':', ';', '<', '=', '>'}, {'?', '#', 'A', 'B', 'C'}, {'D', 'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L', 'M'}, {'N', 'O', 'P', 'Q', 'R'}, {'S', 'T', 'U', 'V', 'W'}, {'X', 'Y', 'Z', '[', '\'}, {']', '^', '_', '`', 'a'}}
You can also reshape it:
view.reshape(std::vector{25, 2});
fmt::print("reshaped: {}\n", view);
Printing
reshaped: {{'0', '1'}, {'2', '3'}, {'4', '5'}, {'6', '7'}, {'8', '9'}, {':', ';'}, {'<', '='}, {'>', '?'}, {'#', 'A'}, {'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I'}, {'J', 'K'}, {'L', 'M'}, {'N', 'O'}, {'P', 'Q'}, {'R', 'S'}, {'T', 'U'}, {'V', 'W'}, {'X', 'Y'}, {'Z', '['}, {'\', ']'}, {'^', '_'}, {'`', 'a'}}
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...
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.
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
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.