2-D Arrays Help Please!

Hi I am having trouble converting my single array code into a 2-d Array.

The following is my code for a single array for my battle ship program:
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
67
68
#include <iostream>
#include <ctime> 
using namespace std; 

int main()
{
    srand (time(NULL));
    const int SIZE= 10;
    char board [SIZE];
    int answer;
    fill(board, board+10, 'O');
    const int TOTAL_ATTEMPTS = 3;
    int attempts = TOTAL_ATTEMPTS;
    int index = rand() % 10;
    
    cout<<"Let's Play Battleship!!" << endl;

    for (int i=0; i<SIZE; i++)
    {
        cout << board[i] <<" ";
    }

    cout<< endl;
    
    
    do
    {   cout <<"Enter a guess from 0 to 10: ";
         cin >> answer;
      
        if (board[answer]=='X')
        {
            cout <<"You already guessed that location landlubber!"<< endl; 
            cout <<"Enter a guess from 0 to 10: ";
            cin >> answer;
        }
        if (answer >= 0 && answer <= SIZE)
        { board[answer] = 'X';
        }
      
        for (int i=0; i<SIZE; i++)
        {
            cout << board[i] <<" ";
        }        
        cout<< endl; 
        
        if(answer!=index)
        {
            cout<<"You missed by Battleship!" << endl; 
        }
        if(answer==index)
        {
            cout<<"YOU SANK MY BATTLESHIP!  Well done." << endl; 
            
            cout << "GAME OVER" << endl; 
        }
        
   
    }while ( --attempts && answer != index );
    
    if(answer!=index)
    {
    cout << "HA! You are out of guesses - I live to sail another day!" << endl;
    
    cout << "GAME OVER" << endl; 
        return 0;
    }

}

For the 2-array program I have to do the following:

Create an init_board function that initializes your ocean (board) to all ‘O’ characters (you will need to pass in a two dimensional array). You can assume the board will always be square and you should use a single global constant for the size.
Create a print_board function that prints your Battleship board
Generate two random numbers (one for the row of the ship and one for the column)
Set up game play (using function(s) where useful) as follows:
Each turn, the user enters a guess for row and column
If they hit the ship, they win, end the game (the winning guess counts as a turn)
If they guess a spot they already guessed, print a message (this counts as a turn)
If they guess outside the bounds of the array, print a message (this counts as a turn)
If they miss, mark the spot in the array as missed (this counts as a turn)
Repeat until the player guesses the correct location.
When the user wins, print the total number of turn

This is what I currently have for my 2-array code. I do not know where to go from here: I need to do my print function but I am having trouble writing it.

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
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <ctime>
using namespace std;

const int S=4;// can't change size once declared. Global any fucntion can use this v.


void init_board(char nums [][S]);

void print_board();

int main()
{
	srand (time(NULL));
	int indexrow = rand() % 10;
	int indexcolumn=rand() % 10;
	int row, column;
    
    char ocean[S] [S];
	
    cout<< "Let's Play Battleship!!!"<< endl;
    
    init_board(ocean);
	print_board();
    
    
    
    cout<<"Enter a guess for the boat row: "<<endl;
    cin>> row;
    
    cout<<"Enter a guess for the boat column:" <<endl;
    cin>> column;
    
    /*
     
     for(int row = 0; row <4; row++)
     {
     
     
     }
     for(int column = 0; column <4; column++)
     {
     
     }*/
    
    
    
    
    return 0;
}

void init_board(char nums [][S])/// fucntion defen
{
    
    for (int i=0; i<4; i++)
        
    { 
        for(int j=0; j<4; j++)
        {
            nums[i] [j]='O';
          cout<< nums[i][j]<< " ";
            
        }
      cout<< endl; 
        
    }
    
    
}
void print_board()
{

	
}


Last edited on
This is my most update code:

My question is how do I call print_board function so that it prints like this:

Let's Play Battleship!!!
O O O O
O O O O
O O O O
O O O O

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
#include <iostream>
#include <ctime>
using namespace std; 

const int S2=4;
const int S4=4;

void init_board(int nums [][S2]);

void print_board();

int main()
{
	srand (time(NULL));
	int indexrow = rand() % 10;
	int indexcolumn=rand() % 10;


    int row, column;

cout<< "Let's Play Battleship!!!"<< endl;


cout<<"Enter a guess for the boat row: "<<endl;
cin>> row;

cout<<"Enter a guess for the boat column:" <<endl;
cin>> column;

    
   
    return 0;
}



Working Process: Still stuck on how to print init_board(int nums [][S2]);
and will I need to use a do or while loop so that the program keeps looping also I do not know where to put the following questions in the program:
cout<<"Enter a guess for the boat row: "<<endl;
cin>> row;

cout<<"Enter a guess for the boat column:" <<endl;
cin>> column

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
oid print_board();

int main()
{
	srand (time(NULL));
	int indexrow = rand() % 10;
	int indexcolumn=rand() % 10;
	int row, column;

	
cout<< "Let's Play Battleship!!!"<< endl;


for(int row = 0; row <10; row++) 
{


}
for(int column = 0; column <10; column++) 
{

}
cout<<"Enter a guess for the boat row: "<<endl;
cin>> row;

cout<<"Enter a guess for the boat column:" <<endl;
cin>> column;


   
    return 0;
}
Topic archived. No new replies allowed.