Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/x.c Secret
Created Jul 30, 2016

Embed
What would you like to do?
CS50x Coding Contest 2016 - X
#include <stdio.h>
#include "cs50.h"
int main(void)
{
int n = GetInt();
if (n % 2 == 0)
return 0;
if (n < 3 || n > 23)
return 0;
int board[n][n];
// counters
int x = 1;
int y = n-2;
// loop through 2D array and put border around it
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == 0)
board[i][j] = '*';
else if (j == 0)
board[i][j] = '*';
else if (j == (n-1))
board[i][j] = '*';
else if (i == (n-1))
board[i][j] = '*';
else
board[i][j] = ' ';
}
}
// loop through at put the X down
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (x > 0 && x < n-1)
{
if (y > 0 && y <= n-2)
{
board[x][x] = '*';
board[x][y] = '*';
x++;
y--;
}
}
}
}
// print the final board
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%c", board[i][j]);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.