c - displaying X made out of 1s with a matrix

Ok, so I want to print an 8x8 X made of 1s with a matrix.

The output should look like this for example:
1--------1
--1----1
-----1
--1----1
1--------1

Though, I don't know how to achieve this. I can only do a box of ones:
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

This is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <stdio.h>

int main()
{
	int matrix[8][8], i, j, b=1;
	
	for(i=0; i<8 ;i++)
	{
		for(j=0; j<8 ;j++)
		{
			matrix[i][j]=b;
			printf("%d ", matrix[i][j]);
		}
		printf("\n");
	}
}


I'm not sure if it's possible to print something like of an X without anything to fill in the blanks.

Though I'd like know if it is possible and how.
Last edited on
When printing the matrix, insert a condition such as
if (matrix[i][j] == b)
printf("%d", b);
else
printf("%c", '\s') ;

EDIT: I'm noticing you're filling the entire matrix with 1s. You can accomplish your task with a single for loop. To help yourself figure out how to do that, try drawing the matrix (with its coordinates) on a piece of paper and write 1s in the mood right places.
Last edited on
memset(matrix, 0, 64*sizeof(int)); //flood fill to zeros, or whatever you want.
for(int i = 0; i < 8; i++)
{
matrix[i][i] = 1; //normal diagonal
matrix[i][7-i] = 1; //reversed diagonal
}

this may look incorrect for even numbered array sizes. You show a 5x5 which has a center, but 8x8 does NOT have a true center. It won't look the same? What do you want to do for that?

with Lehti's example, this boils down to (because we used zeros!)

if(matrix[i][j]) //abuse of the zero = false
cout << matrix[i][j]
else cout << ' '

Last edited on
Ok, so I tweaked it a little and I got this so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main()
{
	int matrix[8][8], i, j;
	
	for(i=0; i<8 ;i++)
	{
		matrix[i][i] = 1;
		matrix[i][7-i] = 1;
		
		for(j=0; j<8 ;j++)
		{
			if(matrix[i][j]!=1)
			{
				matrix[i][j]=0;	
			}
			printf("%d ", matrix[i][j]);	
		}
		printf("\n");
	}
}


And you were right on the scale part, though it was only an example for everyone to get my point.

The output looks like this:

10000001
01000010
00100100
00011000
00011000
00100100
01100010
10000001


The stray 1 doesn't want to leave. However, if I print this in char.

xoooooox            
oxooooxo
ooxooxoo
oooxxooo  
oooxxooo
ooxooxoo
oxooooxo
xoooooox

It works fine.

How do I get rid of that stray?
Last edited on
It's happening because the array hasn't been initialized to zeroes. So if there's a stray 1 in it somewhere, your code won't set that to 0 and the 1 remains. There's probably a bunch of different values in there, but only the extraneous 1 remains.

The best thing to do is zero out the array initially when you define it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
	int matrix[8][8] = {{0}}; // start with all zeroes

	for (int i = 0; i < 8; i++) // put in the ones
	{
		matrix[i][i] = 1;
		matrix[i][7-i] = 1;
	}

	for (int i = 0; i < 8; i++) // print
	{
		for (int j = 0; j < 8; j++)
			printf("%d ", matrix[i][j]);	
		printf("\n");
	}
}

Last edited on
Thank you all!!
you missed the memset(matrix, 0, 64*sizeof(int)); //flood fill to zeros, or whatever you want.
which is a barbaric and crude way to say
int matrix[8][8] = {{0}};

*** I very, very rarely do 2 and 3d arrays, and tend to mangle things a bit with them, I forgot the double {} initialize.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cmath>
#include <bitset>
using namespace std;

int main()
{
   const int W = 8;
   for ( int M = pow( 2, W - 1 ), x = M; x > 0; x /= 2 ) cout << bitset<W>( M / x + x ) << '\n';
}


10000001
01000010
00100100
00011000
00011000
00100100
01000010
10000001




Or, for arbitary symbols,
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

string bits( int N, int W, char O, char X ) { return W ? bits( N/2, W-1, O, X ) + ( N%2 ? X : O )  : ""; }

int main()
{
   const int W = 8;
   const char O = '.', X = 'x';
   for ( int M = pow( 2, W - 1 ), x = M; x > 0; x /= 2 ) cout << bits( M / x + x, W, O, X ) << '\n';
}


x......x
.x....x.
..x..x..
...xx...
...xx...
..x..x..
.x....x.
x......x


Last edited on
I am amused. Nice one. I wonder if the old cryptic code contest/site is still around...
Last edited on
Very nice. (Although I think 1<<(W-1) is better than pow(2,W-1).)
It doesn't work for odd sizes, though.

E.g., W = 9:

x.......x
.x.....x.
..x...x..
...x.x...
...x.....      <== this x is out of place
...x.x...
..x...x..
.x.....x.
x.......x


The solution is to "or" x into M/x instead of adding it.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <bitset>
using namespace std;

int main() {
   const int W = 9;
   for ( int M = 1 << (W - 1), x = M; x; x >>= 1 )
       cout << bitset<W>( M / x | x ) << '\n';
}



x.......x
.x.....x.
..x...x..
...x.x...
....x....
...x.x...
..x...x..
.x.....x.
x.......x

Ah, I completely missed the odd-width cases, but the bitwise-or'ing is a great solution. Thanks!
Topic archived. No new replies allowed.