Thread: Passing values to arrays? Weird output?

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Question Passing values to arrays? Weird output?

    i made a simple porgram to create an array of 52 values, and then pass those values to another array.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int deck[51];
        for (int i=0; i<=51; i++)
        deck[i]=i;
        int n[51], s[51], e[51], w[51];
        for (int i=0, f=0; f<=51; f+=4)
        {
            n[f]=deck[i];i++;
            s[f]=deck[i];i++;
            e[f]=deck[i];i++;
            w[f]=deck[i];i++;
        }
        for (int i=0; i<=12; i++)
        cout << n[i]<<" ";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    now the output for this program should be "1 5 9... etc etc" but what i am getting is.

    0 2359672 2 0 4 -2141844640 6 -1410036744 8 -1410036744 10 2 12 65536 14 15 16 1 18 3 20 5 22 7 24 9 26 11 28 13 30 15 32 17 34 19 36 21 38 23 40 25 42 27 44 29
    46 31 48 33 50 35
    wtf?
    Last edited by Loic; 05-09-2007 at 11:40 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, make arrays with a size of 52 elements; then index them from 0-51.

    [edit]D'oh! That corresponds to the code you removed/replaced.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Dave_Sinkula View Post
    First, make arrays with a size of 52 elements; then index them from 0-51
    arnt i doing that here
    Code:
    int deck[51];
        for (int i=0; i<=51; i++)
        deck[i]=i;

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is an array with 51 elements, indexed 0-50.
    Code:
    int deck[51];
    [edit]The common idiom is this:
    Code:
    int array[52];
    for ( int i = 0; i < 52; ++i ) { /* ... */ }
    [/edit]

    Now, realize how you are hopping through the n, s, e, w arrays to initialize them. You're skipping 75&#37; of the initializations.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Dave_Sinkula View Post
    This is an array with 51 elements, indexed 0-50.
    Code:
    int deck[51];
    [edit]The common idiom is this:
    Code:
    int array[52];
    for ( int i = 0; i < 52; ++i ) { /* ... */ }
    [/edit]

    Now, realize how you are hopping through the n, s, e, w arrays to initialize them. You're skipping 75% of the initializations.
    that shouldnt matter... even if a change them like this.
    Code:
    int main(int argc, char *argv[])
    {
        int deck[51];
        for (int i=0; i<=51; i++)
        deck[i]=i;
        int n[12], s[12], e[12], w[12];
        for (int i=0, f=0; f<=51; f+=4)
        {
            n[f]=deck[i];i++;
            s[f]=deck[i];i++;
            e[f]=deck[i];i++;
            w[f]=deck[i];i++;
        }
        for (int i=0; i<=12; i++)
        cout << n[i]<<" ";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The output is still the same

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So if you try to stuff 52 things into a 51-element container, it shouldn't matter?

    And if you initialize only the 0th, 4th, 8th, etc. members of n[], and
    only the 1st, 5th, 9th, etc members of s[], and
    only the 2nd, 6th, 10th, etc members of e[], and
    only the 3rd, 7th, 11th, etc members of w[]

    ...one should expect the computer to know that you meant differently?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Dave_Sinkula View Post
    So if you try to stuff 52 things into a 51-element container, it shouldn't matter?

    And if you initialize only the 0th, 4th, 8th, etc. members of n[], and
    only the 1st, 5th, 9th, etc members of s[], and
    only the 2nd, 6th, 10th, etc members of e[], and
    only the 3rd, 7th, 11th, etc members of w[]

    ...one should expect the computer to know that you meant differently?
    well no i am trying to stuff 52 things into a 52 element container... n[0] - n[51]...

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Indexing starts at zero. If you're determining an arrays size, start counting at one. Tricky fence post errors.

    If I may butt in here:

    > for (int i=0; i<=51; i++)
    and then...

    > for (int i=0, f=0; f<=51; f+=4)
    > n[f]=deck[i];i++;

    Notice the addition to f in the for loop: as you use this value to index n, e, s, w. You would logically therefore only assign every fourth element, up to the eleventh, but you continue to overextend your array's bounds. The printout isn't very strange at all, just a symptom of undefined behavior.

    Whitespace is your friend. Listen to Dave's advice about loop control: it will help you get rid of your problem, and you need to rethink this anyway. Don't card decks have 52 cards?

  9. #9
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by citizen View Post
    Indexing starts at zero. If you're determining an arrays size, start counting at one. Tricky fence post errors.

    If I may butt in here:

    > for (int i=0; i<=51; i++)
    and then...

    > for (int i=0, f=0; f<=51; f+=4)
    > n[f]=deck[i];i++;

    Notice the addition to f in the for loop: as you use this value to index n, e, s, w. You would logically therefore only assign every fourth element, up to the eleventh, but you continue to overextend your array's bounds. The printout isn't very strange at all, just a symptom of undefined behavior.

    Whitespace is your friend. Listen to Dave's advice about loop control: it will help you get rid of your problem, and you need to rethink this anyway. Don't card decks have 52 cards?
    i got it working... thanks for the help.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int deck[51];
        for (int i=0; i<=51; i++)
            deck[i]=i;
        random_shuffle(deck, deck+51);
        
        int n[12], s[12], e[12], w[12];
        for (int i=0,h=0; h<=13;h++)
        {
            n[h]=deck[i];i++;
            s[h]=deck[i];i++;
            e[h]=deck[i];i++;
            w[h]=deck[i];i++;
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    and yes a deck of cards dose have 52 cards, but the first card is stored in deck[0]... not deck[1]

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Loic View Post
    and yes a deck of cards dose have 52 cards, but the first card is stored in deck[0]... not deck[1]
    Reread a tutorial on arrays.

    Your declaration:

    Code:
    int deck[51];
    That means you have 51 elements in the array, from index 0 to index 50.

  11. #11
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by MacGyver View Post
    Reread a tutorial on arrays.

    Your declaration:

    Code:
    int deck[51];
    That means you have 51 elements in the array, from index 0 to index 50.
    ah righto, sorry my bad.. lol

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Now work on the dealing of hands and how to handle these arrays correctly.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Enough already... I can't take it any more:
    Code:
    int deck[52];
    for (int i=0; i<52; i++)
        deck[i]=i;
    random_shuffle(deck, deck+52);
        
    int n[13], s[13], e[13], w[13];  // 52 / 4 = 13 (not 12)
    for (int i=0,h=0; h<13;h++)
    {
        n[h]=deck[i];i++;
        s[h]=deck[i];i++;
        e[h]=deck[i];i++;
        w[h]=deck[i];i++;
    }
    Also, without a call to srand I'd guess your random_shuffle call will be giving players the same hand (which may be desirable - in a way) each time the program is run.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> without a call to srand I'd guess your random_shuffle call will be giving players the same hand
    Depends on the implementation. At least one popular implementation isn't affected by srand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing 2D dynamic arrays
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 11-12-2008, 08:08 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  4. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  5. Weird Output...(Link list)
    By davidvoyage200 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2003, 09:07 PM