My 2d array is not out putting all the words

Pages: 12
My code is this:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


int main()
{
    int numWords;
    std::vector<std::string> words;
    std::cout<<"How many words do you want?" << std::endl;
    std::cin>>numWords;

    //get words from user to computer
    for(int i = 0; i < numWords; i++)
    {
        std::string word;
        std::cout<<"Enter word n" << i+1 << ": ";
        std::cin>>word;
        words.push_back(word);
    }

    //determine size of wordsearch *3
    int size = 0;
    for(int i = 0; i < numWords; i++)
    {
        if(words[i].length() > size)
        {
            size = words[i].length()*3;
        }
    }

    //create word search
    char wordSearch[size][size];
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            wordSearch[i][j] = '-';
        }
    }

    //randomly insert words into word search
    srand (time(NULL));
    for(int i = 0; i < numWords; i++)
    {
        int rand1 = rand() % size;
        int rand2 = rand() % size;
        int rand3 = rand() % 2;
        int rand4 = rand() % 2;

        if(rand3 == 0)
        {
            //insert horizontally
            for(int j = 0; j < words[i].length(); j++)
            {
                if(rand4 == 0)
                {
                    //insert forwards
                    wordSearch[rand1][(rand2+j)%size] = words[i][j];
                }
                else
                {
                    //insert backwards
                    wordSearch[rand1][(rand2-j+size)%size] = words[i][j];
                }
            }
        }
        else
        {
            //insert vertically
            for(int j = 0; j < words[i].length(); j++)
            {
                if(rand4 == 0)
                {
                    //insert downwards
                    wordSearch[(rand1+j)%size][rand2] = words[i][j];
                }
                else
                {
                    //insert upwards
                    wordSearch[(rand1-j+size)%size][rand2] = words[i][j];
                }
            }
        }
    }

    //print word search
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            std::cout<<wordSearch[i][j] << " ";
        }
        std::cout<<std::endl;
    }

    return 0;
}


The only problem is that It outputs the word out of range sometimes (the random letters I am going to add them later), like this:


How many words do you want?
3
Enter word n1: num
Enter word n2: dumb
Enter word n3: just
m - - - u - - - - 
- - - - s - - - - 
- - - - t d u m b 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
n - - - - - - - - 
u - - - j - - - - 

As you see just appears in the last but prints the other 3letters in the beginning.
What can I do in this case?
Last edited on
If you had done that from the start, posting the entire assignment and sample output, it would have made things much easier.

You are making a mistake most beginners make. Writing code without have a program design idea. You should write this out on paper. After you have an idea of what the program is supposed to do, step by step, you code each step and test, test and TEST the steps. When you are sure each step works you can move on to the next step of your program design.

A possible partial design, with rudimentary steps to create the code. My quick program design idea is based on the assignment as given:

1. Design a menu system to display possible options and accept user input.

What menu system? So far you are shot-gunning your code, without a plan.

2. Ask the user how many words they want to enter.

Do any of the words contain spaces? That changes how to get the input. std::cin, std::getline. Could there be file input?

3. Create a container to store the retrieved words.

A vector of C++ strings is a good idea. Either create a sized vector and assign the input to the elements, or push back each string as it is entered.

4. Figure out the dimensions of your 2D container. AKA the puzzle grid.

Loop through your vector and figure out which word is the largest. That size is used to create your 2D container.

Now, about your 2D container. Because of the requirements of randomly placing the words into your 2D container a vector of strings is not the best choice. A std::vector<std::vector<char>> would be a better fit for placing individual characters.

5. Loop through the word list and with each word determine how you want to place it in the puzzle grid. Horizontal, vertical or diagonal + forwards or backwards.

Do you know how to generate random numbers? To do it the C++ way?

5a. Now comes the hard part, the heart of the code. Figuring out if trying to place a word will conflict with previously placed word(s) AND/OR if the location will go off the boundaries of the puzzle grid. If it does, get another random location and direction and test again.

Loop through the word list. The first word placed is the anchor. You only have to test if the location will go off the grid.

6. When all the words have been properly placed display the puzzle grid and the word list.

You have already done steps 2 & 3.

Really recommended to also write the output to a file.
Last edited on
Thank you for the suggestions, I am now having problems with the part 5 as you can see in here:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


int main()
{
    int numWords;
    std::vector<std::string> words;
    std::cout<<"How many words do you want?" << std::endl;
    std::cin>>numWords;

    //get words from user to computer
    for(int i = 0; i < numWords; i++)
    {
        std::string word;
        std::cout<<"Enter word n" << i+1 << ": ";
        std::cin>>word;
        words.push_back(word);
    }

    //determine size of wordsearch *3
    int size = 0;
    for(int i = 0; i < numWords; i++)
    {
        if(words[i].length() > size)
        {
            size = words[i].length()*3;
        }
    }

    //create word search
    char wordSearch[size][size];
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            wordSearch[i][j] = '-';
        }
    }

    //randomly insert words into word search
    srand (time(NULL));
    for(int i = 0; i < numWords; i++)
    {
        int rand1 = rand() % size;
        int rand2 = rand() % size;
        int rand3 = rand() % 2;
        int rand4 = rand() % 2;

        if(rand3 == 0)
        {
            //insert horizontally
            for(int j = 0; j < words[i].length(); j++)
            {
                if(rand4 == 0)
                {
                    //insert forwards
                    wordSearch[rand1][(rand2+j)%size] = words[i][j];
                }
                else
                {
                    //insert backwards
                    wordSearch[rand1][(rand2-j+size)%size] = words[i][j];
                }
            }
        }
        else
        {
            //insert vertically
            for(int j = 0; j < words[i].length(); j++)
            {
                if(rand4 == 0)
                {
                    //insert downwards
                    wordSearch[(rand1+j)%size][rand2] = words[i][j];
                }
                else
                {
                    //insert upwards
                    wordSearch[(rand1-j+size)%size][rand2] = words[i][j];
                }
            }
        }
    }

    //print word search
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            std::cout<<wordSearch[i][j] << " ";
        }
        std::cout<<std::endl;
    }

    return 0;
}




How many words do you want?
3
Enter word n1: num
Enter word n2: dumb
Enter word n3: just
m - - - u - - - - 
- - - - s - - - - 
- - - - t d u m b 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
n - - - - - - - - 
u - - - j - - - - 


The output is going off boundaries.
Your inserts have a % size when you access the array. This is good because it's safe, but you shouldn't need it if you choose a proper location.

Make sure your starting position (rand) + string.size() < array.size()
I really do suggest dumping the C library random functions and learning to use C++ <random> library.

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

Random Number Generation in C++11
https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf

Theoretically with each word placement there could/should be three things randomly generated:

1. The element for the starting location. Yes, that is actually two numbers, but it is one location within the puzzle grid.

2. The direction of placement: horizontal, vertical, diagonal increase (column number increases as each letter is placed), diagonal decrease.

3. The direction the word is placed. Forwards or backwards.

When you know the parameters of where and how the current word is to be placed you then try to figure out if the word:

1. goes out of bounds
2. overlaps another already placed word.

If either condition is true you start over with a new location and direction and try again.

If the word doesn't go out of bounds and there is no conflicting word you loop though the extent of the word and place char by char the word into the puzzle grid.

Mark the word as placed and Lather, Rise and Repeat until all the words are on the grid.

As I said earlier, having an idea of the what needs to be done before you code is helpful in the extreme. Coding and then adding features without a plan is gonna bite you on the tuchus.
Topic archived. No new replies allowed.
Pages: 12