#include <iostream>
#include <random>
int main(){
int x = 10;
int y = 10;
char c[x][y];
std::fill(c[0], c[0]+x*y, 'X');
std::default_random_engine gen;
std::uniform_int_distribution<int> dist(0, 10);
for (int yy = 0; yy < y; yy++){
for (int xx = 0; xx < x; xx++){
c[x][y] = static_cast<char>(dist(gen));
std::cout << c[x][y] << " ";
}
}
return 0;
}
any help is appreciated. thanks in advance :)
1) Line 7 is illegal. The C++ standard does not allow you to use a non-const variable as the size of an array.
2) At line 14, you attempt to modify memory beyond the end of your array. At line 15 you attempt to output the contents of memory past the end of the array. This is undefined behaviour.
static_cast<char>(dist(gen)); probably will not be doing what you expect.
If you cast int to char :
Dec Char Description
0 NUL null
1 SOH start of header
2 STX start of text
3 ETX end of text
4 EOT end of transmission
5 ENQ enquiry
6 ACK acknowledge
7 BEL bell
8 BS backspace
9 HT horizontal tab
10 LF line feed
....
2) At line 14, you attempt to modify memory beyond the end of your array. At line 15 you attempt to output the contents of memory past the end of the array. This is undefined behaviour.
But MikeyBoy both indexes of c are 10 and the for-loops seem to be right to me (because both indexes are 10)? Where is the program trying to access an out of memory index?
But MikeyBoy both indexes of c are 10 and the for-loops seem to be right to me (because both indexes are 10)? Where is the program trying to access an out of memory index?
Look closer
1 2 3 4 5 6
for (intyy = 0; yy < y; yy++){
for (intxx = 0; xx < x; xx++){
c[x][y] = static_cast<char>(dist(gen));
std::cout << c[x][y] << " ";
}
}