Weird output loop+array.

This is a discussion on Weird output loop+array. within the C Programming forums, part of the General Programming Boards category; I've got the following function: Code: void deck() { int cards[14][4]; int a; int b; for (b = 0; b ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Weird output loop+array.

    I've got the following function:
    Code:
    void deck()
    {
    	int cards[14][4];
    	int a;
    	int b;
    	for (b = 0; b < 4;b++)
    	{
    		for (a = 1; a < 15; a++)
    		{
    			cards[a][b] = a;
    			printf("%d\n",cards[a][b]);
    		}
    	}
    return;
    }
    The output is:
    Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    -304991592
    I notice it sort of crashes in the 3rd time it does the b loop. At least it puts a silly value in cards[14][3]
    and then stops doing the loop.

    Could anybody help?

    Thank You.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,384
    Recall that array indices start from 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    Code:
    int cards[14][4];
    Valid indicies for the first dimension are from 0 through 13 (inclusive). You are going from 1 to 14.
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with output from array
    By jaw24 in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2007, 09:16 PM
  2. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Ex. 2-4 KnR (2nd ed.) loop && array handling
    By olbas in forum C Programming
    Replies: 2
    Last Post: 03-23-2004, 10:07 PM
  5. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21