c method named xo that accepts an integer size as a parameter and prints a squar
ID: 3533398 • Letter: C
Question
c method named xo that accepts an integer size as a parameter and prints a square of size by size characters, where all characters are "o" except that an "x" pattern of "x" characters has been drawn from the corners of the square. In other words, on the first line, the first and last characters are "x"; on the second line, the second and second-from-last characters are "x"; and so on. If 0 or less is passed for the size, no output should be produced.
The following table lists some calls to your method and their expected output:
Call xo(5); xo(8); xo(3); xo(1); xo(0); xo(12); xo(11); Output xooox oxoxo ooxoo oxoxo xooox
xoooooox oxooooxo ooxooxoo oooxxooo oooxxooo ooxooxoo oxooooxo xoooooox
xox oxo xox
x
xoooooooooox oxooooooooxo ooxooooooxoo oooxooooxooo ooooxooxoooo oooooxxooooo oooooxxooooo ooooxooxoooo oooxooooxooo ooxooooooxoo oxooooooooxo xoooooooooox
xooooooooox oxoooooooxo ooxoooooxoo oooxoooxooo ooooxoxoooo oooooxooooo ooooxoxoooo oooxoooxooo ooxoooooxoo oxoooooooxo xooooooooox
Explanation / Answer
I'm not familiar with java but here is the code in c, maybe you can make something of it by changing to printf statements to System.print from java.
void xo(int num)
{
int i,j;
for(i = 0; i < num; i++){
for(j = 0; j < num; j++){
if(j == i || j == (num - i - 1)){
printf("x");
}
else
printf("o");
}
printf(" ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.