# Thread: Two Dimentional array problems

1. ## Two Dimentional array problems

Hi everyone, I looked at the FAQ a couple of times and could not find anything on this, and I figured someone on here could help.

I am making a hangman game in C (just for learning purposes) and would like to create a database of words for the computer to select randomly in order to allow the user to play multiple times. I figured the best way to do this was to make a two dimensional array with one dimension being the word itself, and the other being the string of letters that make up the word. I would like to know if there is an easy way to assign the string to the first dimention without having to create a new line for each letter.

For example:I don't want to do this
Code:
```char pass1[10][6];

pass1 [0][0]='a';
pass1 [0][1]='l';
pass1 [0][2]='f';
pass1 [1][0]='t';
pass1 [1][1]='r';
...```
I want to do something more like:
Code:
```char pass1 [10][6];

pass1 [0]="alf";
pass1 [1]="tree";```

What is the correct syntax for this?

2. I suggest looking up strcpy string.h function.

Tim S.

3. Originally Posted by VASHtheCHIBI
What is the correct syntax for this?
It depends on when you want to make the assignment:
Code:
```char pass1 [10][6] =
{
"alf",
"tree",
};```

Quzah.

4. That is not an assignment, quzah. It is an initialisation (or initalization, for those influenced by America). There is no equivalent assignment (hence the need to use strcpy()).

5. Welcome to the forum, Vas.

I suggest having the words in a text file, one word per line. Then, in your program, you can open the word file, and read them all into an array. From there you can randomly select each word for the player to discover.

Code:
```char words[100][48];
int i;
FILE *fp;
//open the words.txt file

//then

for(i = 0;i<100;i++) {
fscanf(fp, "%s", words[i]);
}```
That sort of thing. I can't show much more, since I put a drill bit into my finger, and keying with difficulty.

6. Originally Posted by grumpy
That is not an assignment, quzah. It is an initialisation (or initalization, for those influenced by America). There is no equivalent assignment (hence the need to use strcpy()).
Of course it's an assignment. That's what the = operator is. Read his first post again, and you'll see that it is exactly what he wants.

Quzah.

7. Originally Posted by quzah
Of course it's an assignment. That's what the = operator is. Read his first post again, and you'll see that it is exactly what he wants.

Quzah.
It may be using the assignment operator, but what you've written is specifically termed "initialisation" and not "assignment". There needs to be that discinction even though they use the same operator, because initialisation is used to refer to that which cannot be done except where it assigns to the variable at the point which it is declared.
I.e. the fact that you can't do:
Code:
```int pass1;
pass1[10][6] = {
"alf",
"tree",};```
is specifically what makes the original code you posted initialisation and not assignment.

On the other hand, it does look like the original poster would be happy with initialisation instead of assignment.

8. Thank you Quzah, Your suggestion works perfectly, even with the vocabulary taboo. (Honestly, I never noticed it, but I'm new to this. I'm happy that when I press a button, the character I want shows up on the screen.)

Thank you for the vocabulary lesson iMalc, I'll be careful about that in the future.

Thanks for the suggestion Adak. I would like to play around with files and things like that in the near future, however, I wanted this exorcise to be simple, and easy to share with other people. Having additional files requires me to do a little more research about how to ensure the game is pulling the correct file, even if it gets transferred to another computer. (This is research I want to do myself first though. I learn better trying it on my own and then asking for help if I need it.)

I want to make a few additions to my code to make the screen look nicer while the game is running, but other than that, I'm quite happy. For such a simple program, it took me a long time to write, but I guess that I will get faster the more I practice.

If you all would like, I can post the code when I'm finished, and I would really appreciate a critique and any advice to clean it up and make it run better. Plus, who doesn't like the nostalgia of playing a few rounds of hangman? (no pictures though, again, that's more advanced than I can do right now.)

-VASH

9. ## Finished!

Here is my finished game! Since this is my first full fledged program, I was wondering if I could get some input from everyone. Attached is the source code. Could you take a look at it, and give me a few pointers?

Thanks a ton!

-VASH

main.c

10. ## Correction

Please ignore the original attachment (7.3 KB), This is a modified code.

I added a few comments to clarify things and make the code more understandable. This should help with your evaluation.

Thanks again,

-VASH

main.c

11. Originally Posted by iMalc
It may be using the assignment operator, but what you've written is specifically termed "initialisation" and not "assignment".
Please define initialization for me. What does "initialize a variable" mean? Once you define the word initialization, you'll find that I am in fact correct when I say it's an assignment.

Quzah.

12. Originally Posted by quzah
Please define initialization for me. What does "initialize a variable" mean? Once you define the word initialization, you'll find that I am in fact correct when I say it's an assignment.

Quzah.
Please post working code that initializes the same variable twice; then it will definitely be assignment.

Tim S.

13. Originally Posted by stahta01
Please post working code that initializes the same variable twice; then it will definitely be assignment.

Tim S.
Code:
```void initialize( void )
{
struct foo
{
int x;
} bar = { 10 };
}

int main( void )
{
for( int x = 0; x < 2; x++ )
initialize( );
return 0;
}```
There are very few people as good at picking nits as I am.
verb (used with object), -ized, -iz·ing. Computers.
1. to set (variables, counters, switches, etc.) to their starting values at the beginning of a program or subprogram.
Main Entry: set
Part of Speech: verb
Definition: decide upon
Synonyms: agree upon, allocate, allot, appoint, arrange, assign, conclude, decree, designate, determine, dictate, direct, establish, estimate, fix, fix price, impose, instruct, lay down, make, name, ordain, prescribe, price, rate, regulate, resolve, schedule, settle, specify, stipulate, value
You lose.

Quzah.

14. Originally Posted by quzah
Code:
```void initialize( void )
{
struct foo
{
int x;
} bar = { 10 };
}

int main( void )
{
for( int x = 0; x < 2; x++ )
initialize( );
return 0;
}```
There are very few people as good at picking nits as I am.
You lose.

Quzah.
I said the SAME variable twice!

Tim S.

15. Originally Posted by stahta01
I said the SAME variable twice!

Tim S.
Honestly it doesn't matter what you said. I didn't say an initialization was identical to every assignment. I said an initialization IS AN assignment. Prove to me that an initialization is not an assignment, or shut up.

Quzah.

Popular pages Recent additions