chapter 11 cs
TRANSCRIPT
-
8/12/2019 Chapter 11 CS
1/6
CASE STUDIES
1. Processing of Examination Marks
Marks obtained by a batch of students in the Annual Examination are tabulated as follows:
Student name Marks obtained
S.Laxmi 45 6 !" 55#.S.$ao "% 56 6%
& & & & & &
't is re(uired to com)ute the total marks obtained by each student and )rint the rank list based onthe total marks.
*he )ro+ram in ,i+.--.-4 stores the student names in the array nameand the marks in the arraymarks. After com)utin+ the total marks obtained by all the students the )ro+ram )re)ares and)rints the rank list. *he declaration
int marks[STUDENTS][SUBJECTS+1];
defines marks as a )ointer to the array/s first row. 0e use rowptr as the )ointer to the row ofmarks. *he rowptris initiali1ed as follows:
int (*rowptr)[SUBJECTS+1] = array;
2ote that arrayis the formal ar+ument whose 3alues are re)laced by the 3alues of the actualar+ument marks. *he )arentheses around *rowptrmakes the rowptr as a )ointer to an array ofSU!ECTS"1inte+ers. $emember the statement
int *rowptr[SUBJECTS+1];
would declare rowptras an array of SU!ECTS"1elements.
0hen we increment the rowptrby rowptr"1 the incrementin+ is done in units of the si1e ofeach row of array#makin+ rowptr )oint to the next row. Sincerowptr )oints to a )articular row$*rowptr%&x')oints to the xth element in the row.
POINTERS AND TWO-DIMENSIONAL ARRAYS
ro!ram
#define STUDENTS 5#define SUBJECTS 4#include !"in$%&'
()in*+
-
8/12/2019 Chapter 11 CS
2/6
,c&) n)(eSTUDENTS./0.1in" ()2!STUDENTS.SUBJECTS3.1
in"f*6Inu" !"uden"! n)(e! 7 "&ei ()2! in f8u !u9:ec"!;n6+1
$e"in"f*6;n6+1in"
>
?@ C8(u"e "8")l ()2! 89")ined 9 e)c& !"uden" @?
$e"
*@*8" 3 i++n-. 3 *@*8" 3 i++:.1>
>
?@ Pe)e )n2 li!" 9)!ed 8n "8")l ()2! @?
$e"
-
8/12/2019 Chapter 11 CS
3/6
c&) @"e(1
f8*i 1 i (-1 i33+f8*: 1 : (-i1 :33+
if* *@*8" 3 :-++n-. *@*8" 3 :++n-.+,
!)
>
?@ Pin" 8u" "&e )n2ed li!" @?
in">
?@ Ec&)n$e 8f in"e$e F)lue! @?
!)
f8*i 01 i /5H1 i33+!))e)i. ;01
i 01&ile*!i. ;0 77 i /5H+
-
8/12/2019 Chapter 11 CS
4/6
,!))e)i. !i.1i331
>i 01&ile*!/i. ;0 77 i /5H+
,!i. !/i.1!33i. ;01
>i 01&ile*!))e)i. ;0+,
!/i. !))e)i.1!/33i. ;01
>>
"#tp#t
Inu" !"uden"! n)(e! 7 "&ei ()2! in f8u !u9:ec"!S%L)(i 45 HK 55%S%R)8 KK 5H HA%u") HH K 45S%M)ni H K/ 0 /5R%D)niel 44 55 HH KK
S%L)(i 45 HK 55 /05%S%R)8 KK 5H H /A%u") HH K 45 /KS%M)ni H K/ 0 /5
R%D)niel 44 55 HH KK /4/
R)n2ed Li!"
%S%R)8 KK 5H H /A%u") HH K 45 /KR%D)niel 44 55 HH KK /4/S%L)(i 45 HK 55 /05S%M)ni H K/ 0 /5
Fig.11.14Preparation of the rank list of a class of students.
(. In)entory Upating
*he )rice and (uantity of items stocked in a store chan+es e3ery day. *hey may either increaseor decrease. *he )ro+ram in ,i+.--.-5 reads the incremental 3alues of )rice and (uantity andcom)utes the total 3alue of the items in stock.
*he )ro+ram illustrates the use of structure )ointers as function )arameters. +item#the addressof the structure item# is )assed to the functions ,pate$% and m,-$%. *he formal ar+umentspro,ctand stock#which recei3e the 3alue of +item#are declared as )ointers of ty)e str,ctstores.
-
8/12/2019 Chapter 11 CS
5/6
STRUCTURES AS QUNCTION PARAMETERSU!in$ !"uc"ue 8in"e!
ro!ram
!"uc" !"8e!
,c&) n)(e/0.1fl8)" ice1in" Gu)n"i"1
>1()in*+,
F8id ud)"e*!"uc" !"8e! @= fl8)"= in"+1fl8)"
-
8/12/2019 Chapter 11 CS
6/6
"#tp#tInu" ince(en" F)lue! ice ince(en" )nd Gu)n"i" ince(en"0 /Ud)"ed F)lue! 8f i"e(
N)(e YPice 5%K50000u)n"i" /4
)lue 8f "&e i"e( 5%000000
Fig.11.15Use of structure pointers as function parameters.