Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,940 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,430 people online right now. Registration is fast and FREE... Join Now!




2d array

 
Reply to this topicStart new topic

2d array, putting a word into a 2d array

TWhitt24
31 Jan, 2008 - 11:08 AM
Post #1

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 5


My Contributions
I really need help on this program. I am supposed to put a list of words into a 2d array in a grid type fashion. I am a novice programmer and I am completely stuck. Any help would be awesome.

Here is the code that I have come up with so far.

CODE

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

const int ROWS = 10;
const int COLUMS = 10;
const int MAX_WORDS = 10;

void GetFileName(string filename, ifstream& fin, string wordList[]);
void ProcessTxtFile(string wordList[], int& location, int& location2, ifstream& fin, char puzzle[][COLUMS], int border[], int& direction);
void FillPuzzle(char puzzle[][COLUMS], char filler);
void DisplayPuzzle(char puzzle[][COLUMS], int border[]);
void PutWordInPuzzle(int& location, int& location2, int& direction, char puzzle[][COLUMS]);


void main()
{
    
    ifstream fin;
    string filename;
    int direction = 0;
    int location = 0;
    int location2 = 0;
    char filler = '.';
    char puzzle[ROWS][COLUMS];
    int border[10] = {0,1,2,3,4,5,6,7,8,9};
    string wordList[MAX_WORDS];
    
    GetFileName(filename, fin, wordList);
    FillPuzzle(puzzle, filler);
    ProcessTxtFile(wordList, location, location2, fin, puzzle, border, direction);
    
}
void GetFileName(string filename, ifstream& fin, string wordList[])
{
    string word;
    int count = 0;
    int index = 0;
    do
    {
        cout<< "Please enter the filename with the word list: ";
        cin >> filename;
        fin.open( filename.c_str() );
        if (filename != "words.txt")
        {
            cerr<< "File does not exsist. Please try again." << endl;
            fin.close();
            fin.clear();
        }
    }
    while ( filename != "words.txt" );
    fin >> wordList[index];
    while ( !fin.eof() )
    {
        index++;
        fin >> wordList[index];
    }
    fin.close();
    system("cls");
}

void FillPuzzle(char puzzle[][COLUMS], char filler)
{
    /*srand(unsigned (time(NULL)));
    (rand() %26 + 1);*/
    for(int r=1; r<=ROWS; r++)
    {
        for(int c=1; c<=COLUMS; c++)
        {
            puzzle[r-1][c-1]= filler;
        }
    }
}



void DisplayPuzzle(char puzzle[][COLUMS], int border[])
{
    cout << "             WORD SEARCH          " << endl;    
    cout << "    0  1  2  3  4  5  6  7  8  9  " << endl;
    cout << "   -------------------------------" << endl;
    
    for (int r = 1; r <= ROWS; r++)
    {
        setiosflags(ios::right);
        cout<< border[r-1] << " | ";
        for(int c = 1; c <= COLUMS; c++)
        {
            cout<< puzzle[r-1][c-1] << "  ";
            
        }
        cout<< "| " << border[r-1];
        cout << endl;
    }    
    cout << "   -------------------------------" << endl;
    cout << "    0  1  2  3  4  5  6  7  8  9  " << endl;
}
void ProcessTxtFile(string wordList[], int& location, int& location2, ifstream& fin, char puzzle[][COLUMS], int border[], int& direction)
{
    
    for(int i=0; i < 5; i++)
    {
        DisplayPuzzle(puzzle, border);
        cout<< "What row will " << wordList[i] << " begin on? ";
        cin >> location;
        cout<< "What column will " << wordList[i] << " begin on? ";
        cin >> location2;
        cout<< "What direction will it go in? " << endl
            << "1  2  3" << endl << " \ | / " << endl
            << "4-- --5" << endl << " / | \ " << endl
            << "6  7  8" << endl << "Choose direction 1 to 8: ";
        cin >> direction;
        system("cls");
        PutWordInPuzzle(location, location2, direction, puzzle);

    }
    
}
void PutWordInPuzzle(int& location, int& location2, int& direction, char puzzle[][COLUMS])
{
    switch (direction)
    {
        case 1:
            for(int r=location; r<ROWS; r--)
                {
                    for(int c=location2; c<COLUMS; c--)
                    {
                        puzzle[r][c]= 'a';
                    }
                }
        break;
    }
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 1 Feb, 2008 - 01:30 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: 2d Array
1 Feb, 2008 - 01:05 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, please code.gif

I don't see what is the problem, assign values(chars) to the 2d array. Just try to think the way you'll assign them. Say you have one word and you want to assign that word diagonally to the matrix, so I suggest you create a function assignDiagonally, you can use loops in the function. So another function for assigning vertically perhaps... etc.

Now that is easier said than done, but it's an idea and yes you'll need a lot of checks, like length of the words.. etc.

Hope this helps a bit.
User is offlineProfile CardPM
+Quote Post

TWhitt24
RE: 2d Array
1 Feb, 2008 - 03:27 PM
Post #3

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 5


My Contributions
Thanks but the problem is that I don't know how to put the words in vertically or diaginally. What I have to do is take a list of words and put them into the 2d array so that each letter of the words has its own spot.
Kind of like:

h e l l o
h o w a
r e y o u

With each letter being a spot in the array. If you any suggestions on what I can do, it would be awesome cause right know I have no idea what to do.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: 2d Array
1 Feb, 2008 - 03:43 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
try some thing like this, just a thought for assigning word to the main diagonal:

CODE

int i,j;
for(i=0, j=0; i<WORD_LENGTH && j<WORD_LENGTH; i++, j++ )
{
     puzzle[i][j]=wordList[0][i];
}


say wordList[0] is hoee
then puzzle[][] would be

[h] [] [] [] []
[] [o] [] [] []
[] [] [e] [] []
[] [] [] [e] []
[] [] [] [] []
User is offlineProfile CardPM
+Quote Post

TWhitt24
RE: 2d Array
1 Feb, 2008 - 03:58 PM
Post #5

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 5


My Contributions
That helps, but when ever I do that I just get the whole word instead of each in dividual letter.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: 2d Array
1 Feb, 2008 - 04:08 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Please note that in the code I used wordList[0][i] not just wordList[0].
Any updated code would be great to see, if you've made some changes.
User is offlineProfile CardPM
+Quote Post

TWhitt24
RE: 2d Array
1 Feb, 2008 - 04:25 PM
Post #7

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 5


My Contributions
That works sort. Instead of each individual letter I only get the first letter of the first word on the list. As for the code, unfortunately all my code is on my lap top, and I'm using my home computer at the moment with no way to connect to the internet with my laptop.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month