Thread: In a 2 dimensional array, my 0,0 spot keeps changing

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    In a 2 dimensional array, my 0,0 spot keeps changing

    I have a 10x10 array, initialized to all zeros.
    I create 2 random numbers for the purpose of guessing a position in the array.
    However, when I print the array, the 0,0 spot keeps shifting to match the 2nd random number generated.
    Is there something strange about the 0,0 spot?
    Here is my code:

    Code:
    seedrnd();
    for (x=0;x<2;x++)
    {
    randArray[x]=rnd(10);
    }
    for (x=0;x<2;x=x+1)
          {
              printf("randArray[%i] = %i\n",x,randArray[x]);
          }
    randRow=randArray[0];
    randColumn=randArray[1];
    printf("%i Random Row \n",randRow);
    printf("%i Random Column \n",randColumn);
    printf("[0][0] = %i\n",oppBoard[0][0]);
    printf("[0][1] = %i\n",oppBoard[0][1]);
    The 2nd to last print statement actually prints randColumn.
    The last print statement correctly prints 0.

    Any advice?
    Thanks...Brownie5

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Post the complete program.
    You miss key information about how you declared your arrays.
    Simply describing them won't work if your understanding is flawed.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You're not showing nearly enough code to see what you're actually doing. Nowhere in the code you posted is the oppBoard array getting populated.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    OK - I was trying to keep it simple in case there was something specific to the 0,0 spot.
    Here's the full program. It is a battleship game. Right now the array is just full of zeros because I pulled out the placement of the ships while I debug this 0,0 position issue. Doesn't matter. It still populates the 0,0 position in the obbBoard with the randColumn value every time it generates a new randColumn, but none of the other positions are affected.
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    main()
    {
    int row;
    int column;
    int a;
    int b;
    int k;
    int loopCount;
    a=0;
    b=0;
    k=0;
    char directionLetter;
    int finalRand;
    finalRand=0;
    int rnd(int range);
    void seedrnd(void);
    
    int oppBoard[10][10]; //Sets up the opponent’s array
    for(row=0;row<10;row=row+1) //Fills the array with zeros
    {
     for (column=0;column<10;column=column+1)
          {
           oppBoard[row][column]=0;
          }
    }
    
    for(row=0;row<10;row=row+1)
    {
     for (column=0;column<10;column=column+1)
          {
              printf("oppBoard[%i][%i] = %d\n", row,column, oppBoard[row][column] );
          }
    }
    /*
    BEGINNING OF GAMEPLAY
    */
    int timesPlayed;
    timesPlayed=0;
    //while(timesPlayed<100)
    //{
    int shotsFired;
    shotsFired=0;
    int totalHits;
    totalHits=0;
    int ship........;
    ship........=0;
    int randRow;
    randRow=0;
    int randColumn;
    randColumn=0;
    int numShipHit;
    numShipHit=0;
    long waitLoop;
    waitLoop=0;
    int x;
    int randArray[1];
    while(totalHits<5)
    {
    waitLoop=0;
    while(waitLoop<600000000)
    {
    waitLoop++;
    }
    seedrnd();
    for (x=0;x<2;x++)
    {
    randArray[x]=rnd(10);
    }
    for (x=0;x<2;x=x+1)
          {
              printf("randArray[%i] = %i\n",x,randArray[x]);
          }
    randRow=randArray[0];
    randColumn=randArray[1];
    printf("%i Random Row \n",randRow);
    printf("%i Random Column \n",randColumn);
    printf("[0][0] = %i\n",oppBoard[0][0]);
    printf("[0][1] = %i\n",oppBoard[0][1]);
    if (oppBoard[randRow][randColumn]==0)
    {
     shotsFired++;
     oppBoard[randRow][randColumn]=6;//A missed shot
     printf("MISS!\n");
    }
    else if (oppBoard[randRow][randColumn] < 6)
    {
    numShipHit = oppBoard[randRow][randColumn];
    oppBoard[randRow][randColumn]=numShipHit+10;//Hit
    printf("HIT!\n");
    shotsFired++;
    for(randRow=0;randRow<10;randRow=randRow+1) //Check the array for the hit ship
    {
     for (randColumn=0;randColumn<10;randColumn=randColumn+1)
          {
           if (oppBoard[randRow][randColumn]==numShipHit)
      {
      oppBoard[randRow][randColumn]=numShipHit+10;
      printf("Row Update!\n");
      }
          }
    }
    totalHits++;
    }
    }
    for(row=0;row<10;row=row+1)
    {
     for (column=0;column<10;column=column+1)
          {
              printf("oppBoard[%i][%i] = %d\n", row,column, oppBoard[row][column] );
          }
    }
    //}
    }//End of Main file
    int rnd(int range)
    {
     int r;
     r=rand()%range;
     return(r);
    }
    void seedrnd(void)
    {
     srand((unsigned)time(NULL));
    }
    Last edited by brownie5; 01-12-2015 at 10:05 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    > int randArray[1];
    How many valid subscripts does this have?
    How many are you using?
    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.

  6. #6
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Quote Originally Posted by Salem View Post
    > int randArray[1];
    How many valid subscripts does this have?
    How many are you using?
    One subscript with 2 elements used in lines 77 and 78. The 2nd element in RandArray is somehow being placed in OppBoard [0][0].

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,867
    Quote Originally Posted by brownie5 View Post
    One subscript with 2 elements used in lines 77 and 78. The 2nd element in RandArray is somehow being placed in OppBoard [0][0].
    It is a array with only 1 element with the index of 0 zero.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    It is a array with only 1 element with the index of 0 zero.

    Tim S.
    OMG thanks! I'm new to this. I knew it had to be something dumb but the oddness of the error had me looking in the wrong place. I forgot that an array with 2 elements had to be initialized with a 2 even though the index positions are 0 and 1!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning One dimensional array to two dimensional array
    By lightning_star in forum C Programming
    Replies: 1
    Last Post: 03-19-2014, 09:44 PM
  2. Replies: 12
    Last Post: 09-02-2013, 07:50 PM
  3. Replies: 4
    Last Post: 09-02-2013, 11:19 AM
  4. printing array content - spot the error
    By nosnowking in forum C Programming
    Replies: 5
    Last Post: 02-04-2009, 05:50 PM
  5. Changing from one dimensional arrays to two
    By Schmag in forum C Programming
    Replies: 5
    Last Post: 05-14-2007, 02:29 PM