C/C++ supports multidimensional arrays. The simplest form of the multidimensional
array is the two-dimensional array. A two-dimensional array is, essentially, an array of
one-dimensional arrays. To declare a two-dimensional integer array d of size 10,20, you
would write
int d[10][20];
Pay careful attention to the declaration. Some other computer languages use commas
to separate the array dimensions; C/C++, in contrast, places each dimension in its own
set of brackets.
Similarly, to access point 1,2 of array d, you would use
d[1][2]
The following example loads a two-dimensional array with the numbers 1 through 12
and prints them row by row.
#include <stdio.h>
int main(void)
{
int t, i, num[3][4];
for(t=0; t<3; ++t)
for(i=0; i<4; ++i)
num[t][i] = (t*4)+i+1;
/* now print them out */
for(t=0; t<3; ++t) {
for(i=0; i<4; ++i)
printf("%3d ", num[t][i]);
printf("\n");
}
return 0;
}
Two-dimensional arrays are stored in a row-column matrix, where the first index
indicates the row and the second indicates the column. This means that the rightmost
index changes faster than the leftmost when accessing the elements in the array in the
order in which they are actually stored in memory. See Figure 4-2 for a graphic
representation of a two-dimensional array in memory.
In the case of a two-dimensional array, the following formula yields the number of
bytes of memory needed to hold it:
bytes = size of 1st index x size of 2nd index x sizeof(base type)
Therefore, assuming 4-byte integers, an integer array with dimensions 10,5 would have
10 x 5 x 4
or 200 bytes allocated.
When a two-dimensional array is used as an argument to a function, only a
pointer to the first element is actually passed. However, the parameter receiving a
two-dimensional array must define at least the size of the rightmost dimension. (You
can specify the left dimension if you like, but it is not necessary.) The rightmost
dimension is needed because the compiler must know the length of each row if it is to
index the array correctly. For example, a function that receives a two-dimensional
integer array with dimensions 10,10 is declared like this:
void func1(int x[][10])
{
.
.
.
}
The compiler needs to know the size of the right dimension in order to correctly
execute expressions such as
x[2][4]
inside the function. If the length of the rows is not known, the compiler cannot
determine where the third row begins.
The following short program uses a two-dimensional array to store the numeric
grade for each student in a teacher's classes. The program assumes that the teacher has
three classes and a maximum of 30 students per class. Notice the way the array grade is
accessed by each of the functions.
/* A simple student grades database. */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define CLASSES 3
#define GRADES 30
int grade[CLASSES][GRADES];
void enter_grades(void);
int get_grade(int num);
void disp_grades(int g[][GRADES]);
int main(void)
{
char ch, str[80];
for(;;) {
do {
printf("(E)nter grades\n");
printf("(R)eport grades\n");
printf("(Q)uit\n");
gets(str);
ch = toupper(*str);
} while(ch!='E' && ch!='R' && ch!='Q');
switch(ch) {
case 'E':
enter_grades();
break;
case 'R':
disp_grades(grade);
break;
case 'Q':
exit(0);
}
}
return 0;
}
/* Enter the student's grades. */
void enter_grades(void)
{
int t, i;
for(t=0; t<CLASSES; t++) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
grade[t][i] = get_grade(i);
}
}
/* Read a grade. */
int get_grade(int num)
{
char s[80];
printf("Enter grade for student # %d:\n", num+1);
gets(s);
return(atoi(s));
}
/* Display grades. */
void disp_grades(int g[][GRADES])
{
int t, i;
for(t=0; t<CLASSES; ++t) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
printf("Student #%d is %d\n", i+1, g[t][i]);
}
}
array is the two-dimensional array. A two-dimensional array is, essentially, an array of
one-dimensional arrays. To declare a two-dimensional integer array d of size 10,20, you
would write
int d[10][20];
Pay careful attention to the declaration. Some other computer languages use commas
to separate the array dimensions; C/C++, in contrast, places each dimension in its own
set of brackets.
Similarly, to access point 1,2 of array d, you would use
d[1][2]
The following example loads a two-dimensional array with the numbers 1 through 12
and prints them row by row.
#include <stdio.h>
int main(void)
{
int t, i, num[3][4];
for(t=0; t<3; ++t)
for(i=0; i<4; ++i)
num[t][i] = (t*4)+i+1;
/* now print them out */
for(t=0; t<3; ++t) {
for(i=0; i<4; ++i)
printf("%3d ", num[t][i]);
printf("\n");
}
return 0;
}
Two-dimensional arrays are stored in a row-column matrix, where the first index
indicates the row and the second indicates the column. This means that the rightmost
index changes faster than the leftmost when accessing the elements in the array in the
order in which they are actually stored in memory. See Figure 4-2 for a graphic
representation of a two-dimensional array in memory.
In the case of a two-dimensional array, the following formula yields the number of
bytes of memory needed to hold it:
bytes = size of 1st index x size of 2nd index x sizeof(base type)
Therefore, assuming 4-byte integers, an integer array with dimensions 10,5 would have
10 x 5 x 4
or 200 bytes allocated.
When a two-dimensional array is used as an argument to a function, only a
pointer to the first element is actually passed. However, the parameter receiving a
two-dimensional array must define at least the size of the rightmost dimension. (You
can specify the left dimension if you like, but it is not necessary.) The rightmost
dimension is needed because the compiler must know the length of each row if it is to
index the array correctly. For example, a function that receives a two-dimensional
integer array with dimensions 10,10 is declared like this:
void func1(int x[][10])
{
.
.
.
}
The compiler needs to know the size of the right dimension in order to correctly
execute expressions such as
x[2][4]
inside the function. If the length of the rows is not known, the compiler cannot
determine where the third row begins.
The following short program uses a two-dimensional array to store the numeric
grade for each student in a teacher's classes. The program assumes that the teacher has
three classes and a maximum of 30 students per class. Notice the way the array grade is
accessed by each of the functions.
/* A simple student grades database. */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define CLASSES 3
#define GRADES 30
int grade[CLASSES][GRADES];
void enter_grades(void);
int get_grade(int num);
void disp_grades(int g[][GRADES]);
int main(void)
{
char ch, str[80];
for(;;) {
do {
printf("(E)nter grades\n");
printf("(R)eport grades\n");
printf("(Q)uit\n");
gets(str);
ch = toupper(*str);
} while(ch!='E' && ch!='R' && ch!='Q');
switch(ch) {
case 'E':
enter_grades();
break;
case 'R':
disp_grades(grade);
break;
case 'Q':
exit(0);
}
}
return 0;
}
/* Enter the student's grades. */
void enter_grades(void)
{
int t, i;
for(t=0; t<CLASSES; t++) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
grade[t][i] = get_grade(i);
}
}
/* Read a grade. */
int get_grade(int num)
{
char s[80];
printf("Enter grade for student # %d:\n", num+1);
gets(s);
return(atoi(s));
}
/* Display grades. */
void disp_grades(int g[][GRADES])
{
int t, i;
for(t=0; t<CLASSES; ++t) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
printf("Student #%d is %d\n", i+1, g[t][i]);
}
}
No comments:
Post a Comment