Create Alphabet from Array 2 dimensions

#include <iostream>
using namespace std;

main()
{
int arr[8][8] = {{0,1,1,1,1,1,1,0},{0,1,0,0,0,0,1,0},{0,1,0,0,0,0,1,0},{0,1,0,0,0,0,1,0},{1,1,1,1,1,1,1,1},{1,1,0,0,0,0,0,1},{1,1,0,0,0,0,0,1},{1,1,0,0,0,0,0,1}};
int n = sizeof(arr);

if (arr == 0){
cout << " ";
}
else if(arr == 1){
cout << "#";
}

cout << arr;
}

The conditions is if element is 0 the output will change to space, and if the element is 1 the output will change to "#".
or somebody can give me example script?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main() // <--
{
    int arr[8][8] = {
        {0,1,1,1,1,1,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {1,1,1,1,1,1,1,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1}
    };

    for(int i = 0; i < 8; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            if (arr[i][j] == 0)
                cout << " ";
            else
                cout << "#";
        }
        cout << '\n';
    }

    return 0;
}


 ###### 
 #    # 
 #    # 
 #    # 
########
##     #
##     #
##     #
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
	constexpr size_t arrsz {8};

	const int arr[arrsz][arrsz] {
		{0,1,1,1,1,1,1,0},
		{0,1,0,0,0,0,1,0},
		{0,1,0,0,0,0,1,0},
		{0,1,0,0,0,0,1,0},
		{1,1,1,1,1,1,1,1},
		{1,1,0,0,0,0,0,1},
		{1,1,0,0,0,0,0,1},
		{1,1,0,0,0,0,0,1}
	};

	for (size_t i = 0; i < arrsz; ++i)
		for (size_t j = 0; j < arrsz; ++j)
			std::cout << char(' ' + ('#' - ' ') * arr[i][j]) << (j == arrsz - 1 ? "\n" : "");
}

Thanks, its very helpful
Hello ninetails99,

Looking at your program and with a couple of suggestions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

constexpr int MAXROWS{ 8 }, MAXCOLS{ 8 };

int main()
{
    int arr[MAXROWS][MAXCOLS]
    {
        {0, 1, 1, 1, 1, 1, 1, 0},
        {0, 1, 0, 0, 0, 0, 1, 0},
        {0, 1, 0, 0, 0, 0, 1, 0},
        {0, 1, 0, 0, 0, 0, 1, 0},
        {1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 0, 0, 0, 0, 0, 1},
        {1, 1, 0, 0, 0, 0, 0, 1},
        {1, 1, 0, 0, 0, 0, 0, 1}
    };
    int n = sizeof(arr);  // <--- Not what you are expecting. Will return the size of the whole size of the array, i.e., number of bytes.

    //if (arr == 0)  // <--- Needs to be in 2 for loops to check each element of the array.
    //{
    //    cout << " ";
    //}
    //else if (arr == 1)
    //{
    //    cout << "#";
    //}

    for (int row = 0; row < MAXROWS; row++)
    {
        for (int col = 0; col < MAXCOLS; col++)
        {
            if (arr[row][col])
                cout << "#";
            else
                cout << " ";
        }

        std::cout << '\n';
    }

    cout << '\n' << arr << '\n';  // <--- Will print the address of the array. You need 2 for loops to print each element of the array.

    return 0;  // <--- Not required, but makes a good break point for testing.
}

When you initialize the array either way that you set up the numbers, i.e., my way or the other 2 suggestions, it makes it much easier to work with.

Using the constant variables makes it much easier to change should the need arise. You only have 1 place to make the change. This concept will be more useful in future programs.

The revised code produces this output:

 ######
 #    #
 #    #
 #    #
########
##     #
##     #
##     #

0096F848  // <--- Address of "arr". Does not print the whole array.


Not a complete program, but a start for you.

Andy
And where the matrix is a square matrix a single variable MAXSIZE = 8 can be used instead of MAXROW = 8 and MAXCOL = 8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
    int arr[8][8] = {
        {0,1,1,1,1,1,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {1,1,1,1,1,1,1,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1}
    };

    for ( int i = 0; i < 64; i++)
    {
       cout << " #"[*(arr[0]+i)];
       if ( !( (i + 1 )%8 ) ) cout << '\n';
    }
}


 ###### 
 #    # 
 #    # 
 #    # 
########
##     #
##     #
##     #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

const int M_SIZE {8};

int main()
{
    char character[] {' ', '#'}; // <--
    int arr[][M_SIZE] {
        {0,1,1,1,1,1,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {0,1,0,0,0,0,1,0},
        {1,1,1,1,1,1,1,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1},
        {1,1,0,0,0,0,0,1}
    };

    for(int i = 0; i < M_SIZE; i++)
    {
        for(int j = 0; j < M_SIZE; j++)
        {
            cout << character[ arr[i][j] ]; // <--
        }
        cout << '\n';
    }

    return 0;
}


 
 ###### 
 #    # 
 #    # 
 #    # 
########
##     #
##     #
##     #
Program ended with exit code: 0
Topic archived. No new replies allowed.