Download - Two dimensional array
Two dimensional
- Two dimensional arrays are declared similarly to one dimensional arrays:
char t[20][10]; (and referred to as a matrix, or as a "table")
The first subscript gives the row number, and the second subscript specifies column number.
We can also initialize a two-dimensional array in a declaration statement; E.g.,
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
which specifies the elements in each row of the array (the interior braces around each row of values could be omitted). The matrix for this example is
654
321m
Specification of the number of rows could be omitted. But other subscript specifications can not be omitted. So, we could also write the initialization as
int m[ ][3] = {{1, 2, 3}, {4, 5, 6}};
int a[2][3]= {1,2,3,4,5,6};
specifies 6 integer locations.
Storage for array elements are in contiguous locations in memory in row major order (unlike column major order in Fortran), referenced by subscripts(or index) values starting at 0 for both rows and columns.
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
RAM
1 2 3 4 5 6
1. Problem Definition
Assume we have a file “in.dat” (located in the pwd) consisting of 10 rows of 10 integers per row (100 integers total). Write a C program which reads from the file and stores the numbers in a two dimensional array named ‘mat’, computes the largest of all of these numbers and prints the largest value to the screen.
2. Refine, Generalize, Decompose the problem definition
(i.e., identify sub-problems, I/O, etc.)
Input = from file “in.dat”
Output= Largest of the 100 integers printed to screen.
3. Develop Algorithm
Use Unix redirection to read file “in.dat”.
Use nested for loop to go through two dimensional array to
find the largest value.
#include <stdio.h>
void main(void){ int row, col, maximum;
/* Declare a 2-D array called ‘mat’ which can hold a 10-by-10 */ /* matrix of integers */ int mat[10][10]; /* Write code here to read from the file ‘in.dat’ */ for(row=0;row<10;++row) for(col=0;col<10;++col)
scanf("%i",&mat[row][col]);
/* Write code to find the largest value in the array ‘mat’ */ maximum = mat[0][0]; /* why ??? */ for(row=0;row<10;++row) /* How does this work??? */ for(col=0;col<10;++col)
if (mat[row][col] > maximum)maximum = mat[row][col];
/* Print the largest value to the screen */ printf(" max value in the array = %i\n",maximum); }
The typedef mechanism in C allows us to define our own data types. For example,
typedef double matrix[10][20];
In this example we create a new data-type named matrix. We can declare a variable of data-type matrix by
matrix a;
and this declaration is equivalent to
double a[10][20];
Other examples of the use of typedef are,
typedef int blah; /* don’t do this !!!!! */
typedef char string[5];
In the first example we give another name or alias to the data type int .
In the second example we create a new data-type named string.
To declare a variable of data-type blah ,
blah x;
this declaration is equivalent to int x;
To declare a variable of data-type string ,
string var ="init"; /* declare and initialize */scanf("%s",var); /* read in a string of chars */
strcpy(var , "next");
The declaration
string morse[26];
declares morse as an array of 26 elements. Each element has data-type string.
The declaration and initialization:
string morse[26] = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...",
"-", "..-", "...-", ".--", "-..-", "-.--", "--.. " };
declares morse as an array of 26 elements with
morse[0] = ".-"
morse[1] = "-..." and so on...
Standard (ANSI) C allows up to 12 dimension.
But more computation is required by the system to locate elements in multi-subscripted arrays. Therefore, in problems involving intensive numerical calculations, we should use arrays with fewer dimension.
We can also use 3, 4, … dimensional arrays, e.g.,
threeDArray [i][j][k];