# 2-dem array

• 05-05-2002
alika
2-dem array
hi,
please i really need a help with this,
in my program i wrote an algorithm that searches a two dimentional array for ships. The array is a char array initialized with two character values - the value '~' to represent water, and the value 'x' to represent a square occupied by a ship. It contains five ships.
It works just fine.

#include
using namespace std;

int main() {

const int Size = 6;
char ocean[Size][Size]={
{'~', '~', 'x', 'x', '~', 'x'},
{'x', '~', '~', '~', '~', 'x'},
{'x', '~', 'x', 'x', '~', 'x'},
{'x', '~', '~', '~', '~', 'x'},
{'~', '~', '~', '~', '~', '~'},
{'~', 'x', 'x', 'x', 'x', '~'},
};

int i, j;
int ship = -1, length = 0;

for (i = 0; i < Size; i++) {
for (j = 0; j < Size; j++) {
if(ocean[j] == 'x' || ocean[j] == '~') {
if (ocean[j] == 'x') {
if (ship == 0)
ship = -1;
length++;
}
}else{
printf("Invalid Ship Placement\n");
}
}

if (ship == -1) {
printf("Found a ship, its length is %d\n",length);
}
ship = 0;
length = 0;
}
}

This is the output of this program:

Found a ship, its length is 2.
Found a ship, its length is 4.
Found a ship, its length is 3.
Found a ship, its length is 2.
Found a ship, its length is 4.

-------------------------------------------------------------------

I need to make two changes to my program:

1. The two dimentional array should be changed from a char array to a bool array
2. I should read my data from a text file instead of initializing it in code.

in my bool array, true should represent an element that is occupied by a ship, and false should represent an empty element ... make the new array 10 * 10(instead of 6*6)

just like this:

~~~~~X~~~~
XX~~~X~~~X
~~~X~X~~~X
~~~X~X~~~X
~~~~~~~~~~
XXXXX~~~XX
~~~~~~~~~~
X~~~~~~~~~
X~~~~~XXXX
X~~~~~~~~~

could you please point me .... i have no idea how to do it

alika
• 05-05-2002
jdinger
Making it a bool array would just be:

Code:

```bool ocean[5][5] {  {true, true, true, false, true},  {true, false, true, true, false},  {true, true, true, false, false}  {true, true, true, true, false}  {true, false, false, true, false} };```
To write/read it to/from a file just use CreateFile, WriteFile and ReadFile.
• 05-05-2002
Traveller
You define the boolean array like this: bool ocean[9][9];
to read info from a file use this:

#include<fstream.h>

void main()
{
fstream Game
Game.open("Game1.bin", ios :: in | ios :: binary); //assuming your going to use binary format.
for(int y = 0, y<10, y++)
for(int x = 0, x<10, x++)
Game.close();
}

Same idea for writing just use the write member:
Game.write((char *) &ocean[x][y], sizeof(bool));

Note: However when you open the file you need to use the (ios :: out | ios :: binary) statement as the second argument.
• 05-07-2002
alika
hi,
i greatly appreciate for your help ... thank you so much for your solutions.
I wrote it by following your solution and this is what i've done:

#include<iostream>
#include<fstream>using namespace std;
const int Size=6;
const char *filename = "ocean.txt";

fstream myFile;
myFile.open(filename, ios::in);
if (!myFile.is_open()) {
cout << "Input file opening failed.\n";
return false;
}

for (int y = 0; y < Size; y++)
for (int x = 0; x < Size; x++)
myFile >> Ocean[x][y];
return true;
}

int main() {
bool Ocean[Size][Size]={ false };