Thread: Two Dimentional array problems

  1. #1
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14

    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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest looking up strcpy string.h function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by VASHtheCHIBI View Post
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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()).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14
    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. #9
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14

    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. #10
    Registered User VASHtheCHIBI's Avatar
    Join Date
    Jan 2012
    Location
    Hangzhou, China
    Posts
    14

    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
    Attached Files Attached Files

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quzah View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    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.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quzah View Post
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to two dimentional array
    By lio in forum C Programming
    Replies: 2
    Last Post: 11-06-2010, 10:55 AM
  2. 3 dimentional array in C
    By jahanpanah in forum C Programming
    Replies: 3
    Last Post: 10-24-2010, 11:16 AM
  3. 2 dimentional array
    By theone1 in forum C++ Programming
    Replies: 10
    Last Post: 11-11-2007, 01:30 PM
  4. Two dimentional array
    By h3ro in forum C++ Programming
    Replies: 12
    Last Post: 04-17-2007, 01:01 AM
  5. 2 dimentional array of pointers
    By Cerber4s in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2003, 06:56 PM