Вы находитесь на странице: 1из 4

Multi-dimensional Arrays

The C language allows multi-dimensional arrays. An example is the representation of a


matrix: one can define a two-dimensional array to represent any matrix in C. Consider a
matrix of N X N, each entry of which is a floating number. It can be represented as

float m[N] [N];

An element of this matrix m, say mi , j , will be accessed as

m [i-1] [j-1]

Note that indices are one smaller than what we typically use in mathematics since an
array index starts from 0 in C language.

Multi-dimensional arrays are not limited to 2-dimensional arrays. C allows matrices of


arbitrary dimensions. For instance, one can define a 4-dimensional integer array as
follows:

int k [N] [M] [K] [L];

The arrays with higher dimensions come handy when you need to do tensor calculations,
for instance.

Variable Length Arrays

So far we have seen arrays only with fixed length. In other words, when we define an
array, we always defined the length in each dimension. (For instance, in the previous
lecture Listing 11 worked with a one-dimensional array of length 10.) C allows variable
length arrays. We will see how this is done when we talk about the concept of a pointer.

An example of a multi-dimensional array use in a program

An example program named matrixmultiplication.c is posted on the lectures page. This


is the example we will use to complete our discussion of multi-dimensional arrays this
week. What this program does is to read in two 2-dimensional arrays that represent
matrices of 3 X 3, calculates the matrix product, and prints the result. In other words,

A = B ⋅C where A, B, and C are matrices. So,

N −1
Ai , j = Bi ,k C k , j (Eq-1)
k =0

The program is in hbar server (usual location). It is compiled as


[gunaly@hbar p488C]$ gcc matrixmultiplication.c -o matrixmultiplication

and run as

[gunaly@hbar p488C]$ ./matrixmultiplication m.dat n.dat


Multiplication result is:
2.000000 9.000000 2.000000
4.000000 14.000000 2.000000
4.000000 3.000000 0.000000

Note that this program takes in two arguments on command-line. Each argument is the
name of the file where I stored a sample 3 X 3 matrix. The file m.dat has the following
content:

121
231
101

and n.dat has the following content:

3 2 -1
-1 3 1
111

The sample program uses file operations, which I was planning to cover later in the
course. For now, I will briefly go over how files are read in C.

As usual, I put in most of my comments of the program inside the program. I will briefly
go over it here, but I strongly urge you to read through the program. One learns
programming by writing programs and by reading the programs other people have written
(possibly, you can also ruin your knowledge by reading other people’s programs thinking
that a particular style is a good-programming approach; more on this later in the course).

Line 6 defines the dimension of our matrices: we will multiply 3 X 3 matrices.


Line 17 defines a function that reads numbers from a file and sets the matrix elements.
Line 31 defines a function that takes in two matrices to multiply and a matrix to put the
results into.
Line 43 defines a function that takes in a matrix and prints it on console.
Line 70 is where our main program starts.
Lines 75-76 define names of files from which we will read our matrices.
Line 82 defines the first 3 X 3 matrix operand as a 2-dimensional array.
Line 88 defines the second 3 X 3 matrix operand as a 2-dimensional array.
Line 93 defines the matrix product, also 3 X3 of course, as a 2-dimensional-array.
Lines 98-99 set file names from command-line arguments.
Line 104 calls the readMatrix() function to read in the first array.
Line 109 calls the readMatrix() function to read in the second array.
Line 114 calls the multiplyMatrices() function to take the matrix product.
Line 120 calls the printMatrix() function to print the matrix product.

Note that main program itself does not do much, but calls functions that actually do stuff.
This is what people call as modular programming: divide logically distinct tasks into
smaller pieces and make them functions. Here tasks are read, multiply, and print, details
of which are in functions, not in main program.

In lines 134-234 we implement the function that reads a matrix from a file. Here is a
brief introduction to reading from files:

Reading from a file


Reading from a file could be visualized as going through documents in a folder: you first
need to find the folder (analogous to having a file name), open the folder and hold the
folder, then look through what the folder contains, and finally close the folder. Let’s see
how this is done in C in the context of reading a matrix from a file.

First of all, you need to define a handle to access a file (analogous to holding the folder in
hand):

FILE* fp;

This line defines a pointer to a file. We will not cover the pointers yet, just think of this
as a way to hold the folder in your hands.

Then, you need to open the file to read (In C you need to explicitly specify that your
intention is to just read the file and not, say, write to it):

fp = fopen(fileName, "r");

Note that the first argument is the name of the file, so that file can be located, and the
second argument states that you are opening this file to read from it. When a file is
opened, a handle is returned to it, so we assign the handle to variable “fp”. This operation
is analogous to opening a folder and holding it in your hands.

A floating number can be read from a file as follows:

fscanf(fp, "%f", &number);

Note how similar this is to reading from console, scanf(“%f”, &number). What we have
here additionally is the slight change in name, fscanf() rather than scanf(), and the fact
that fscanf() takes an additional argument, which is the file handle.
Finally, you need to close the file:

fclose(fp);
In lines 160-174 we read in the whole 3 X 3 matrix. Here we have a so-called nested for-
loop, which means a for-loop within a for-loop. Consider the following example:

for (i = 0; i < 3; i++)


{
for (j = 0; j < 3; j++)
{
/* some operation */
….
}
}

How this works is, i is set to 0, then j runs from 0 up to 2, then i is set to 1 and j runs
from 0 up to 2, and finally, i is set to 2 and j runs from 0 up to 2.

In lines 194-234 we calculate the matrix product. Specifically, in lines 214-233 we


implement Eq-1 shown above.

In lines 243-262 we implement the function that prints the matrix product.

Вам также может понравиться