My 2d array is not out putting all the words

Pages: 12
I have this code which ask the user for how many words and makes a 2d array but the words the user input do not always print all of them.

#include <iostream>
#include<iostream>
#include<cstdlib>
#include <stdio.h>
#include <algorithm>
int main () {
srand(time(0));
int size = 0;
int siz = 6;
int numberwords;
std::cout << "How many words do you want on the search puzzle:" << std::endl;
std::cin >> numberwords;
std::string eliasArray[numberwords];
for (int i = 0; i < numberwords; i++) {
std::cout << "Word n*" << i + 1 << std::endl;
std::cin >> eliasArray[i];
int size2 = eliasArray[i].length();
if (size2 >= size) {
size = size2;
int randnum = rand() % 4;
int index = rand() % numberwords;

if (randnum == 1 || randnum == 2 || randnum == 3) {
reverse(eliasArray[index].begin(), eliasArray[index].end());
eliasArray[index];

} else {

eliasArray[index];
}
}
}

srand(time(0));
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int arrayLenght = sizeof(eliasArray) / sizeof(eliasArray[0]);
const int number = 10;
char x[number][number] = {};
int number1[arrayLenght];
int e = 0;
//Looks that all th words have been input in the word search
while (e < arrayLenght) {
int i;
int x = rand() % number;
for (i = 0; i < arrayLenght; i++) {
if (number1[i] == x) {
break;
}
}

if (i == arrayLenght) {
number1[e] = x;
e = e + 1;

}
}
//Helps output all the words Problem
for (int v = (arrayLenght - 1); v >= 0; v--) {
int x = number1[v - 1];

if (number1[v] < number1[v - 1]) {
number1[v - 1] = number1[v];
number1[v] = x;
}
}


int h = 0;
//initialize the others
for (int y = 0; y < number; y++) {
int tracker = 0;


for (int i = 0; i < number; i++) {
if (y == number1[h]) {
if (i < eliasArray[h].length()) {
x[y][i] = eliasArray[h][i];
std::cout << x[y][i] << " ";
tracker++;

//outputs the words the user wants LINEAR down
} else {
x[y][i] = letters[rand() % 26];;
std::cout << x[y][i] << " ";
}


} else {
x[y][i] = letters[rand() % 26];;
std::cout << x[y][i] << " ";
}
}
//Inputs table
if (h < arrayLenght) {
if (tracker > 0) {
h = (h + 1);

}

}

std::cout << std::endl;


}


}
Last edited on
Let's start off with the minor:

Use code tags so we can read your code:



[code]

//Put your code here!

[/code]




Second, your code is.. awful. Pretty bad. There are very bad coding practices used and the code itself is an eyesore. It's poorly spaced and just a giant blob. The hanging bracket at the top is already ugly, but added to the rest makes this code unbearable to read.



Third, you are using C functions, not C++. You DONT want to do that. C++ has alternatives to what people use in C, and they're better. If you #include <something.h> (the give away is the .h within <>), it's a C library, try to avoid.

Also, you're using C style arrays. This:

1
2
3
4
int a = 0;
cin >> a;

int arr[a];


Is NOT valid C++. Some compilers will allow this, but this is not standard C++. You should use Vectors.

Fourth, your logic is incorrect. You frequently go beyond the bounds of your arrays. Like this piece of code will:

1
2
3
4
5
6
7
//Helps output all the words Problem
for (int v = (arrayLenght - 1); v >= 0; v--) {
int x = number1[v - 1];
....

// if v is 0, and you access number1[v - 1] ...
}


You may not know you have - another benefit of using vectors. A vector will throw an error that clearly says you went beyond the array bounds of the vector.

With your own array, your program can "silently" crash. The program will just end and you won't know why.


To get a random number, use the <random> library, it's better in every way.
Hi, sorry of my mess. I actually have not seen 2d vectors in my class but can I initialize them like this?
and how can I do a table with 2d vectors?


#include <iostream>
#include <vector>
#include <random>
int main () {
//Number maximum of column
int column = 100;
//Number maximun of rows
int row =100;
//Initialize 2d vector
std::vector<std::string>str(column, 0);
std::vector<std::vector<std::string>>st(row, str);
Last edited on
A 2D vector would look like this:

 
std::vector<std::vector<std::string>> str;


str(num) -> Creates num amount of elements.
str(num, " ") -> Creates and sets those elements to " ".


Again, use code tags so we can easily read your code. You can edit your posts to include them
@Bubbles0012,
State the ORIGINAL problem, not your paraphrasing of it in either words or code.

"Wordsearch" as a puzzle is usually a 2-d array of chars, or a 1-d array of strings. So you would need vector<string>, not vector<vector<string>>.

Clarify your problem if you want to receive answers.
The OP's code, formatted.
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
#include <iostream>
#include<iostream>
#include<cstdlib>
#include <stdio.h>
#include <algorithm>
int main()
{
  srand(time(0));
  int size = 0;
  int siz = 6;
  int numberwords;
  std::cout << "How many words do you want on the search puzzle:" << std::endl;
  std::cin >> numberwords;
  std::string eliasArray[numberwords];

  for (int i = 0; i < numberwords; i++) {
    std::cout << "Word n*" << i + 1 << std::endl;
    std::cin >> eliasArray[i];
    int size2 = eliasArray[i].length();
    if (size2 >= size) {
      size = size2;
      int randnum = rand() % 4;
      int index = rand() % numberwords;

      if (randnum == 1 || randnum == 2 || randnum == 3) {
        reverse(eliasArray[index].begin(), eliasArray[index].end());
        eliasArray[index];
      } else {
        eliasArray[index];
      }
    }
  }

  srand(time(0));
  char letters[] = "abcdefghijklmnopqrstuvwxyz";
  int arrayLenght = sizeof(eliasArray) / sizeof(eliasArray[0]);
  const int number = 10;
  char x[number][number] = { };
  int number1[arrayLenght];
  int e = 0;

  //Looks that all th words have been input in the word search
  while (e < arrayLenght) {
    int i;
    int x = rand() % number;
    for (i = 0; i < arrayLenght; i++) {
      if (number1[i] == x) {
        break;
      }
    }
    if (i == arrayLenght) {
      number1[e] = x;
      e = e + 1;
    }
  }
  
  //Helps output all the words Problem
  for (int v = (arrayLenght - 1); v >= 0; v--) {
    int x = number1[v - 1];

    if (number1[v] < number1[v - 1]) {
      number1[v - 1] = number1[v];
      number1[v] = x;
    }
  }

  int h = 0;
  //initialize the others
  for (int y = 0; y < number; y++) {
    int tracker = 0;
    for (int i = 0; i < number; i++) {
      if (y == number1[h]) {
        if (i < eliasArray[h].length()) {
          x[y][i] = eliasArray[h][i];
          std::cout << x[y][i] << " ";
          tracker++;
          //outputs the words the user wants LINEAR down
        } else {
          x[y][i] = letters[rand() % 26];;
          std::cout << x[y][i] << " ";
        }
      } else {
        x[y][i] = letters[rand() % 26];;
        std::cout << x[y][i] << " ";
      }
    }

    //Inputs table
    if (h < arrayLenght) {
      if (tracker > 0) {
        h = (h + 1);
      }
    }
    std::cout << std::endl;
  }
}


I made this but now it only outputs the words I give to the problem (after the number, I still have not put to output "words n+1" but that should work fine) and not extra letters to fill the blank and the second problem is that is just inputting on rows and not on columns too.


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

int main()

{
    int r, c, n;
    std::cout << "Enter number of words: ";
    std::cin>>n;
   std::vector<std::string> words(n);
    int max_len = 0;
    for (int i=0; i<n; i++)
    {
        std::cin >> words[i];
        if (words[i].length() > max_len)
            max_len = words[i].length();
    }

    char table[max_len+1][n];
    for (int i=0; i<n; i++)
    {
        int len = words[i].length();
        for (int j=0; j<len; j++)
            table[j][i] = words[i][j];
        for (int j=len; j<max_len; j++)
            table[j][i] = '- ';
    }
    for (int i=0; i<max_len; i++)
    {
        for (int j=0; j<n; j++)
            std::cout << table[i][j] << " ";
        std::cout<<std::endl;
    }
    char extra;
    std::cin >> extra;
    for (int i=0; i<n; i++)
    {
        int len = words[i].length();
        if (table[len][i] == ' -')
            table[len][i] = extra;
        else
        {
            std::cout << "Extra character can't be inserted.";
            return 0;
        }
    }
    for (int i=0; i<max_len; i++)
    {
        for (int j=0; j<n; j++)
            std::cout << table[i][j] << "  ";
        std::cout << std::endl;
    }
    return 0;
}
Last edited on
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
... and you still haven't clarified what the program is supposed to do. What does "make 2d array" actually mean...

Why a 2D vector for storing words input by the user? So far your interpretation of the assignment is not very understandable.

A C string is a char array. char cstr[] = "word";

For C++ a string is not an array, it is a single entity. Gotta include the <string> header:
1
2
3
4
5
#include <string>

/// 

std::string str { "word" };

To store a bunch of C strings would require an array of C strings. A 2D char array.

To store a bunch of C++ strings using a std::vector is a good choice.

We really do want to help you, but you need to help us as well. Post the exact assignment, not what you think it means.

Use code tags.

How to ask for C++ coding help
http://www.gregcons.com/KateBlog/HowToAskForCCodingHelp.aspx

When posting code it is really helpful if it is a Short, Self Contained, Correct (Compilable), Example
http://www.sscce.org/
What I need to do is a program that ask user the how many words they want and what words they want in order to make a word search then I need to print the word search made with the words the user input but at the same time random letters (the size of the word search does not matter). The wordsearch should output the words the user input in random order can be vertical, horizontal and diagonal and some words need to be reversed.

The problem of the code is that only prints on columns (I had another and only print on rows).


Last edited on
@Bubbles0012,

Let's try again, shall we:
State the ORIGINAL problem, NOT YOUR PARAPHRASING of it in either words or code.

Problem:
If the user chooses to create a new puzzle, your program should ask the user how many words they would like in the program. Once the user enters a number, your program should continue to ask the users to type in the words they would like to add to the puzzle. Once the user has finished, the program should generate a word search puzzle and also list all the possible words. It should then ask the user if they would like to save the puzzle. Your program should randomly place words in the grid diagonally, horizontal, and vertically. Your grid should be created using a 2D-Array. Let the words in the puzzle determine how large of an array you need. The words should also be placed both forward and backwards. The program should then return to the main menu. Your puzzle should look something like this:



w e a p p l e t y u

h r t i g i d i o e

e e j g r g r h r h

a r u n t y u t u o

t e t h j k a s w r
Last edited on
This is the code I have but the words only appears on rows and not on columns.



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

int main()

{
    int r, c, n;
    std::cout << "Enter number of words: ";
    std::cin>>n;
    std::vector<std::string> words(n);
    int max_len = 10;
    for (int i=0; i<n; i++)
    {
        std::cin >> words[i];
        if (words[i].length() > max_len)
            max_len = words[i].length();
    }

    char table[max_len+1][n];
    for (int i=0; i<n; i++)
    {
        int len = words[i].length();
        for (int j=0; j<len; j++)
            table[j][i] = words[i][j];
        for (int j=len; j<max_len; j++)
            table[j][i] = '- ';
    }
    for (int i=0; i<max_len; i++)
    {
        for (int j=0; j<n; j++)
            std::cout << table[i][j] << " ";
        std::cout<<std::endl;
    }
    char extra;
    std::cin >> extra;
    for (int i=0; i<n; i++)
    {
        int len = words[i].length();
        if (table[len][i] == ' -')
            table[len][i] = extra;
        else
        {
            std::cout << "Extra character can't be inserted.";
            return 0;
        }
    }
    for (int i=0; i<max_len; i++)
    {
        for (int j=0; j<n; j++)
            std::cout << table[i][j] << "  ";
        std::cout << std::endl;
    }
    return 0;
}
M'ok, if you are going to use std::string, and you are, you should have #include <string> . To not include it can lead to problems.

You've shown us what you expect the output is supposed to be. Show us what output you do get.

Hint, use output tags for output, they keep the formatting intact.
https://cplusplus.com/articles/z13hAqkS/

How to use the output tags is the 2nd item.
I only get the words in the column and not in the rows.



Enter number of words: 4
word
string
vector
array
w s v a 
o t e r 
r r c r 
d i t a 
  n o y 
  g r   
So you want the input as something like:
Enter number of words: 4
word
string
vector
array

w o r d
s t r i n g
v e c t o r
a r r a y

maybe?

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

int main()
{
   std::vector<std::string> str_vec;

   std::cout << "How many words to enter? ";
   size_t num_words { };  // can't have a negative number
   std::cin >> num_words;
   std::cout << '\n';

   for ( unsigned num { }; num < num_words; ++num )
   {
      std::string input;
      std::cout << "Word #" << num + 1 << ": ";
      std::cin >> input;
      str_vec.push_back(input);
   }
   std::cout << '\n';

   for ( const auto& vec_itr : str_vec )
   {
      for (const auto& str_itr : vec_itr)
      {
      std::cout << str_itr << ' ';
      }
      std::cout << '\n';
   }
}
How many words to enter? 4

Word #1: iostream
Word #2: vector
Word #3: string
Word #4: array

i o s t r e a m
v e c t o r
s t r i n g
a r r a y
The input should look something like this, including the words the user wants to input and the place in the grid should be random (vertical, horizontal and diagonal.

z u p w q x v l v b 
w z x v b f k s x a 
c v y e m u o t k y 
b d c w a b n h z n 
t p d e y o t v i n 
j p c y m q f m l e 
c y i a z h s g a g 
x x x u x n d i h q 
z e i f i a e d u v
 
Last edited on
Post your entire assignment verbatim.

Don't do it bit by bit as you have been doing, don't post your interpretation of what you think the assignment is supposed to be.




This is the assignment:
Create a puzzle
If the user chooses to create a new puzzle, your program should ask the user how many words they would like in the program. Once the user enters a number, your program should continue to ask the users to type in the words they would like to add to the puzzle. Once the user has finished, the program should generate a word search puzzle and also list all the possible words. It should then ask the user if they would like to save the puzzle. Your program should randomly place words in the grid diagonally, horizontal, and vertically. Your grid should be created using a 2D-Array. Let the words in the puzzle determine how large of an array you need. The words should also be placed both forward and backwards. The program should then return to the main menu. Your puzzle should look something like this:



w e a p p l e t y u

h r t i g i d i o e 

e e j g r g r h r h 

a r u n t y u t u o

t e t h j k a s w r



apple 

pig

runt

wheat
Pages: 12