# Thread: I'm having trouble writing into specific points of a 2D array (battleship)

1. ## I'm having trouble writing into specific points of a 2D array (battleship)

Here's the code I'm using to initialize the board so the rows and columns are numbered 0-9 and everything else is filled in with '~'
Code:
```void initializeboard(char plyr1board[][10], int numrows, int numcolumn)
{
int rowindex = 0, clmnindex = 0;
for (; rowindex < numrows; ++rowindex)
{
for (clmnindex = 0; clmnindex < numcolumn; ++clmnindex)
{
plyr1board[rowindex][clmnindex] = '~';
}
}
}```
Then I ask the user for the direction they want to place the ship - just working with the carrier for now (5 spaces) - so I can prevent the ship from being placed outside of the board. The last loop in this snippet is supposed to be writing the 'C' character to the board starting at the coordinate given by the user and looping DOWN 5 spaces (chose vertical option), so the column placement shouldn't be altered by the loop.

Code:
```printf("\n\nEnter direction you would like to place your Carrier: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
scanf("%d", &direction);
if (direction == 1)
{
printf("\n\nEnter coordinate where you would like to place Carrier: ");
scanf("%d%d", &c1, &c2);
while (c1 > 5)
{
printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
printf("Enter coordinate where you would like to place Carrier:");
scanf("%d%d", &c1, &c2);
}
for (int a = c1, q = 0; q < 5; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
{
plyr1board[a][c2] = 'C'; //a = row, c2 = column (for vertical, column shouldn't change)
}```
However, when I run this with coordinates (4, 2) this is what I get:

0 1 2 3 4 5 6 7 8 9
0 ~ ~ ~ ~ C C C C C ~
1 ~ ~ ~ ~ C C C C C ~
2 ~ ~ ~ ~ C C C C C ~
3 ~ ~ ~ ~ C C C C C ~
4 ~ ~ ~ ~ C C C C C ~
5 ~ ~ ~ ~ C C C C C ~
6 ~ ~ ~ ~ C C C C C ~
7 ~ ~ ~ ~ C C C C C ~
8 ~ ~ ~ ~ C C C C C ~
9 ~ ~ ~ ~ C C C C C ~

2. Dunno - works for me.
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void initializeboard(char plyr1board[][10], int numrows, int numcolumn)
{
int rowindex = 0, clmnindex = 0;
for (; rowindex < numrows; ++rowindex)
{
for (clmnindex = 0; clmnindex < numcolumn; ++clmnindex)
{
plyr1board[rowindex][clmnindex] = '~';
}
}
}
void printboard(char plyr1board[][10], int numrows, int numcolumn)
{
for ( int r = 0 ; r < numrows ; r++ ) {
for ( int c = 0 ; c < numcolumn ; c++ ) {
printf("%c",plyr1board[r][c]);
}
printf("\n");
}
}

int main ( ) {
char plyr1board[10][10];
int direction,c1,c2;
initializeboard(plyr1board,10,10);
printf("\n\nEnter direction you would like to place your Carrier: \n\n1. Vertical\n2. Horizontal\n\nSelection: ");
scanf("%d", &direction);
if (direction == 1)
{
printf("\n\nEnter coordinate where you would like to place Carrier: ");
scanf("%d%d", &c1, &c2);
while (c1 > 5)
{
printf("\n\t\t\t\tDon't send your men into the abyss! Stay within the board (5 Spaces)\n\n");
printf("Enter coordinate where you would like to place Carrier:");
scanf("%d%d", &c1, &c2);
}
for (int a = c1, q = 0; q < 5; ++q, ++a) //q represents the size of the carrier (5), once 4 is reached, a should stop incrementing as well
{
plyr1board[a][c2] = 'C'; //a = row, c2 = column (for vertical, column shouldn't change)
}
}
printboard(plyr1board,10,10);
}

\$ gcc -g foo.c
\$ ./a.out

Enter direction you would like to place your Carrier:

1. Vertical
2. Horizontal

Selection: 1

Enter coordinate where you would like to place Carrier: 4 2
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~C~~~~~~~
~~C~~~~~~~
~~C~~~~~~~
~~C~~~~~~~
~~C~~~~~~~
~~~~~~~~~~```

Perhaps like I did, you should post an actual complete program which replicates your observation, not just random snippets which prove absolutely nothing one way or another.