Hi everyone. I wrote the following code:
if the number entered is even ie if n=6 , the column of # should be at the 3rd column (ie 6/2)
if the number is odd ie if the number is 9, then the column of # should be at the 5th column

This the code I have to far: However, it prints the column of # always on the first column, so not in the middle as needed. Any help would be greatly appreciated! Thanks.

``````    #include <stdio.h>

int main(int argc,char *argv[]) {

char letterI[100][100];
int n;
int retVal;
int i=0;
int j=0;
int middleI;

printf("Enter a number: ");
retVal = scanf("%d", &n);

if (retVal == 1) {
for (j=0; j<n-1; j++) {
letterI[0][j] = '#';
printf("%c", letterI[0][j]);
}

if (n%2!=0) {
for (i=0; i<n-1; i++) {
middleI = n/2 + 1;
letterI[middleI][i] = '#';
printf("%c\n", letterI[middleI][i]);
}
}

else if (n%2==0) {
for (i=0; i<n-1; i++) {
middleI = n/2 - 1;
letterI[middleI][i] = '#';
printf("%c\n", letterI[middleI][i]);
}
}

for (j=0; j<n; j++) {
letterI[n-1][j] = '#';
printf("%c", letterI[n-1][j]);
}

printf("\n");

} else {
printf("error");
}

return 0;
}
``````

int argc,char *argv[]

1st of all, why do you use that? I don't see in your program nothing related to that.
2nd: this is an example of how you should do it, if I understood corectly.
Here I'm not using any char 2x2 array, but I do simulate one. So, I think this should help you figure out the problem.

``````#include <stdio.h>
#include <stdlib.h>

int even(int x){
if ((x&1)==0) return (1);
return (0);
}

int lenght(int x){
if ((x/10)==0) return (1);
else return (-1);
}

int main(){
int i, j, value, nr;
printf("Insert a nr. (0-9): ");
scanf("%d", &nr);
if (even(nr) && lenght(nr)==1){
value=nr/2;
printf("\n0123456789\n");
for (i=0;i<10;i++)
for (j=0;j<10;j++){
if (j==value)
printf("%c", '#');
else printf("%c", 'x');
if (j>8) printf("\n");
}
}
else if (!even(nr) && lenght(nr)==1){
printf("\n0123456789\n");
for (i=0;i<10;i++)
for (j=0;j<10;j++){
if (j==5) printf ("%c", '#');
else printf("%c", 'x');
if (j>8) printf("\n");
}
}
else printf("The number is too big: 0-9 only.");
return (0);
}
``````

My output:

``````Insert a nr. (0-9): 2
0123456789
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
x#xxxxxxxx
``````

for even numbers and for odd:

``````Insert a nr. (0-9): 9
0123456789
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
xxxxx#xxxx
``````

Thank you, thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.