0% found this document useful (0 votes)
69 views4 pages

Objective:: ICS 103: Computer Programming in C Topic: 2-D Array

This document provides information on 2-D arrays in C programming, including: 1) How to declare and initialize a 2-D array with examples of integer, character, and float arrays. 2) How to read and print 2-D arrays using nested for loops in either row-major or column-major order. 3) An example program that adds two matrices stored as 2-D arrays and prints the sum.

Uploaded by

alwafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

Objective:: ICS 103: Computer Programming in C Topic: 2-D Array

This document provides information on 2-D arrays in C programming, including: 1) How to declare and initialize a 2-D array with examples of integer, character, and float arrays. 2) How to read and print 2-D arrays using nested for loops in either row-major or column-major order. 3) An example program that adds two matrices stored as 2-D arrays and prints the sum.

Uploaded by

alwafi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

ICS 103: Computer Programming in C

Handout-16 Topic: 2-D Array



Objective :
To know what is 2-D array?
To know how to declare and reference 2-D array.
To know how to read and print 2-D array using for loops.
To know what is row-major and column-major order of 2-D array.
What is 2_D Array ? How to declare and initialize it ?:
Just like single dimensional arrays we can have multidimensional arrays
which are arrays of arrays. The two dimensional array can be visualied as
a matri!.
"onsider the following two-dimensional array declarations #
$define %&'( )
$define %&'(* 2)
int grades +%&'(, +*-, .
char letter +*-, +/, .
float !y +*--, +%&'(*, .
The two-dimensional arrays need two sies as name+m,+n,. The total sie
of the array is the product of the two sies m ! n. The first element is
always name+-,+-, and the last element is name+m-*,+n-*,. The two
dimensional array elements are ordered by row-wise as name+-,+-,0
name+-,+*,0 name+-,+2,10 name+-,+2,0 name+*,+-,0 name+*,+*,010 name+/,
+3,0 name+/,+2,. The two dimensional array elements are ordered by
column-wise as name+-,+-,0 name+*,+-,0 name+2,+-,10 name+/,+-,0 name+-,
+*,0 name+*,+*,010 name+4,+2,0 name+/,+2,.
&n the first case0 grades is an integer array of sie ) 5 *- 6 )-. The
elements can be referenced by using 2 subscripts as grades+*,+2,0
grades+/,+2,. The first element is grades+-,+-, and the last element is
grades+/,+2,. %imilarly0 the letter is an array of characters of sie /-
with last element being letter+2,+4,. !y is an array of float of sie 2)--
with last element being !y+22,+2/,.
The two-dimensional arrays can be initialied as
int ! +4, +2, 6 7 *20 )80 890 /0 8 093 : .
1
where the !+-,+-, 6 *20 !+-,+*, 6 )80 !+*,+-, 6 890 !+*,+*, 6 /0 !+2,+-, 6 80
!+2,+*, 6 93. ;s with single dimension arrays if the initial values are less
than the size of array the remaining values are set to 0.
How to read and print 2-D Array usin two !or loops ?:
The two-dimensional arrays can also be initialied by using for loop.
To initialie0 read and print two-dimensional arrays we need two for loops.
The first loop controls the first subscript and the second loop controls
the second subscript. "onsider the following e!ample#
int abc+),+*-, 0 i0 j .
for < i 6 - . i = ) . >> i ?
for < j 6 -. j = *- . >>j ?
7 printf <@(nter the +Ad,+Ad, element BnC0 i0 j ? .
scanf <@ Ad C0 Dabc+i,+j, ? .
:
&n the above case the array is read in row-major order. To read in
column-major order just reverse the two for loops of two subscripts as
for < j 6 - . j = *- . >> j ?
for < i 6 -. i = ) . >>i ?
7
printf <@(nter the +Ad,+Ad, element BnC0 i0 j ? .
scanf <@ Ad C0 Dabc+i,+j, ? .
:
%imilarly for printing in row-major order
for < i 6 - . i = ) . >> i ?
for < j 6 -. j = *- . >>j ?
7
printf <@The abc+Ad,+Ad, element is Ad BnC0 i0 j0
abc+i,+j, ? .
:
and printing in column-major order
for < j 6 - . j = *- . >> j ?
for < i 6 -. i = ) . >>i ?
2
7
printf <@The abc+Ad,+Ad, element is Ad BnC0 i0 j0
abc+i,+j, ? .
:
"olved #roble$ !or 2-d array:
/*Matri !ddition */
$include=stdio.hE
$define row *-
$define col *-
int main<?
7
int i0j0k0 a+row,+col,0 b+row,+col,0 s+row,+col, 6 7-:.
int rowsa0 colsa0 rowsb0 colsb.
printf<F(nter number of rows for Gatri! *# F?.
scanf<FAdF0 Drowsa?.
printf<F(nter number of columns for Gatri! *# F?.
scanf<FAdF0 Dcolsa?.
printf<F(nter the Ad elements of Gatri! * # BnF0 rowsaHcolsa?.
for<j6-.j=rowsa. j>>? // reading matri a
7
for<k6-.k=colsa.k>>?
7
scanf<FAdF0 Da+j,+k,?.
:
:
puts<FBnF?.
printf<F(nter number of rows for Gatri! 2# F?.
scanf<FAdF0Drowsb?.
printf<F(nter number of columns for Gatri! 2# F?.
scanf<FAdF0Dcolsb?.
printf<F(nter the Ad elements of Gatri! 2 # BnF0 rowsbHcolsb?.
for<j6-.j=rowsb. j>>? // reading matri b
7
3
for<k6-.k=colsb.k>>?
7
scanf<FAdF0 Db+j,+k,?.
:
:
/* !ddition of two matrices */
for<j6-. j=rowsb. j>>?
7
for<k6-. k=colsb. k>>?
7
s+j,+k,6a+j,+k,>b+j,+k,.
:
:
/*"rint sum of two matrices */
printf<FThe sum of two matrices is # BnF?.
for<j6-. j=rowsa. j>>?
7
for <k6-. k=rowsb. k>>?
7
printf<FA)d F0 s+j,+k,?.
:
printf<FBnF?.
:
return -.
: // end of main
4

You might also like