Having problems on the output

Hello, I have this code but I am having problem with the output as it keeps getting out of the grid order and is not outputting all the words.
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()*5;
        }
    }

    //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: max
Enter word n2: lecl
Enter word n3: win
- - - - - - - - - 
- - - - - - - l c 
e l - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - w i n - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 

The output should be like this (the order does not matter as it has to be random the only thing that it matters is the words to be under the bounds of the grid).

How many words do you want?
3
Enter word n1: max
Enter word n2: leclerc
Enter word n3: fun
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - m - - - - - - - 
- - - - - - - a - - - - - - - 
- - - - - - - x - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - - - - - 
- - - - - - - - - - - n - - - 
- - - - - - - - - - - u - - - 
- - - - - - - - - - - f - - - 
- - - - - - - - - - - - - - - 
 - - - - - - - - l e c l e r c 
Last edited on
L34. This is non-standard C++ - the size of an array needs to be determined at compile-time. Changing this to a vector and with some refactoring for C++ random and condition simplification:

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
#include <iostream>
#include <string>
#include <vector>
#include <random>

std::mt19937 rng(std::random_device {}());

int main() {
	size_t numWords {}, size {};

	std::cout << "How many words do you want: ";
	std::cin >> numWords;

	std::vector<std::string> words(numWords);

	for (size_t i { }; auto& w : words) {
		std::cout << "Enter word " << ++i << ": ";
		std::cin >> w;

		if (w.size() > size)
			size = w.size() * 5;
	}

	std::vector<std::vector<char>> wordSearch(size, std::vector<char>(size, '-'));
	std::uniform_int_distribution dir(0, 3);
	std::uniform_int_distribution<size_t> coord(0, size - 1);

	for (const auto& w : words) {
		const auto x { coord(rng) };
		const auto y { coord(rng) };

		switch (dir(rng)) {
			case 0:
				for (size_t j {}; j < w.length(); ++j)
					//insert forwards
					wordSearch[x][(y + j) % size] = w[j];

				break;

			case 1:
				for (size_t j {}; j < w.length(); ++j)
					//insert backwards
					wordSearch[x][(y - j + size) % size] = w[j];

				break;

			case 2:
				for (size_t j {}; j < w.length(); ++j)
				    //insert downwards
					wordSearch[(x + j) % size][y] = w[j];

			break;

			case 3:
				for (size_t j {}; j < w.length(); ++j)
					//insert upwards
					wordSearch[(x - j + size) % size][y] = w[j];

				break;
		}
	}

	for (size_t i {}; i < size; ++i) {
		for (size_t j {}; j < size; ++j)
			std::cout << wordSearch[i][j] << ' ';

		std::cout << '\n';
	}
}


which gives a sample output of:


How many words do you want?
3
Enter word n1: max
Enter word n2: leclerc
Enter word n3: fun
- - - l - - - - - - - - - m -
- - - e - - - - - - - - - a -
- - - r - - - - - - - - - x -
- - - c - - - - - - - - - - -
- - f - - - - - - - - - - - -
- - u - - - - - - - - - - - -
- - n - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - l - - - - - - - - - - -
- - - e - - - - - - - - - - -
- - - c - - - - - - - - - - -


with all three words displayed.

There is a problem, though, were words overlap/cross each other as below:


How many words do you want?
3
Enter word n1: max
Enter word n2: leclerc
Enter word n3: fun
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - x
l e r c - - - - - - - - l e c
- - - - - - - - - - - - - - m
- - - - - n u f - - - - - - -


where the a in max has been changed to the c from leclerc
Last edited on
And with some overlap checking, then something like:

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <utility>

std::mt19937 rng(std::random_device {}());

using Matrix = std::vector<std::vector<char>>;

auto xy(size_t m) {
	static std::uniform_int_distribution<size_t> coord(0, m - 1);

	return std::pair { coord(rng), coord(rng) };
}

void forw(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok{}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[x][(y + j) % m.size()] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[x][(y + j) % m.size()] = w[j];
	}
}

void back(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[x][(y - j + m.size()) % m.size()] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[x][(y - j + m.size()) % m.size()] = w[j];
	}
}

void down(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[(x + j) % m.size()][y]}; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[(x + j) % m.size()][y] = w[j];
	}
}

void up(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[(x - j + m.size()) % m.size()][y] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[(x - j + m.size()) % m.size()][y] = w[j];
	}
}

int main() {
	constexpr size_t Expand { 5 };
	size_t numWords {}, size {};

	std::cout << "How many words do you want: ";
	std::cin >> numWords;

	std::vector<std::string> words(numWords);

	for (size_t i { }; auto& w : words) {
		std::cout << "Enter word " << ++i << ": ";
		std::cin >> w;

		if (w.size() > size)
			size = w.size() * Expand;
	}

	Matrix wordSearch(size, std::vector<char>(size, '-'));
	std::uniform_int_distribution dir(0, 3);

	for (const auto& w : words) {
		switch (dir(rng)) {
			case 0:
				forw(w, wordSearch);
				break;

			case 1:
				back(w, wordSearch);
				break;

			case 2:
				down(w, wordSearch);
				break;

			case 3:
				up(w, wordSearch);
				break;
		}
	}

	for (size_t i {}; i < size; ++i) {
		for (size_t j {}; j < size; ++j)
			std::cout << wordSearch[i][j] << ' ';

		std::cout << '\n';
	}
}

Last edited on
And to finish it off with assigning random chars to to unused elements:

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <utility>

std::mt19937 rng(std::random_device {}());

using Matrix = std::vector<std::vector<char>>;

auto xy(size_t m) {
	static std::uniform_int_distribution<size_t> coord(0, m - 1);

	return std::pair { coord(rng), coord(rng) };
}

void forw(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok{}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[x][(y + j) % m.size()] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[x][(y + j) % m.size()] = w[j];
	}
}

void back(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[x][(y - j + m.size()) % m.size()] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[x][(y - j + m.size()) % m.size()] = w[j];
	}
}

void down(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[(x + j) % m.size()][y]}; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[(x + j) % m.size()][y] = w[j];
	}
}

void up(const std::string& w, Matrix& m) {
	static std::uniform_int_distribution<size_t> coord(0, m.size() - 1);

	for (bool ok {}; !ok; ) {
		const auto [x, y] {xy(m.size())};

		ok = true;

		for (size_t j {}; ok && j < w.length(); ++j)
			if (const auto& c { m[(x - j + m.size()) % m.size()][y] }; c != '-' && c != w[j])
				ok = false;

		if (ok)
			for (size_t j {}; j < w.length(); ++j)
				m[(x - j + m.size()) % m.size()][y] = w[j];
	}
}

void fill(Matrix& m) {
	static std::uniform_int_distribution chr(0, 25);

	for (auto& r : m)
		for (auto& c : r)
			if (c == '-')
				c = static_cast<char>(chr(rng)) + 'a';
}

void display(const Matrix& m) {
	for (const auto& r : m) {
		for (const auto& c : r)
			std::cout << c << ' ';

		std::cout << '\n';
	}
}

auto getWords(size_t& size) {
	constexpr size_t Expand { 5 };
	size_t numWords {};

	std::cout << "How many words do you want: ";
	std::cin >> numWords;

	std::vector<std::string> words(numWords);

	for (size_t i { }; auto & w : words) {
		std::cout << "Enter word " << ++i << ": ";
		std::cin >> w;

		if (w.size() > size)
			size = w.size() * Expand;
	}

	return words;
}

int main() {
	size_t size {};
	const auto words { getWords(size) };
	Matrix wordSearch(size, std::vector<char>(size, '-'));
	std::uniform_int_distribution dir(0, 3);

	for (const auto& w : words) {
		switch (dir(rng)) {
			case 0:
				forw(w, wordSearch);
				break;

			case 1:
				back(w, wordSearch);
				break;

			case 2:
				down(w, wordSearch);
				break;

			case 3:
				up(w, wordSearch);
				break;
		}
	}

	fill(wordSearch);
	display(wordSearch);
}


eg


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


This contains the names of 10 well-known cplusplus users...
Topic archived. No new replies allowed.