Tech Support Forum banner
Status
Not open for further replies.
1 - 13 of 13 Posts

·
Registered
Joined
·
18 Posts
Discussion Starter · #1 ·
does anyone know a quicker way of storing words in an array? I have to put 58,000 words in an array and it may take a while writing words[0] = "aardvark" 58,000 times. Help anyone?
 

·
TSF Team, Emeritus
Joined
·
2,367 Posts
Moved from Web Design and Programming to assist you better.

Two questions before we get started:

1. What language are we working with here?
2. What source are you using for the words?
 

·
Registered
Joined
·
2,276 Posts
What's the purpose of storing all of them in an array? that must take a fair bit of memory to do and would surely make the program size a fair bit larger than it needs to be?...

Tell us why you need to access them readily and we can try work out a decent solution using C++ and the source file rather than making you have to copy+paste all of them =]

Cheers,
Jamey
 

·
Registered
Joined
·
18 Posts
Discussion Starter · #5 ·
the purpose of the array is to get a pointer to point to a random object in the array, then, using a loop, get rand() to increment the pointer a random amount of times, so that it points to a random word. Then i would print the string that the pointer is pointing to. But i dont know if there is another way to do the same thing?
 

·
Microsoft MVP
Joined
·
3,339 Posts
Is this a list of words, one word per line?
If so, you can use Notepad++ to easily edit the file to add the words[X] =" part, and a trailing quote, so you can paste it in.
Open file in Notepad++
Press CTRL+Home
Press CTRL+H
type \r\n in the Find what: box
type "\r\n in the Replace with: box
Select Extended under Search Mode:
Click Replace All
Click Close
Press CTRL+Home
Press ALT+C
Select the Text to insert option
Enter ] = " in the box
Click OK
Press CTRL+Home
Press ALT+C
Select the Number to insert option
Type 0 in the Initial Number: box
Type 1 in the Increase by: box
Uncheck Leading zero
Select Dec or Hex under Format as required
Click OK
Press CTRL+Home
Press ALT+C
Select the Text to insert option
Enter words[ in the box
Click OK
Press CTRL+End
Clean up any extra lines if present.
Click File | Save (or Save As if you want to keep the original file) and save the file
Press CTRL+A
Press CTRL+C
Now paste into your C++ file
 

·
Emeritus
Joined
·
1,131 Posts
If it's one word per line, it's much better to read the file in programmatically. Even if it's not one word per line, it's best not to hardcode input data. Do you know how to read in a file with C++? Do you have a book you're reading?
 

·
TSF Team, Emeritus
Joined
·
2,367 Posts
You'd need a dynamic array or an arraylist and the fileio header.

Essentially, it looks something like this:

declare int32 no_objects
declare int32 no_chosen
declare arraylist Words
open file
read file input into Words [every /n is a new element]
count elements in Words //there's a statement that will do this, but I'll leave that for you to find
Assign no_words to the result
Choose a random integer with floor of one and ceiling of [[no_words]] | assign result to no_chose
Output arraylist value that corresponds to no_chose

Modify the above pseudocode to suit your particular assignment.
 

·
Microsoft MVP
Joined
·
3,339 Posts
It's in the previous post, you replace \r\n (which is a CR\LF) with "\r\n ("CR\LF)
Open file in Notepad++
Press CTRL+Home
Press CTRL+H
type \r\n in the Find what: box
type "\r\n in the Replace with: box
Select Extended under Search Mode:
Click Replace All
Click Close
You can trim trailing spaces from the Edit menu. Several options in the TextFX Edit menu, trim trailing spaces, trim blank lines. I've found Notetab Light to be better at that though, and I prefer it for Regular Expressions.

But as others have said, you can just read in the file rather than coding it as part of the program. That makes it much easier to add to as well, or even switch to a different language.
 

·
Registered
Joined
·
18 Posts
Discussion Starter · #12 ·
ok i have another issue. whne i try to compile the array, for every line it says that theres a problem with conversion type with the = sign. more help?
 
1 - 13 of 13 Posts
Status
Not open for further replies.
Top