hey guys ! i have been given an assignment as pasted below :
You will be given two files.

Grid.txt will contain a grid of letters (as shown below)
j q v r l k u v e r s i o n l

d v k e r n e l t u m e d i a
i e c k s n d y v x k d n w e
r k u c w g e c b a l z u s j
r p h a l d a t a o t w h x d
q x p h d s i f w m a c a p a
w d p b d q f r t o z r r o o
p a o m o u s e f n r l d t l
r t l s c a a t c i b k w p n
o a i u u p z n j t l z a a w
g b a v m b n i z o x e r l o
r a m g e e a r n r j g e q d
a s e w n y c p y c v b b t z
m e q d t n s e q j f g v x b
v x i n i v b o i p v v t j b

Words.txt: containing a list of words as shown below.
data database document download email file hacker hardware kernel keyboard laptop media monitor mouse network printer program scan undo version

You have to write a program which find all the words given in Words.txt from the gird given in Grid.txt. The words can be in any direction - horizontal, vertical or diagonal. It should write a new file Output.txt which writes a modified grid such that letters not in any word are replaced by blanks. (See example below)



      • r - k - v e r s i o n -

    • k e r n e l - - m e d i a


      • k - n - y - - - - n - -


      • c - - e - b - - - u - -


      • a - d a t a o - - h - d


      • h - - - - w m a - a p a
  • d - - d - - r - o - r r o o
    p a - m o u s e f n r - d t l
    r t l - c - - t - i - k w p n
    o a i - u - - n - t l - a a w
    g b a - m - n i - o - e r l o
    r a m - e - a r - r - - e - d
    a s e - n - c p - - - - - - -
    m e - - t - s - - - - - - - -













                            • -

Your code should work for a grid of any size and a word list of any size as well (Use dynamic memory allocation, static arrays are not allowed; wasting memoery is not allowed). Your program should be able to figure out the size of the grid by reading the file (No user input!)

So far i have written this much code :

#include <iostream>
#include <fstream>
using namespace std;
char** Read_Grid (ifstream &inFile, int Rows , int Cols);
int main ()
{
    int Rows = 0  ;
    int Cols = 0 ;
    ifstream inFile ;
    inFile.open("grid.txt");
    char** ptr = Read_Grid(inFile,Rows,Cols);
    for (int i = 0 ; i<Rows ; i++ )
    {
        for (int j = 0 ; j<Cols ; j ++ )
        {
            cout << ptr[i][j] << " " ;
        }
        cout << endl;
    }

    system("pause");
    return 0 ;
}
char** Read_Grid (ifstream &inFile, int Rows, int Cols )
{

    char ** grid = new char *[Rows];

    if (!inFile.is_open())
    {
        cout << " opening of file failed " << endl;
    }
    for (int i = 0 ; i<Rows ; i++ )
    {
        grid[i] = new char [Cols];
    }

    for (int i = 0 ; i<Rows ; i++ )
    {
        for ( int j = 0 ; j<Cols ; j++ )
        {
            inFile >> grid[i][j] ;
        }
    }

    return grid ;
}

i have a problem that it does not display anything .,. how will my code be able to know the size of the grid ??

Edited 3 Years Ago by waqas.zafar.125

My first thought is to make a guess about the width of the grid and allocate that for the first line, and if that's not enough allocate a larger array and copy what you have read so far into it. Repeat that until you have read the entire first line, then you know exactly how wide the grid is. Knowing that, allocate another array that is exactly the right size and copy the first line into it. This way you aren't wasting any memory once the first line has been read, but your initial guesses might be considered wasteful when they are larger than needed.

Next, make a guess about the height of the grid and allocate a char* array. For each line you can allocate exactly what you need and put the pointer in your char* array. If your guess about the height was too small, allocate again and copy the pointers into the new array.

If you consider the guessing to be too much like wasting memory, then you also have the alternative of using a linked list. Each time you allocate you will be allocating memory for a char plus a pointer to the next bit of memory. It seems that you would end up using more memory this way, but at least you will be using every bit of memory that you allocate. If you give each link three pointers, one to the next character to the right, one to the next character downward, and one for the next character diagonally, then it should be no trouble to search the grid.

A third alternative is to read your grid file once to find out how big the grid is, then read the file a second time to store the content in dynamically allocated memory that is the perfect size. This is the easiest way and from the wording of your instructions it might be what they want you to do.

Are you allowed to use std::string and/or std::vector?

If so, you can use getline to read each line into a std::string and add them to a std::vector:

std::vector< std::string > grid;
std::string line;
while ( true )
{
    getline( file, line );
    if ( line.empty() )
        break;

    grid.push_back( line );
}

This reads all the lines from a file and stops when it gets to a blank line. The characters can still be accessed using the notation grid[i][j]. The number of rows and columns can be found by using the size() methods of std::vector and std::string. If you aren't allowed to use vector or string that won't be much help, unfortunately.

As an asside, you can also do this nicely using STL algorithms. This will read the whole file into the grid object ( using namespace std ):

vector< string > grid;
copy( istream_iterator< string >( file ), istream_iterator< string >(), back_inserter( grid ) );

Just for reference though, I don't think this is what your professor was after :)

This article has been dead for over six months. Start a new discussion instead.