Battleship: weird values showing up in array

Hey I'm working on code for randomly placing ships in a game of battleship. I've set up a 10x10 matrix with all values initially set to zero, to be changed to 1 when a ship is placed. Everything is working fine, except that the array is returning strange values for the last number in row 9 and the entire row 10.

The following is an example of the flawed array:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 2009288258
10 10 0 42477224 2147344384 2293680 4198987 1 4079016 4074632

Here's the portion of code for placing the first ship (an aircraft carrier taking up 5 spaces):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    const int nNumRows = 10; 
    const int nNumCols = 10; 
    int cAircraftCarrier[nNumRows][nNumCols] = {0}; 
    int randVorH; //Choosing vertical or horizontal placement
    srand ( time (NULL) ); 
    randVorH = rand() % 2 + 1;
    switch (randVorH)
    {
        case 1: //Vertical Placement
        {
            int randCol, randRow;
            srand ( time (NULL) );
            randCol = rand() % 10 + 1; //Choosing start column 
            srand ( time (NULL) );
            randRow = rand() % 6 + 1; //Choosing start row 
            for (int nCol = randCol; nCol == randCol; nCol++)
                for (int nRow = randRow; nRow < (randRow+5); nRow++)
                {
                    cAircraftCarrier[nRow][nCol] = 1;
                }
            for (int nRow = 1; nRow < (nNumRows+1); nRow++)
            {
                for (int nCol = 1; nCol < (nNumCols+1); nCol++)
                {
                    cout << cAircraftCarrier[nRow][nCol] << " ";
                }
                cout << endl;
            }
            break;
        }
        case 2: //Horizontal Placement
        {
            int randCol, randRow;
            srand ( time (NULL) ) ;
            randCol = rand() % 6 + 1;
            srand ( time (NULL) );
            randRow = rand() % 10 + 1;
            for (int nRow = randRow; nRow == randRow; nRow++)
                for (int nCol = randCol; nCol < (randCol+5); nCol++)
                {
                    cAircraftCarrier[nRow][nCol] = 1;
                }
            for (int nRow = 1; nRow < (nNumRows+1); nRow++)
            {
                for (int nCol = 1; nCol < (nNumCols+1); nCol++)
                {
                    cout << cAircraftCarrier[nRow][nCol] << " ";
                }
                cout << endl;
            }
            break;
        }
        default:
            cout << "error in aircraft carrier placement" << endl;
    }
    return 0;
}


I'm brand new to both programming and C++, so this might not be the best code to begin with... I just can't figure out where the weird values are coming from.
Scratch my earlier post. I am not sure why it doesn't work the way you have it but I switched it to the following and it is working with me aside from minor errors. For instance I compiled it one time and the ship was on two different lines which I am guessing is not supposed to happen. This code gets rid of the odd values at the end though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    const int nNumRows = 10; 
    const int nNumCols = 10; 
    int cAircraftCarrier[nNumRows][nNumCols] = {0}; 
    int randVorH; //Choosing vertical or horizontal placement
    srand ( time (NULL) ); 
    randVorH = rand() % 2 + 1;
    switch (randVorH)
    {
        case 1: //Vertical Placement
        {
            int randCol, randRow;
            srand ( time (NULL) );
            randCol = rand() % 10 + 1; //Choosing start column 
            srand ( time (NULL) );
            randRow = rand() % 6 + 1; //Choosing start row 
            for (int nCol = randCol; nCol == randCol; nCol++)
                for (int nRow = randRow; nRow < (randRow+5); nRow++)
                {
                    cAircraftCarrier[nRow][nCol] = 1;
                }
            for (int nRow = 0; nRow < (nNumRows); nRow++)
            {
                for (int nCol = 0; nCol < (nNumCols); nCol++)
                {
                    cout << cAircraftCarrier[nRow][nCol] << " ";
                }
                cout << endl;
            }
            break;
        }
        case 2: //Horizontal Placement
        {
            int randCol, randRow;
            srand ( time (NULL) ) ;
            randCol = rand() % 6 + 1;
            srand ( time (NULL) );
            randRow = rand() % 10 + 1;
            for (int nRow = randRow; nRow == randRow; nRow++)
                for (int nCol = randCol; nCol < (randCol+5); nCol++)
                {
                    cAircraftCarrier[nRow][nCol] = 1;
                }
            for (int nRow = 0; nRow < (nNumRows); nRow++)
            {
                for (int nCol = 0; nCol < (nNumCols); nCol++)
                {
                    cout << cAircraftCarrier[nRow][nCol] << " ";
                }
                cout << endl;
            }
            break;
        }
        default:
            cout << "error in aircraft carrier placement" << endl;
    }
    return 0;
}
Last edited on
Thank you so much! Yeah, I'm not sure why it wasn't working right earlier, but that fixes it, and I think I know what's making it run onto the next line. Really appreciate the help.
Topic archived. No new replies allowed.