filing

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 :
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
#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 ??

1
2
3
4
std::vector< std::string > grid;
std::string line;
while( std::getline(std::cin,line) )
   grid.push_back(line);
Last edited on
is there any other way to do it ?
Topic archived. No new replies allowed.