Lab13 2D Arrays
Transcript of Lab13 2D Arrays
-
8/10/2019 Lab13 2D Arrays
1/5
ICS 103: Computer Programming in C
Fall Semester 2010-2011 (Term-101)
Lab #13: To !imensional "rras
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
too many initializers.
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
scanf38d 9a'row)'col)74
Column-ise
for 3col*"4 col5csize col667
for 3row*"4 row5rsize4 row667
scanf38d 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
-
8/10/2019 Lab13 2D Arrays
2/5
: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&
-
8/10/2019 Lab13 2D Arrays
3/5
void addC2dCarrays3int a')'?B@) int b')'?B@) int c')'?B@) int rows int cols7 +
int i 4
for 3i*"4 i5rows4 i667 + for 3*"4 5cols4 667
c'i)') * a'i)') 6 b'i)')4
void printC2dCarray3int a')'?B@) int rows int cols7 +
int i 4
for3i*"4 i5rows4 i667 +
for 3*"4 5cols4 667 printf3E8%d E a'i)')74
printf3EInE74
Exercise 1:;rite a program where you declare a two-dimensional array labScoreswhich 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
input.txt into this array. ach row in the file input.txt contains 12 lab marks
corresponding to a certain student. As shown below&80 90 70 100 60 90 85 78 93 80 70 9898 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 98Student 2: 98 85 100 99 89 90 72 0 78 98 100 65
.
.
-
8/10/2019 Lab13 2D Arrays
4/5
(. 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 #include #define !"S 10#define !$S 81int %ystrc%p &ch'r s1()* ch'r s2()+,-id selectin/srt&ch'r ()(!$S)* int rs+, // prototypegivenint %'in&-id + int i, ch'r n'%es (!"S)(!$S) 4uSe4*4'hi%4* 4us'%4*4h%'d4*4';'h4* 4duli4* 4dul;'=eed4*4'y?f4*4@h'leA4*4Bdullh4 C,
printf&4'%es efre srtingDn4+, fr&i0,i
-
8/10/2019 Lab13 2D Arrays
5/5
int %ystrc%p &ch'r s1()* ch'r s2()+ int i,fr &i0, s1(i)IJD0JKKs2(i)IJD0J, iEE + if&tler&s1(i)+Itler&s2(i)++ return tler&s1(i)+Gtler&s2(i)+,C
return s1(i)Gs2(i),CFunction from lab 11 to be modified-id selectin/srt&int ()* int sie+int @*=*%inps*te%p,fr &@0, @ < sie G 1, @EE+ %inps @, LL initi'lie lc'tin f %in -'lue LL g -er the ele%ents t find lc'tin f %ini%u% -'lue fr&= @E1, = < sie, =EE+
if&(=) < (%inps)+%inps =,
CLL end f fr = lpLL ring %ini%u% -'lue hich is 't %inps 't inde @te%p (%inps),(%inps) (@),(@) te%p,C LL end f fr @ lpC
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(.