# question about multidimensional arrays

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-25-2006
richdb
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.
• 02-25-2006
Dave_Sinkula
An array of size N may be indexed from 0 to N-1.

Initializing data will be helpful.
• 02-25-2006
richdb
I dont get it.
• 02-25-2006
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]?
• 02-25-2006
richdb
Are you saying I forgot to initialize them to 0?
• 02-26-2006
Dave_Sinkula
Well, to something.
Code:

```        int seats[10][20],                                 enter_class,                 first_class, /*first class subscript*/                 business_class; /*business class subscript*/```
• 02-26-2006
richdb
Aren't they initialized to 0 in each for loop?
• 02-26-2006
Dave_Sinkula
No. Look closely. One half is, the other half ain't. And for each it's the wrong half.

...And how do 2 arrays make you choose an array of 10?
• 02-26-2006
richdb
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]?
• 02-26-2006
Dave_Sinkula
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]?

• 02-26-2006
richdb
they are defined as integers as subscripts for the two sizes of the array. lol Im too tired
• 02-26-2006
Dave_Sinkula
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.
• 02-26-2006
richdb
Dont know what you mean by defined. #define first_class 10
#define business_class 20 ???
• 02-26-2006
Dave_Sinkula
Quote:

Originally Posted by richdb
Dont know what you mean by defined.

Has no known value.
• 02-26-2006
richdb
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;                 }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last