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