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

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble printing x and y values of struct points in an array?
    By arcadedragon in forum C Programming
    Replies: 6
    Last Post: 10-03-2012, 10:15 AM
  2. C++ Plot Simple Points / Graph, X & Y array points
    By Khadafi in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2011, 03:47 AM
  3. writing in a specific position
    By overlord21 in forum C Programming
    Replies: 6
    Last Post: 09-28-2008, 03:44 PM
  4. Why so much trouble with floating points?
    By darketernal in forum C++ Programming
    Replies: 14
    Last Post: 06-09-2007, 05:16 AM
  5. Writing to Specific Locations
    By Spex in forum C Programming
    Replies: 5
    Last Post: 02-14-2002, 10:08 PM

Tags for this Thread