Sei sulla pagina 1di 5

# ICS 103: Computer Programming in C

## Fall Semester 2010-2011 (Term-101)

Lab #13: To !imensional "rra#s
Objective:
Practice with 2-Dimensional Arrays
Declaration
A 2-D array can be visualized as a matrix or a table of data elements consisting of rows
and columns.
Declaration is similar to 1-D array except an additional number is provided in bracket
representing the size of the second dimension.
xample: int gra\$es%100&%'&
!he first size indicates the number of rows and the second the number of columns
!he indexing starts from ". #n the above example the row indexes are from " to \$\$ and
column indexes are from " to %.

Initialization
A 2-D array can be initialized at declaration by nesting the braces for each row in another
braces as shown below&
int x'()'2) * + +1,-22.- +-((-/%.- +10-(1..
#n the above- if the data is not enough- the remaining elements are set to zero.
#f number of values is more than the size of each row- then we get the compilation error
1too many initializers2.
Data can also be read into a 2-D array using nested loops. !his can be row-wise or
column-wise as shown below&
(o-ise
for 3row*"4 row5rsize- row667
for 3col*"4 col5csize4 col667
scanf318d2- 9a'row)'col)74
Column-ise
for 3col*"4 col5csize- col667
for 3row*"4 row5rsize4 row667
scanf318d2- 9a'row)'col)74

Passing a multi-dimensional array as a parameter
!his is similar to 1-D array - the argument is the array name followed by brackets for
each of the dimensions
:owever- the sizes of all the dimensions except the first- must be specified.
;hen calling a function that takes a 2-D array as argument- we only give the array name
with no brackets.
Example:
!he following program uses functions to read two 2-D arrays and compute their sum in
another array and print it&
<include5stdio.h=
<define >?;@ 1"
<define A?B@ 1"
void readC2dCarray3int ')'A?B@)- int rows- int cols74
void addC2dCarrays3int a')'A?B@)- int b')'A?B@)- int c')'A?B@)- int rows- int cols74
void printC2dCarray3int a')'A?B@)- int rows- int cols74
int main3void7 +
int i- D- a'>?;@)'A?B@)- b'>?;@)'A?B@)- c'>?;@)'A?B@)4
int rows- cols4

printf3Enter number of rows for the Fatrix& E74
scanf3E8dE- 9rows74
printf3Enter number of columns for the Fatrix& E74
scanf3E8dE- 9cols74

printf3E!he sum of two matrices is& InE74
printC2dCarray3c- rows- cols74 GHJrint sum of two matrices HG
system3EpauseE74
return "4
.
void readC2dCarray3int a')'A?B@)- int rows- int cols7 +
int i- D4
printf3Enter the 8d elements of the 2-D array row-wise& InE- rows H cols74

for3i*"4 i5rows4 i667 +
for3D*"4 D5cols4 D667
scanf3E8dE- 9a'i)'D)74
.
.
void addC2dCarrays3int a')'A?B@)- int b')'A?B@)- int c')'A?B@)- int rows- int cols7 +
int i- D4

for 3i*"4 i5rows4 i667 +
for 3D*"4 D5cols4 D667
c'i)'D) * a'i)'D) 6 b'i)'D)4
.
.
void printC2dCarray3int a')'A?B@)- int rows- int cols7 +
int i- D4

for3i*"4 i5rows4 i667 +
for 3D*"4 D5cols4 D667
printf3E8%d E- a'i)'D)74
printf3EInE74
.
.
Exercise 1:
;rite a program where you declare a two-dimensional array labScores which contains 2%
rows and 12 columns. ach row corresponds to a particular student and each column
corresponds to a particular lab score.
Define the number of rows and columns as constants.
1. ;rite a function with a 2-D array as parameter. !he function reads data from a file
1input.txt2 into this array. ach row in the file 1input.txt2 contains 12 lab marks
corresponding to a certain student. As shown below&
80 90 70 100 60 90 85 78 93 80 70 98
98 85 100 99 89 90 72 0 78 98 100 65
K% ,2 \$% ,% /0 KK \$/ 0% %% /K \$K K\$
/, 11 2K K\$ K% \$" \$K K% K, %/ /\$ 22
2. Add a function to print the scores so that each studentLs labs appear on a separate line of
output. #nclude a statement in your main program to call this function. Mour output should be
labeled as follows&
Student 1: 80 90 70 100 60 90 85 78 93 80 70 98
Student 2: 98 85 100 99 89 90 72 0 78 98 100 65
.
.

(. Add a function- StudentAvg()- which receives the 2-D array of scores and returns an array
containing the lab average of each student.
0. Add a function- labAvg()- which receives the 2-D array of scores and returns an array
containing the average score of each individual lab.
!he main function should call all these functions and print the results on the screen.
Exercise 2:
Nsing selectionCsort of lab 11- and modified strcmp of lab 12- modify selectionCsort so that it
receives an array of strings 32-D array of type char7 and sorts the strings alphabetically.
xample&
#include <string.h>
#include <ctype.h>
#define !"S 10
#define #!\$S 81
int %ystrc%p &ch'r s1()* ch'r s2()+,
-.id selecti.n/s.rt&ch'r 0()(#!\$S)* int r.1s+, // prototype
given
int %'in&-.id +2
int i,
ch'r n'%es (!"S)(#!\$S) 3 245.uSe64*478'hi%4* 49us'%4*
4:h%'d4*49';<'h4* 4:8dul:<i<4* 4:8dul;'=eed4*4>'y?f4*
4@h'leA4*4:Bdull:h4 C,
printf&4>'%es 8ef.re s.rtingDn4+,
f.r&i30,i<!"S,iEE+
printf&4FG1Hs4*n'%es(i)+,
selecti.n/s.rt&n'%es*!"S+,
printf&4Dn>'%es 8ef.re s.rtingDn4+,
f.r&i30,i<!"S,iEE+
printf&4FG1Hs4*n'%es(i)+,
return 0,
C
int %ystrc%p &ch'r s1()* ch'r s2()+ 2
int i,
f.r &i30, s1(i)I3JD0JKKs2(i)I3JD0J, iEE + 2
if&t.l.1er&s1(i)+I3t.l.1er&s2(i)++
return t.l.1er&s1(i)+Gt.l.1er&s2(i)+,
C
return s1(i)Gs2(i),
C
Function from lab 11 to be modified
-.id selecti.n/s.rt&int 0()* int si<e+2
int @*=*%inp.s*te%p,
f.r &@30, @ < si<e G 1, @EE+ 2
%inp.s 3 @, LL initi'li<e l.c'ti.n .f %in -'lue
LL g. .-er the ele%ents t. find l.c'ti.n .f %ini%u% -'lue
f.r&= 3 @E1, = < si<e, =EE+2
if&0(=) < 0(%inp.s)+
%inp.s 3 =,
CLL end .f f.r = l..p
LL 8ring %ini%u% -'lue 1hich is 't %inp.s 't inde0 @
te%p 3 0(%inp.s),
0(%inp.s) 3 0(@),
0(@) 3 te%p,
C LL end .f f.r @ l..p
C
Exercise :
;rite a program to multiply two ( O ( matrices. Mou will need to use two-dimensional arrays.
!he declaration should be of the form double matrix1'()'()- matrix2'()'()- matrix('()'().
Mou should use nested for-loops. ;rite the whole program in the main function without using
any functions.
Exercise !:
Fodify your solution to the previous exercise so that the matrix multiplication is done in a
separate function. !he function will need at least three arguments- two of them being input
matrices and one being the resulting output matrix. !he arguments should be declared as
double matrix1'()'()- double matrix2'()'() and double matrix('()'(). ;hen calling the
functions- the matrix arguments should be specified using their names- matrix1- matrix2 and
matrix(.