Академический Документы
Профессиональный Документы
Культура Документы
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.
The arrays with higher dimensions come handy when you need to do tensor calculations,
for instance.
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.
N −1
Ai , j = Bi ,k C k , j (Eq-1)
k =0
and run as
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
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).
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:
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.
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:
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 243-262 we implement the function that prints the matrix product.