code produces no output

hello everybody. i have no idea why this code produces no output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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.

closed account (z05DSL3A)
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
....

Last edited on
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?
closed account (z05DSL3A)
Nwb wrote:
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 (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] << " ";
        }
    }
Oh yea..
right, i fixed some mistakes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>
int main(){
    const int x = 10;
    const 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, 9);

    for (int yy = 0; yy < y; yy++){
        for (int xx = 0; xx < x; xx++){
            c[xx][yy] = dist(gen) + '0';
            std::cout << c[xx][yy] << " ";
        }
    }
    return 0;
}


it's working now, but does line 14 seems right? i just want to convert int to char.
Last edited on
closed account (z05DSL3A)
Looks okay...

One thing that is missing is seeding default_random_engine, unless that was intentional. Without seeding the sequence will be the same each run.

a simple seed...
1
2
3
4
#include <chrono>
//...
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine gen(seed);
right, thanks everybody for the help :)
Topic archived. No new replies allowed.