2-dem array

This is a discussion on 2-dem array within the C++ Programming forums, part of the General Programming Boards category; hi, please i really need a help with this, in my program i wrote an algorithm that searches a two ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    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

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    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.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.read((char *) &ocean[x][y], sizeof(bool));
    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.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    2
    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";
    bool ReadData(bool Ocean[Size][Size]) {

    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 };
    if (!ReadData(Ocean))
    return 1;
    return 0;
    }

    could you please let me know if i have some mistakes
    best regards,
    alika

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21