two dimensional array

This is a discussion on two dimensional array within the C Programming forums, part of the General Programming Boards category; my project is : Create a two dimensional array. The array will have 4 rows and 13 columns. - Each ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    two dimensional array

    my project is :
    Create a two dimensional array. The array will have 4 rows and 13 columns.
    - Each row will be a different suit of cards.
    - The columns will store integers that will represent the values of the card (e.g. 2, 3, 4...10, J, Q, K)
    - Populate each row of the array appropriately and print out the value of the array so the output would look like this:

    AD
    2D
    2D
    4D
    5D
    ...
    10D
    JD
    QD
    KD

    AH
    2H

    i have coded
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define VALUES 14
    #define ACE 1
    #define KING 13
    #define QUEEN 12
    #define JACK 11
    enum suits{ clubs , diamonds , hearts , spades ,numOfSuits };
    int main ( void )
    
    {	
    	int arry [numOfSuits] [ VALUES ]= { { 4 } , { 0,2,3,4,5,6,7,8,9,10,11,12,13 } };
    	for( int x = clubs; x < numOfSuits; x++ ) {
    			for ( int y = 0;y<VALUES;y++){
    				arry[x][y]=x*13+(y);
    			}
    	}
    	for( int x = clubs; x < numOfSuits; x++ ) {
    			for ( int y = 0;y<VALUES;y++){
    				switch ( arry [x][y]%13)
    				{
    					case ACE:		printf("A"); break;
    					case QUEEN:		printf("Q"); break;
    					case JACK:		printf("J"); break;
    					case KING:		printf("K"); break;
    					default:		printf("%d",arry[x][y]%13);
    				}
    				switch ( arry[x][y]/13 )
    				{
    					case clubs:		printf(" clubs\n"); break;
    					case diamonds:	printf(" diamonds\n"); break;
    					case hearts:	printf(" hearts\n"); break;
    					case spades:	printf(" spades\n"); break;
    
    				}
    			}
    	}
    		
    	return 0;
    }
    and it skips K output, what am i doing wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you divide a number by 13, how do you propose to get a remainder of 13?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    ya i know i mssed the array up with 14, but i changed it and still no luck

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    switch ( arry[x][y]/13 ) takes [x][y], assume its 26, divide it by 13 to get 2, which would be hearts

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    the output of the current program is :
    A clubs
    2 clubs
    3 clubs
    4 clubs
    5 clubs
    6 clubs
    7 clubs
    8 clubs
    9 clubs
    10 clubs
    J clubs
    Q clubs
    0 diamonds
    A diamonds
    2 diamonds
    3 diamonds
    4 diamonds
    5 diamonds
    6 diamonds
    7 diamonds
    8 diamonds
    9 diamonds
    10 diamonds
    J diamonds
    Q diamonds
    0 hearts
    A hearts
    2 hearts
    3 hearts
    4 hearts
    5 hearts
    6 hearts
    7 hearts
    8 hearts
    9 hearts
    10 hearts
    J hearts
    Q hearts
    0 spades
    A spades
    2 spades
    3 spades
    4 spades
    5 spades
    6 spades
    7 spades
    8 spades
    9 spades
    10 spades
    J spades
    Q spades
    0~

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ask yourself again: is the remainder of 26 divided by 13 equal to 13? No? Then your check for King won't work.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    oh duhhh!!! any hint on how to correct it?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Check for the answer that you will get.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    i still can't figure out how to fix this. i'm new to C , taking intro to programming, so i'm VERY green to it

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by leisiminger View Post
    i still can't figure out how to fix this. i'm new to C , taking intro to programming, so i'm VERY green to it
    I'm assuming you're more familiar with division. What do you get as the remainder when you divide 26 by 13? Use that as your KING.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    i'm not sure i'm taking you correctly, but should i make a new switch statement exclusively for king? i very do appreciate your help

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by leisiminger View Post
    i'm not sure i'm taking you correctly, but should i make a new switch statement exclusively for king? i very do appreciate your help
    You already have an case for KING. However, the value of KING is not 13. You should define KING to be the right thing (remember the defines? at the top?) -- remember that the right thing comes from a remainder of a division.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    This doesn't do anything like what you think it does:
    Code:
    	int arry [numOfSuits] [ VALUES ]= { { 4 } , { 0,2,3,4,5,6,7,8,9,10,11,12,13 } };
    This is a two-dimensional array, with 4x14 = 56 elements and you are only initialising 13 of them, and incorrectly at that. Not to mention it should be 4x13 NOT 4x14! Arrays start at zero!
    Decrease the value of every single one of your defines by 1.
    Last edited by iMalc; 03-09-2008 at 11:55 PM.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 10:58 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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