question about multidimensional arrays

This is a discussion on question about multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Hi, I am not sure about something. If you are using a multidimensional array like: scores[5][10], is it true that ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    question about multidimensional arrays

    Hi,
    I am not sure about something. If you are using a multidimensional array like: scores[5][10], is it true that you can only enter data into the second size because the first size acts like a counter? Say: scores[] is an array of 5 players which is an array of 10 scores. So the 5 has to be the number of groups of 10 scores? So I could not enter data into the 5 part? Only the 10 could have data entered into it? This is the code Im trying:
    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    	int seats[10][20],
    		
    		enter_class,
    		first_class, /*first class subscript*/
    		business_class; /*business class subscript*/
    
    	printf("\nPlease enter you seating class(1 for first class, 2 for business): ");
    	scanf("%d" , &enter_class);
    
    	
    	if(enter_class == 1)
    	{	
    		for(first_class=0; first_class<10; ++first_class)
    		{
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
    				break;
    		}
    	
    	}
    
    	else
    	
    	{
    		for(business_class=0; business_class<20; ++business_class)
    	
    		{	
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
                                    break;
    		}	
    	
    	}
    		for(first_class=0; first_class<10; ++first_class)
    				/*contents of array would be printed here*/
    }
    A 1 is entered into the first class part of the array each time first class is selected, and a 2 is entered each time for business class.
    After the program finishes it would display the contents of the two parts of the array, showing how many times each class were selected.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An array of size N may be indexed from 0 to N-1.

    Initializing data will be helpful.
    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 User
    Join Date
    Jan 2006
    Posts
    100
    I dont get it.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    What is business_class here?
    Code:
    for(first_class=0; first_class<10; ++first_class)
    		{
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
    				break;
    		}
    Or first_class here?
    Code:
    for ( business_class = 0; business_class < 20; ++business_class )
          {
             if ( seats[first_class][business_class] == 0 )
             {
                seats[first_class][business_class] = enter_class;
             }
             break;
          }
    And how do they relate to int seats[10][20]?
    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 User
    Join Date
    Jan 2006
    Posts
    100
    Are you saying I forgot to initialize them to 0?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Well, to something.
    Code:
    	int seats[10][20],
    		
    		enter_class,
    		first_class, /*first class subscript*/
    		business_class; /*business class subscript*/
    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 User
    Join Date
    Jan 2006
    Posts
    100
    Aren't they initialized to 0 in each for loop?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    No. Look closely. One half is, the other half ain't. And for each it's the wrong half.

    [edit]...And how do 2 arrays make you choose an array of 10?
    Last edited by Dave_Sinkula; 02-25-2006 at 11:08 PM.
    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.*

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    in the first for loop, does the second size not belong, and the second for loop the first not belong?
    Code:
    for(first_class=0; first_class<10; ++first_class)
    		{            /*wrong?*/
            if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
    				break;
    		}
    	
    	}
    
    	else
    	
    	{
    		for(business_class=0; business_class<20; ++business_class)
    	
    		{	           /*wrong?*/
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
                                    break;
    		}
    my original question was leaning towards this. Can the first size[10] not accept data because its the number of times of size 2 [20]?
    Last edited by richdb; 02-25-2006 at 11:24 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If neither first_class nor business_class are defined, what are they here?
    Quote Originally Posted by Dave_Sinkula
    What is business_class here?
    Code:
    for(first_class=0; first_class<10; ++first_class)
    		{
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
    				break;
    		}
    Or first_class here?
    Code:
    for ( business_class = 0; business_class < 20; ++business_class )
          {
             if ( seats[first_class][business_class] == 0 )
             {
                seats[first_class][business_class] = enter_class;
             }
             break;
          }
    And how do they relate to int seats[10][20]?
    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.*

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    they are defined as integers as subscripts for the two sizes of the array. lol Im too tired

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The values are not defined. They are declared as ints.

    And their values are random. Let me post a couple of random values: 100002345 and 100002346.

    And I'm tired too.
    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
    Join Date
    Jan 2006
    Posts
    100
    Dont know what you mean by defined. #define first_class 10
    #define business_class 20 ???

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by richdb
    Dont know what you mean by defined.
    Has no known value.
    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.*

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    So business_class should have the value of 20 here?
    Code:
    for(first_class=0; first_class<10; ++first_class)
    		{
    			if(seats[first_class][business_class] == 0)
    				seats[first_class][business_class] = enter_class;
    				break;
    		}

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  2. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  3. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  4. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM
  5. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM

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