Upload
jocansino4496
View
227
Download
0
Embed Size (px)
Citation preview
8/13/2019 Exercise 10 n Structures
1/45
Exercise: Structures
1. Product / Inventory Program
a) Create a structure to store information about productsfor sale in a store. Variables of this type should storeinformation about the product name, the price, and the product number.
name: tshirtproductnumber: 3788price: 15.99
b) rite functions called inputProduct and outputProductthat can be used to !or" !ith this data structure.
c) Create an array of products called Inventory. #dd fi$e products to your in$entory:
Example:1%3 mu& 1.99
1%' pen (.99 1%5 tshirt 19.99 1% poster 5.99 1%7 "eyrin& 3.99
d) rite a function called outputInventorythat outputs the in$entory array. *he array and it+s sie should be parametersto the function.
e) rite a function called sortInventorythat sorts the in$entory by price. -*he in$entory array and sie should beparameters to the function). utput the in$entory after!ards to ensure that it is sorted.
f) rite a function called priceLookupthat has a strin& parameter for the product name, and then returns the price forthat product. /f there is no such product in in$entory,the function should return a price of (.
2. Customer ills
0tend your 2roduct /n$entory pro&ram to process customer bills. # bill consists of se$eral items. 0ach item on thecustomer bill consists of a product name, a 4uantity, and a total.
Example: Customer bill
a) Create t!e "ill : 2rompts the user to enter purchase information and creates customerill array. *he user should entera product name and a 4uantity. *he pro&ram should continue promptin& for bill items until the user chooses to end their
purchase.
b ) #utput t!e "ill:utput the final bill information in a user friendly !ay. /nclude the total.
4uantity: 3name: mu&total: 5.97
4uantity: 1name: postertotal: 5.99
4uantity: 1(name: "eyrin&total: 39.9(
8/13/2019 Exercise 10 n Structures
2/45
Structuresare a po!erful data type capable of storin& multiple $alues. 6nli"e arrays,ho!e$er, structures can hold data of multiple types -included nested structures).
et+s suppose that !e !ant to store data about people. 0ach person has a name, a !eig!t,
and aneye colour. *hese $alues could be stored as a series of simple $ariables a strin&for the name, a double for the hei&ht, and an inte&er code for the eye colour. /f !e !anted
to store information for se$eral people, !e could use three arrays one for the names, onefor the hei&hts, and one for the eye colour.
# better !ay is to use tructures. e could ha$e a sin&le structure for a 2erson, and then
ha$e an array of 2ersons. #ll of the information about a sin&le person could be containedinside onedata structure. *he pro&rammer defines a structure as sho!n belo!:
struct personcompartments>) for each field.
personfriend,mother;
8/13/2019 Exercise 10 n Structures
3/45
cout
8/13/2019 Exercise 10 n Structures
4/45
?ou can define structures !here one of the fiends of the structure is a structure # typicaleample !ould be to ha$e a person structure, !ith an address field, !here the address is
another structure.
ex.
struct address) char street21; // street address (number appartment street!
char city21; // city
char ro!ince21; // province
char ostalcode2;// postal code
-;
struct erson )
string name;
address location;// person's address
int age;
-;
....
erson sally;
cout
8/13/2019 Exercise 10 n Structures
5/45
7
8
$1
%
11
1
21
3
1
41
5
1
61
71
8
1$
2
%
21
2
22
3
24
2
5
26
2
72
8
2$
3
%
3
float!olume;-;
!oiddislay?alues$(o: :&; rototye dislay?alues func!oidset?olume$(o: @:&; rototye set?olume func
intmain$&) (o: firstAo: = )"3haul",1,B,15,7-; initiali9e firstAo:
structure
dislay?alues$firstAo:&; dislay firstAo:
get user inut for dimensions of second (o: structure
stdcout secondAo:.length;
set?olume$EsecondAo:&; use func set?olume to assign a !alue to
secondAo:.!olume
stdcout
8/13/2019 Exercise 10 n Structures
6/45
8/13/2019 Exercise 10 n Structures
7/45
'()*+, $: - /+, /0* (**
'y rainin" eriod: xx hours
Note: The examples in this Module wee !ompiled and un usin" Win32 emptyconsole applicationwithout #de$ %de$inition& and #! %esou!e& $iles %'oland( )llpo"am examples ha*e een tested usin" 'oland C++ ,#xx ONLY# It should e O- i$.ou use Win32 empty console applicationas a ta"et $o othe !ompiles e!auseheade $iles used ae $om C++ tandad Lia.# Fo Linux/Unix0 .ou ha*e to !on!enaout the path o die!to. a!!ess e!ause o$ the di$$eent $ile s.stem# ou ma. needsome !ode modi$i!ation and een le$t $o .ou assi"nments :o ou ma. !onside
eadin" e!tion 23#30 4amespa!es$ist0 $o usin" taditional0 $ull. !omplied C++ omixin" the C and C++ !odes# Fo C++ and MFC %5indows 6UI po"ammin"& it is!alled eiali7ation and the topi!s ae in in"le Do!ument Inte$a!e%DI& and MultipleDo!ument Inte$a!e%MDI The C standad $ile input/output is dis!ussed in C FileInput/Output# The sou!e !ode $o this Module is C++ File I/O sou!e !odes#The C++ file input/output programming skills:
*nderstand and use the ifstrea! ofstrea! and fstrea! class obects.
*nderstand and use a seuential access file ead and Write !e!ber functions. *nderstand and use a rando! access file ead and Write !e!ber functions.
e fa!iliar #ith other file /9( !e!ber functions.
!" #ntro$uction The idea aout the steam has een explained extensi*el. in C $ile I/O and C++
$omatted I/O also has een explained in C++ Fomatted I/O# ) $ile 8ust a !olle!tion o$ elated data that teated . C++ as a seies o$ .tes# '. the wa.0 steams mo*e in one wa. and in seial $ashion $om e!ei*e and
sende# Othe than $iles on dis0 de*i!es su!h as ma"neti! tapes0 pima. o se!onda.
http://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/Module23.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc11sdi.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc12mdi.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc12mdi.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/download/Module19.txthttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/Module18.htmlhttp://www.tenouk.com/Module23.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc11sdi.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc12mdi.htmlhttp://www.tenouk.com/visualcplusmfc/visualcplusmfc12mdi.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/download/Module19.txthttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/Module18.htmlhttp://www.tenouk.com/Module9.html8/13/2019 Exercise 10 n Structures
8/45
stoa"e de*i!es0 pintes and netwos that !a.in" data ae also teated as$iles#
File I/O is one o$ the inteestin" topi!s in C / C++ e!ause o$ the wideappli!ations# 5e need to wite to dis when we do so$twae installation0 witin"windows e"ist.0 ead0 wite0 delete0 update $ile !ontents0 sendin" and e!ei*in"
data thou"h netwos %so!ets& et!# The in!lude $iles needed in ode to use the dis $ile I/O !lass o8e!ts ae:
iosteam; and $steam;# iosteam !ontains standad input output o8e!tssu!h as !in0 !out0 !e and !lo" %e$e to C++ Fomatted I/O
-eep in mind that new heade $iles whi!h ae $ull. C++ standad !omplian!e thatusin" template ased heade $iles %IO/IEC C++& don
5hee:1# name is the $ilename#2# Open $ile mode s %$la"s& ae used to indi!ate what to do to the $ile %e#"#
open0 !lose& and the data %e#"# ead0 wite The $la"s !an e lo"i!all.O?ed# Mode must e one o$ the $ollowin":
&o$e 'escription
pp )ppend data to the end o$ the output $ile#e 6o to the end o$ the $ile when open#
Open $o input0 with open%&meme $un!tion o$ the i$steam*aiale#ut Open $o output0 with open%&meme $un!tion o$ the o$steam*aiale#na. 'ina. $ile0 i$ not pesent0 the $ile is opened as an )CII $ile as de$ault#un! Dis!ad !ontents o$ existin" $ile when openin" $o wite#o!eate Fail i$ the $ile does not exist# Fo output $ile onl.0 openin" an input $ile alwa.s $ails i$ thee is noepla!e Do not o*ewite existin" $ile# I$ a $ile exists0 !ause the openin" $ile to $ail#
http://www.tenouk.com/Module18.htmlhttp://www.tenouk.com/Module23.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/ModuleA.htmlhttp://www.tenouk.com/Module18.htmlhttp://www.tenouk.com/Module23.htmlhttp://www.tenouk.com/Module9.htmlhttp://www.tenouk.com/ModuleA.html8/13/2019 Exercise 10 n Structures
9/45
Tale 19#2: File open modes3# File pote!tion a!!ess detemines how the $ile !an e a!!essed# It is
Opeatin" .stem dependent and thee ae othes attiutes that aeimplementation extensions# Fo DO( example0 it must e one o$ the$ollowin":
(ttri)utes 'escription@ 4omal $ile o )!hi*e1 ?eadAonl. $ile2 Bidden $ile
.stem $ile
Tale 19#3: File t.pes To de!lae the input $ile steam i#e# $o eadin" we would de!lae lie this:
i$steam theinputfile> To de!lae the output $ile steam i#e# $o witin" we would de!lae lie this:
o$steam theoutputfile> Then0 to !onne!t to a steam0 let sa. openin" $ile test$ileio#dat $o eadin"0 the $ile
whi!h lo!ated in the same $olde as the exe!utale po"am we would wite liethis:
theinputfile#open%test$ileio#dat &> O with the path we !ould wite:
theinputfile#open%!:test$ileio#dat &> 4ext0 openin" a $ile $o eadin" and ina. t.pe modes0 we !ould wite:
theinputfile#open%test$ileio#dat 0 ios::in ios::ina.&> )nothe example0 openin" a $ile $o eadin"0 with nomal t.pe a!!ess and "o to
the end o$ the $ile0 we !ould wite lie this:theinputfile#open%test$ileio#dat 0 ios::in ios::ate0 @&>
Fo witin"0 to !onne!t to a steam0 let sa. openin" $ile test$ileio#dat $o witin"0the $ile whi!h lo!ated in the same $olde as the unnin" po"am# Ge*ious!ontent o$ the test$ileio#dat will e o*ewitten0 we !ould wite lie this:
theoutputfile#open%test$ileio#dat&> O with the path:
theoutputfile#open%!:test$ileio#dat &> Then0 openin" a $ile $o witin" and appendin" at the end o$ the $ile modes0 we
!ould wite lie this:theoutputfile#open%test$ileio#dat0 ios::out ios::app&>
O openin" $o witin"0 !he! the existin" o$ the $ile with nomal a!!ess modes0we !ould wite lie this:
theoutputfile#open%test$ileio#dat0 ios::out ios::no!eate0 @&> )$te we ha*e !ompleted the $ile po!essin"0 we ha*e to !lose o dis!onne!t the
steam to $ee up the esou!es to e used . othe po!esses o po"ams#Usin" the !lose%& meme $un!tion0 $o input steam we would wite lie this:
theinputfile#!lose%&> )nd $o output steam:
theoutputfile#!lose%&>
8/13/2019 Exercise 10 n Structures
10/45
Duin" the openin" $o eadin" o witin"0 we should po*ide eo handlin"outines to mae sue the $ile opeations ha*e !ompleted su!!ess$ull. othewisesome eo messa"e should e displa.ed o eo handlin" outines eenexe!uted#
Fo example we !an use $ail%& meme $un!tion:
Hin!lude iosteam;Hin!lude $steam;usin" namespa!e std>*oid main%*oid&
i$steam input$ile>input$ile#open%test$ileio#dat&>i$%input$ile#$ail%&&
!outThe $ile !ould not e openedJn>
exit%1&> // @ K nomal exit0 non 7eo K some eo ###
O ad%& with !e #i$%input$ile#ad%&& !eUnale to open test$ileio#datn> exit%1&> // @ K nomal exit0 non 7eo K some eo
The $ollowin" examples !eated $o 5in32 empt. !onsole appli!ation without
#de$ %de$inition& and #! %esou!e& $iles# )ll examples in this Module ha*e eentested usin" 'oland ,#xx O4L# )t the end o$ this Module0 thee ae po"am examples !ompiled with C+
+/C++ #4etand"++$o Linux# -eep in mind that in Mi!oso$t 5indows opeatin" s.stem en*ionment thee ae
anothe la.e o$ se!uit. $o a!!essin" 5indows o8e!ts# The in$omation o$ the5indows se!uit. $eatues !an e $ound in 5in32 D- do!umentation# ThisModule will onl. dis!uss $ile input/output in "eneal $om C++ po"ammin"pespe!ti*e#
Fistl.0 !eate a $ile named samplerea$"t*tat oot on C:di*e %o othe lo!ationpo*ided .ou expli!itl. state the $ull path in the po"am 5ite some text as
shown elow in samplerea$"t*t$ile and sa*e it#This is $ile sampleead#txt# This $ile will e opened $o eadin"then its !ontent will e witten to anothe $ile and standad outputi#e s!een/!onsole###a$te .ou ha*e exe!uted this C++ po"am0without eo####this text should e output on .ou s!een as well aswitten to the samplewite#txt $ile# DonNt $o"et to !he! the!ontent o$ the samplewite#txt#
http://www.tenouk.com/Visualc.htmlhttp://www.tenouk.com/Visualc.htmlhttp://www.tenouk.com/Visualcdotnet.htmlhttp://www.tenouk.com/Module000.htmlhttp://www.tenouk.com/Module000.htmlhttp://www.tenouk.com/Visualc.htmlhttp://www.tenouk.com/Visualc.htmlhttp://www.tenouk.com/Visualcdotnet.htmlhttp://www.tenouk.com/Module000.html8/13/2019 Exercise 10 n Structures
11/45
sampleead#txt $ile !ontent
// eadin" $om a*ailale $ile !ontent// then witin" the !ontent to anothe// $ile# Fistl.0 !eate $ile $o eadin" %!an in!lude path&
// let sa.s C:sampleead#txt0 at oot on C di*e#// T.pe some text as shown0 then exe!utes this po"am#Hin!lude iosteam;Hin!lude $steam;usin" namespa!e std>
// $un!tion de$inition0 to open $ile $o eadin"###*oid openin$ile%i$steam in$ile& !ha $ilenameP1@@Q> !outEnte the $ile name: >
// Ente the $ilename that .ou ha*e !eated // %!an in!lude path Fom the !omment ao*e // .ou ha*e to ente C:sampleead#txt without the doule Ruotes#
!in;;$ilename> in$ile#open%$ilename&>*oid main%*oid& // de!lae the input $ile steam
i$steam input$ile> // de!lae the output $ile steam o$steam output$ile>
!ha !hs>
// $un!tion !all $o openin" $ile $o eadin"### openin$ile%input$ile&> // !eate0 i$ not exist and open it $o witin" output$ile#open%C:samplewite#txt&>
// test until the end o$ $ile while %Jinput$ile#eo$%&& // ead !haa!te until end o$ $ile input$ile#"et%!hs&> i$ %Jinput$ile#eo$%&& // output !haa!te . !haa!te %.te& on s!een0 standad output
8/13/2019 Exercise 10 n Structures
12/45
!out!hs> // wite to output $ile0 samplewite#txt output$ile!hs>
!outn?eadin" and witin" $ile is !ompletedJendl> // !lose the input $ile steam input$ile#!lose%&> // !lose the output $ile steam output$ile#!lose%&>
Output:
In this po"am we do not po*ide eo handlin"s0 the existin" $ile to e openedis not *ei$ied#
)nothe example usin" "etline%& meme $un!tion# Fistl. !eate text $ile0 namedrea$file"t*t0 put it on di*e C:5indows0 then t.pe the $ollowin" sample text and
sa*e it#This is ead$ile#txt# Sust sample text0 openin"$o eadin" . usin" "etline%& meme $un!tion#Thee ae $ou lines o$ text to e ead $om#This is 8ust plain simple eadin" text $om a $ile#
// usin" "etline%& meme $un!tionHin!lude iosteam;Hin!lude $steam;usin" namespa!e std>
*oid main%*oid& !ha $ilenameP,@Q> i$steam input$ile> !ha FistLineP,@Q> !ha e!ondLineP,@Q> !ha ThidLineP,@Q>
8/13/2019 Exercise 10 n Structures
13/45
// pompt use $o $ile name to e opened### !outEnte the $ilename to e opened: > !in;;$ilename>
// test open $ile $o eadin"###
input$ile#open%$ilename&> // i$ not the end o$ $ile0 do### i$%Jinput$ile#eo$%&& !outnThe $ist line o$ text is: n> input$ile#"etline%FistLine0 ,@&> !outFistLineNnN> !outThe se!ond line o$ text is: n> input$ile#"etline%e!ondLine0 ,@&> !oute!ondLineendl> !outThe thid line o$ text is: n>
input$ile#"etline%ThidLine0 ,@&> !outThidLineendl>
Output:
)nothe po"am example with a simple ex!eption handlin"#// a simple $ile ex!eption handlin"< when openin" $ile $o eadin"#
// Thee is no test$ileio#txt at the oot o$ di*e C at the e"innin"#Hin!lude iosteam;Hin!lude $steam;usin" namespa!e std>*oid main%*oid&
!ha $ilenameP Q C:test$ileio#txt> i$steam input$ile>
input$ile#open%$ilename0 ios::in&> // test i$ $ail to open $ail $o eadin"0 doV
8/13/2019 Exercise 10 n Structures
14/45
i$%input$ile#$ail%&& !outOpenin" $ilename $ile $o eadin"n> !outAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn> !outThe $ilename $ile !ould not e openedJn>
!outGossile eos:n> !out1# The $ile does not exist#n> !out2# The path was not $ound#n> s.stem%pause&> exit%1&> // 8ust exit // @Anomal0 non 7eo A some eo // i$ su!!ess$ul openin" $ile $o eadin"0 doV else !outThe $ilename $ile was opened su!!ess$ull.Jn>
!outnDo some $ile po!essin" hee###n> input$ile#!lose%&>
// test i$ $ail to !lose the $ile0 doV i$%input$ile#$ail%&& !outnThe $ile $ilename !ould not e !losedJn> s.stem%pause&> exit%1&> // else0 doV else !outnThe $ilename $ile was !losed su!!ess$ull.Jn>
Output:
Then0 !eate $ile named testfileio"t*t on di*e C:# ?eAun the po"am0 the$ollowin" should e output#
8/13/2019 Exercise 10 n Structures
15/45
ame outine !an e use $o $ile output o$steam 0 . epla!in" the i$steamo8e!ts#
The $ollowin" example shows .ou how to pompt use $o the $ile name#// pomptin" use $o $ilename to e opened othes should e
// same as the pe*ious example# Hin!lude iosteam;Hin!lude $steam;usin" namespa!e std>
*oid main%*oid& !ha $ilenameP1@@Q> i$steam input$ile> // pomptin" use $o $ilename to e openedV
// in!ludin" the $ull path i$ ne!essa.V // e#"# !:test$ileio#txt0 !:5indowsTemptest$ile#txt et!
!outEnte the $ile name to e opened: >
// stoe at an aa. $ilename### // aa. without P Q is a pointe to the
// $ist aa. // opened the $ile $o input### input$ile#open%$ilename0 ios::in&> // test i$ $ail to open $ile $o eadin"0 doV i$%input$ile#$ail%&& !outOpenin" $ilename $ile $o eadin"n> !outAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn> !outThe $ilename $ile !ould not e openedJn> !outGossile eos:n>
!out1# The $ile does not exist#n> !out2# The path was not $ound#n> exit%1&> // 8ust exit // @Anomal0 non 7eo A some eo // i$ su!!ess$ul openin" $ile $o eadin"0 doV else
8/13/2019 Exercise 10 n Structures
16/45
!outThe $ilename $ile was opened su!!ess$ull.Jn> !outnDo some $ile po!essin" hee###n> // !lose $ile $o inputV input$ile#!lose%&>
// test i$ $ail to !lose the $ile0 doV i$%input$ile#$ail%&& !outnThe $ile $ilename !ould not e !losedJn> exit%1&> // else0 doV else !outnThe $ilename $ile was !losed su!!ess$ull.Jn>// tested usin" the win32 !onsole mode########
// po*ided the $ile test$ileio#txt exists on the C: di*eVOutput:
!"2 e,uential -ile .rocessing ?eadin" data and do some !al!ulation0 then displa. the data# Fistl.0 !eate a
$ile named testfileio"t*ton di*e C:# -e. in some data in this test $ile as shownelow and sa*e the $ile#
1@@#23 ,W#33 WX#12 Y9#1@ ,,#,23#12 ,W#11 3#2 W,#32 ,#@@
Ceate and un the $ollowin" po"am#// a simple po!essin" data $om extenal $ile#// ead the data in seRuential mode0 do some
// !al!ulation and displa. to the standad output#// Ceate $ile test$ileio1#txt on di*e C0 and t.pe some data as shownHin!lude iosteam;Hin!lude $steam;usin" namespa!e std>*oid main%*oid&
8/13/2019 Exercise 10 n Structures
17/45
!ha $ilenamePQ C:test$ileio1#txt> i$steam input$ile>
// openin" input $ile $o eadin" input$ile#open%$ilename0 ios::in&>
// test i$ $ail to open the $ile0 doV // eo handlin" $o $ile openin" i$%input$ile#$ail%&& !outOpenin" $ile $ilename $o eadin"n> !outAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn> !outThe $ile !ould not e openedJn> !outGossile eos:n> !out1# The $ile does not exist#n> !out2# The path was not $ound#n> s.stem%pause&>
exit%1&> // 8ust exit // @Anomal0 non 7eo A some eo // i$ su!!ess$ul0 do the $ollowin"### else !outThe $ilename $ile was opened su!!ess$ull.Jn> // de!lae some *aiales $o simple !al!ulation $loat pi!e0 total @> int !ount @>
!out?eadin" data and do some !al!ulationnn> // ead data $om input steam### input$ile;;pi!e> // test0 i$ end o$ $ile not $ound0 do the $ollowin"### while%Jinput$ile#eo$%&& // total total + pi!e total + pi!e> !ount++> !outItem pi!e H !ount is pi!eendl> // eAead the next item pi!e within the loop input$ile;;pi!e> !outThe total pi!e $o !ount items is: totalendl> !outnAAAAAAADO4EAAAAAAAnendl> // !lose the input $ile input$ile#!lose%&>
// test !losin" $ile0 i$ $ail to !lose the $ile0 do### // eo handlin" $o $ile !losin"
8/13/2019 Exercise 10 n Structures
18/45
i$%input$ile#$ail%&& !outThe $ilename $ile !ould not e !losedJn> // somethin" won"0 8ust exit### exit%1&>
// i$ su!!ess$ul !lose the $ile0 do#### else !outThe $ilename $ile was !losed su!!ess$ull.Jn>
Output:
4ow let t. usin" the osteam !lass o8e!t# This will !eate and open a $ile $owitin"# The $ile will e !eated i$ it does not exist .et#// a simple po!essin" data $om extenal $ile#// Ceatin"0 openin" and witin" some data in $ile// and appendin" data at the end o$ $ile###Hin!lude iosteam;Hin!lude $steam;usin" namespa!e std>*oid main%*oid& !ha $ilenameP Q C:test$ileio2#txt> o$steam output$ile>
// !eatin"0 openin" and witin"/appendin" data to $ile output$ile#open%$ilename0 ios::outios::app&> // simple eo handlin" $o $ile !eatin"/openin" $o witin" // test i$ $ail to open the $ile0 doV
8/13/2019 Exercise 10 n Structures
19/45
i$%output$ile#$ail%&& !outCeatin" and openin" $ile $ilename $o witin"n> !outAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn> !outThe $ilename $ile !ould not e !eated/openedJn>
!outGossile eos:n> !out1# The $ile does not exist#n> !out2# The path was not $ound#n> exit%1&> // 8ust exit // @Anomal0 non 7eo A some eo// else0 i$ the $ile !an e opened0 doVelse !outThe $ilename $ile was !eated and opened su!!ess$ull.Jn> !outnDo some $ile witin"####nn>
output$ile5itin" some data in this $ilen> output$ileAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn> !outChe! the $ilename $ile !ontents :A&endl> !outI$ the $ile alead. ha*e had data0 the new data will e appendedn>
int sampledata> // wite some inte"es to the $ile### $o%sampledata@> sampledata1@> sampledata++& output$ilesampledata > output$ileendl> // !lose the output $ile output$ile#!lose%&> // test i$ $ail to !lose the $ile0 do the $ollowin"### // simple eo handlin" $o output $iles !losin" i$%output$ile#$ail%&& !outThe $ilename $ile !ould not e !losedJn> exit%1&> // test i$ su!!ess$ul to !lose the $ile0 do the $ollowin"### else !outnThe $ilename $ile was !losed su!!ess$ull.Jn>
Output:
8/13/2019 Exercise 10 n Structures
20/45
The !ontent o$ the testfileio2"t*t is as $ollows:5itin" some data in this $ileAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@ 1 2 3 , W X Y 9 1@
5hen .ou e un this po"am se!ond times0 the data will e appended at theend o$ the pe*ious data#
5itin" some data in this $ile
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@ 1 2 3 , W X Y 9 1@5itin" some data in this $ileAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@ 1 2 3 , W X Y 9 1@
n an )a. wosheet we had $ou "ame s!oe $om $ou di$$eent pla.es0 whi!h we eadand pinted in e*ese ode# Instead o$ de$inin" !oe10 !oe20 !oe3 and !oe0 wede$ined one aa. that held all $ou s!oes0 that is !oePQ# 5e "ouped them in an aa.e!ause all items wee o$ the same $ata typeand had a simila meanin"0 the. wee all"ame s!oes#
Bowe*e what aout the "oup o$ data items that ae elated ut ha*e $ifferent $atatypesZ Fo example0 suppose that alon" with the "ame s!oes0 we want to stoe thename o$ ea!h pla.e0 the !ount. $om whi!h the. !ome and thei a"es# Bee0 the s!oeis a $loat0 the pla.e and the !ount. ae !haa!te stin"s and the a"e is an inte"e# 5ewould lie to stoe these $ou items to"ethe as one unit and to handle and po!ess themto"ethe as a "oup# In ode to do this we !an use structure $ata type# The e.woduse to de$ine this data t.pe is struct# Fo ou needs0 we !an de$ine the stu!tue asshown elow#
8/13/2019 Exercise 10 n Structures
21/45
stu!tGati!ipant !ha4ameP2@Q> !haCount.P2,Q>
$loat!oe> int)"e>>
stu!tis a e.wod and the Gati!ipantis a name o ta" that we po*ided# )ll that we aedoin" is de$inin" a stu!tue !alled stu!t Gati!ipant# 5e ae not !eatin" a *aialeinstead we ae !eatin" a ne $ata typethat in!ludes o a""e"ate othe C and C++data t.pes# ou !an thin o$ this as a stu!tue template $om whi!h stu!tue *aialesma. e de$ined# )n. *aiale de!laed as ein" o$ this t.pe will ha*e these $ou patsthat ae !alled mem)ers of the structure#Late in the C++wosheet o$ this seies we will add moe $eatues to stu!tue
templates and !all them classes# 0aria)lesde$ined as !lasses will e !alled o)1ects#Sust as *aiales ae !eated $om data t.pes o stu!tue *aiales ae !eated $omstu!tue de$initions0 o)1ects are create$ from classes#5ith aa.s0 we need to spe!i$. the sus!ipt o the index to a!!ess one item inside theaa.# To a!!ess one item in a stu!tue0 we need to spe!i$. the name of the mem)er#The ode in whi!h the stu!tue memes ae de$ined is not impotant# The $ollowin"!ode shows how stu!tue *aiales ae de$ined and initiali7ed#
stu!tGati!ipant Gla.e10 Gla.e2 -oea0 -en.a0 #W0 19>The $ollowin" Fi"ues t. to show the de$inin" two stu!tue *aiales#
8/13/2019 Exercise 10 n Structures
22/45
Gla.e1is de$ined and has $ou memes# Gla.e2is also de$ined and its $ou memesae initiali7ed# Bee0 the ode is impotant# The. should e initiali7ed in the same odethat the stu!tue memes ae de$ined as shown in the ao*e Fi"ue# In dataase
teminolo".0 a meme o$ a stu!tue *aiale is !alled a fiel$0 a stu!tue *aiale withits data is !alled a $ata)ase recor$and a !olle!tion o$ elated e!ods is !alled a file#5ith aa.s0 we spe!i$. the sus!ipt o an index en!losed in a!ets to a!!ess oneelement# 5ith stu!tues0 we must $ollow the *aiale name . a perio$0 $ollowed . thememe name to a!!ess one meme# Fo example0 Gla.e2#!oeis eRual to #W@#The $ollowin" !ode:st!p.%Gla.e1#Count.0 Gla.e2#Count.&>
will !op. the !ount. %*alue& o$ Gla.e2as the !ount. o$ Gla.e1# )dditionall.0 whileGla.e2#Count.is eRual to the stin" [-en.a\0 Gla.e2#Count.P3Qis eRual to the!haa!te .
8/13/2019 Exercise 10 n Structures
23/45
// s!an$%]s0 Gla.eP@Q#4ame&>s!an$^s%]s0 Gla.eP@Q#4ame0 2@&>
The is optional hee e!ause as .ou ha*e leaned pe*iousl.0 aa. names ae a!tual
addesses an.wa.# 5e !an also !eate a stu!tue ased on othe stu!tues# Considethe the $ollowin" !ode example#
stu!tTeam // de$inin" a new stu!tue stu!tGati!ipant Captain> // stu!t Team !ontains a stu!tue int5ins> intLosses> stu!tGati!ipant Gla.eP2Q> // stu!t Team has an aa. o$ stu!tues>
// de$inin" and initiali7in" a *aialestu!tTeam weetThin"s Sasmine0 Camodia0 3#,0 220 ,0 20 Mi!hael0 Mexi!o0 ,#20 2@0 Leonad0 6eman.0 #@0 2 // need the oute a!es>// 8ust de$inin" a *aialestu!tTeam ?owd.'aies>
In the $ollowin" Fi"ue0 .ou !an see what the *aiale weetThin"sloos lie0 astu!tue o$ stu!tues#
8/13/2019 Exercise 10 n Structures
24/45
I$ we want to !op. the !ontents o$ weetThin"sto ?owd.'aies0 all we need to do is:
?owd.'aies weetThin"s>
Bowe*e0 $o the sae o$ otainin" expeien!e in handlin" stu!tues0 let us do the samethin" the lon" wa.#
// !op. the Captain *aluesst!p.%?owd.'aies#Captain#4ame0 weetThin"s#Captain#4ame&>st!p.%?owd.'aies#Captain#Count.0 weetThin"s#Captain#Count.&>
?owd.'aies#Captain#)"e weetThin"s#Captain#)"e>?owd.'aies#Captain#!oe weetThin"s#Captain#!oe>
// !op. the Gla.e *alues$o%i @> i 1> ++i& st!p.%?owd.'aies#Gla.ePiQ#4ame0 weetThin"s#Gla.ePiQ#4ame&> st!p.%?owd.'aies#Gla.ePiQ# Count.0 weetThin"s#Gla.ePiQ#Count.&>
8/13/2019 Exercise 10 n Structures
25/45
?owd.'aies#Gla.ePiQ#)"e weetThin"s#Gla.ePiQ#)"e> ?owd.'aies#Gla.ePiQ#!oe weetThin"s#Gla.ePiQ#!oe>
// Cop. the 5ins and Losses?owd.'aies#5ins weetThin"s#5ins>?owd.'aies#Losses weetThin"s#Losses>
ou !an see that0 althou"h the $ist solution is onl. one statement0 the expandedsolution illustates how to handle indi*idual memes o$ stu!tues#4ow let us tae a simple stu!tue and use it to illustate how stu!tues ae used with$un!tions# 5e will do this . witin" the entie po"am# In the $ollowin" po"amexample0 we de$ine stu!t Moile# It is simpl. made up o$ two memes: one is !alledMaeP Qand the othe is ea# The stu!tue is de$ined outside an. $un!tion0 so it isa*ailale "loall. to all $un!tions#
main%& de$ines Ca1 and Ca2 o$ t.pe stu!t Moile# Then main%&!alls the $un!tionFindea%&. passin" one !haa!te stin" with a *alue o$ [Fod\# Exe!ution then "oesto that $un!tion#In Findea%&0 the !haa!te stin" is !alled 4ameP Q# ) lo!al *aiale o$ t.pe stu!tMoileis de$ined with the name o$ Ca# Findea%&ass $o the .ea o$ the !a "i*en .4ameP Qand eads in the .ea meme o$ Ca# The mae meme o$ Cais assi"nedthe stin" in 4ameP Q# Then Findea%&etuns the name and the .ea o$ the !a as t.pestu!t Moile#This stu!tue is assi"ned to Ca1in main% imilal.0 the stin" [Mustan"\ is passed toFindea%&0 whi!h etuns that stin" and its .ea a! to main%&as a stu!t Moile# Thisnew stu!tue is assi"ned to Ca2#4ow main%&!alls the $un!tion GintOldest% Instead o$ passin" a stin"0 main%&passestwo stu!tues# )lso0 instead o$ assi"nin" the $un!tion to a *aiale lie Ca1and Ca20main%&doesn
8/13/2019 Exercise 10 n Structures
26/45
// $un!tion potot.pe withstu!tue etun t.pestu!tMoile Findea%!ha4ameP Q&>
// $un!tion potot.pe withstu!tues a"umentspassed*oidGintOldest%stu!tMoile )uto10 stu!tMoile
)uto2&>*oidmain%*oid& stu!tMoile Ca10Ca2>
Ca1 Findea%To.ota&> Ca2 Findea%Mustan"&> GintOldest%Ca10 Ca2&>// this $un!tion e!ei*es onestin" that is the mae o$ a!a0
// eads in its .ea andetuns them oth as stu!tMoilestu!tMoile Findea%!ha4ameP Q& stu!tMoile Ca> pint$%5hat .ea is the]sZ 0 4ame&> // s!an$%]d0Ca#ea&> s!an$^s%]d0Ca#ea0 1&> pint$%Ca#ea ]dn0 Ca#ea&> // st!p.%Ca#Mae04ame&> st!p.^s%Ca#Mae0si7eo$%Ca#Mae&0 4ame&>
8/13/2019 Exercise 10 n Structures
27/45
etunCa>// this $un!tion e!ei*es twostu!tues o$ t.pe stu!t
Moile// and pints the .ea o$ theoldest !a# It etunsnothin"#*oidGintOldest%stu!tMoile )uto10 stu!tMoile
)uto2& i$%)uto1#ea
)uto2#ea& pint$%The oldest
!a is the ]sn0)uto1#Mae&> elsei$%)uto1#ea ;
)uto2#ea& pint$%The oldest!a is the ]sn0
)uto2#Mae&> else pint$%The. ae oththe same a"eJn&>
struct 'ata Type .ractices
1# Let us intodu!e two new$un!tions intended $ostin"s# ou ma. ente[ou
puts%4ame .ou $a*oiteson" title: &> // "ets%a&>
a# Onl. the $ist wod## The entie stin"#!# The $ist etun that was enteed#d# es it did#
e# O$ !ouse "ets%&/"ets^s%
8/13/2019 Exercise 10 n Structures
28/45
"ets^s%a0 2,&> puts%n4ame anothe son"title:&> // s!an$%]s0 &> s!an$^s%]s0 0 2,&>
pint$%na ]s0 ]sn0 a0&>
a# 5as the s!an$^s%&ale to ead theentie stin" o onl.the $ist wodZ
# 5as the "ets^s%&ale to ead theentie stin" o onl.the $ist wodZ
!# Did the "ets^s%& stopeadin" at the $istspa!e o the $istetun that wasenteedZ
d# Did the puts%& add an< at the end o$ thestin" that is pintedZ
e# 5hi!h $un!tion would.ou pe$e to ead ina stin"0 s!an$^s%& o
"ets^s%&Z
2# Fistl.0 ente [Ooowee'ae\ and then 2#@ $o the$ollowin" po"am# howthe output and answe theRuestions#
Hin!lude stdio#h;*oid main%&
// de$inition o$ stu!tue stu!t on" !ha 4ameP2,Q> $loat Len"th> > // end o$ de$inition
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
a# !ha and $loat## 4ame and Len"th#!# The. allo!ate spa!e in memo.#d# es it !eates in itsel$ a new *aiale#e# es#
8/13/2019 Exercise 10 n Structures
29/45
// *aiale de!laation
stu!t on" Title1>
pint$%The si7e o$ Title1
stu!tue *aiale is ]d.tes#n0 si7eo$%Title1&&> puts%4ame .ou $a*oiteson" title: &> "ets^s%Title1#4ame0 2,&> puts%Bow lon" is itZ&> s!an$^s%]$0Title1#Len"th&> pint$%nou son" is &> puts%Title1#4ame&> pint$%)nd it is ]#2$ min#
lon"#n0 Title1#Len"th&>
Sust as the int and $loat ae datat.pes0 stu!t on" is a new datat.pe that we ha*e de$ined#
a# 5hat ae the two nownt.pes used to de$ine stu!ton"Z
# stu!t on" is de$ined withtwo memes# 5hat aethei namesZ
!# Do the wods !ha o $loat. themsel*es !eate new*aiales o allo!ate spa!ein memo.Z
d# in!e stu!t on" is also a
$# Title1#"# It has two pats named name and len"th#
h# ###i# 4o# stu!tue !an ha*e di$$eent data t.pes#
8# 5e use a dot opeato and the stu!tueNsmeme name#
# Title1#Len"th @>l# Title1#4ame [M# Moonli"htin"\#
8/13/2019 Exercise 10 n Structures
30/45
new data t.pe0 do .ou thinthat it !eates in itsel$ a new*aialeZ
e# Is a semi!olon used at theend o$ the stu!tue
de$initionZ$# in!e the de$inition o$ thestu!tue doesn#5hen we want to a!!ess ameme o$ a stu!tue0 whatdo we useZ
# Bow would .ou ha*eassi"ned the Len"thmeme o$ Title1 to @Z
l# Bow would .ou ha*eassi"ned the name memeo$ Title1 to [M#Moonli"htin"\Z
8/13/2019 Exercise 10 n Structures
31/45
3# In the $ollowin" po"amexample ente [?ides onthe tom\ and 3#1@ $o thesample input data# howthe output and answe the
Ruestions#Hin!lude stdio#h;Hin!lude stin"#h;*oid main%& stu!t on" !ha 4ameP2,Q> $loat Len"th>
>stu!t on" Title10 Title2>
st!p.^s%Title2#4ame0 2,0
M. Teadops&> Title2#Len"th 2#3,$> puts%4ame .ou $a*oiteson":&> "ets^s%Title1#4ame0 2,&> puts%Bow lon" is itZ&>
s!an$^s%]$0Title1#Len"th&> pint$%nM. son" is ]snou son" is 0 Title2#4ame&> puts%Title1#4ame&> pint$%ous is ]#2$ min#lon"e n0 Title1#Len"th ATitle2#Len"th&>
a# Can .ou pint outoth memes o$Title2 withoutspe!i$.in" thememe names asshown elowZ Doesthis woZ
pint$%]s ]#2$n0 Title2&>
# Can .ou assi"nTitle2 to Title1
a# 4o0 we !anNt and this doesnNt wo# 5e needthe name o$ the meme#
# es we !an and it does wo po*ided thatthe. oth ha*e same stu!tue as in thisexample# Bee we assi"n the whole stu!tue#
!# It doesnNt wo# 5e !annot assi"n the *alue o$Title2#4ame die!tl. to Title1#4ame ut we!an !op. the *alue as the $ollowin" !ode:
st!p.^s%Title1#4ame0 2,0 Title2#4ame&>
8/13/2019 Exercise 10 n Structures
32/45
without spe!i$.in"thei memes asshown elowZ Doesthis woZ
Title1 Title2>
!# Does the $ollowin"!ode woZ 5h. owh. notZ
Title1#4ame Title2#4ame
# how the output and answethe Ruestions $o the $ollowin"po"am#
Hin!ludestdio#h;*oidmain%& stu!ton" !ha4ameP2,Q> $loatLen"th> >
stu!ton" Title1 Ma*elous 6a!e02#,@0 Title2 I uende )ll03#@0 Title3>
Title3 Title1> Title1 Title2> Title2 Title3>
pint$%1: Title A ]s0 Len"th A]#2$n0 Title1#4ame0Title1#Len"th&> pint$%2: Title A ]s0 Len"th A]#2$n0 Title2#4ame0Title2#Len"th&>
a# 5hen initiali7in"
a# 5e need to po*ide the *alue in ode0simila to the de!laation#
# 4o we !anNt#!# Thee# That ae Title10 Title2 and Title3#d# The oi"inal *alue o$ Title1 was
o*ewitten . Title2 and the oi"inal *alueo$ Title2 was o*ewitten . Title3 whi!hwas !opied $om Title1#
8/13/2019 Exercise 10 n Structures
33/45
stu!tues0 do .ou ha*eto po*ide the *alue o$the $ist meme $ist o!an it e pla!ed se!ondZ
# Can .ou lea*e out the
*alue o$ one o$ thememes as shown inthe $ollowin" !odeZ Doesthis woZ
stu!t on" Title1 0 2#,@0 Title2 I uende )ll0 0 Title3>
!# Bow man. *aiales o$t.pe stu!t on" wee!eatedZ
d# 5hat was done to theoi"inal *alues o$ Title1
and Title2Z
,# how the output and answethe Ruestions $o the $ollowin"po"am#
Hin!ludestdio#h;Hin!ludestin"#h;// stat o$ stu!tue de$initionstu!ton" !ha4ameP2,Q> $loatLen"th>> // end o$ stu!tue de$inition// $un!tion potot.pe0 e!ei*es andetuns stu!tue data t.pestu!ton" _eoOut%stu!ton"&>*oidmain%& stu!ton" Title1 5hiteoom0 2#,@0 Title2> Title2 _eoOut%Title1&> pint$%1: ]s0 ]#2$n0Title1#4ame0 Title1#Len"th&> pint$%2: ]s0 ]#2$n0Title2#4ame0 Title2#Len"th&>
a# The po"am wonNt wo e!ause thestu!tue was used in the $un!tionpotot.pe# 5e need to de!lae it e$oeusin" it#
# Title1 e!omes x in _eoOut%!# 4o#d# 4o# The $un!tion !annot !han"e its
!ontents# In the $un!tion we t. to assi"nan empt. stin" to x#4ame and @ to thex#Len"th# Bowe*e0 when we etun tomain%& the oi"inal *alue still inta!t#
e# es $un!tion maes !op. o$ stu!tue liea s!ala#
$# 4o0 it is lie a s!ala#"# stu!t on" t.pe#
h# . *aiale o$ t.pe stu!t on"#i# Title2 *aiale o$ t.pe stu!t on"#8# es0 those *alues e!ei*ed in main%& and
assi"ned to Title2 o$ t.pe stu!t on"#
8/13/2019 Exercise 10 n Structures
34/45
8/13/2019 Exercise 10 n Structures
35/45
$# Do $un!tions e!ei*e onl.addesses o$ stu!tues0as the. do o$ aa.sZ
"# Let us now !onside how$un!tions etun
stu!tues# 5hat is thedata t.pe that _eoOut%&etunsZ
h# 5hat *aiale is etuned. _eoOut%&Z 5hat is itsdata t.peZ 5hat ae its*aluesZ
i# 5hi!h *aiales in main%&a!!epts the etunedstu!tue $om_eoOut%&Z
8# 5ee the *alues etuned. _eoOut%& e!ei*ed inmain%&Z I$ so0 then intowhi!h *aialeZ
W# Bow aout an aa. o$stu!tue# how the output andanswe the Ruestions $o the$ollowin" po"am#
Hin!ludestdio#h;
Hin!ludestin"#h;stu!ton" !ha4ameP2,Q> $loatLen"th>>*oidmain%& stu!ton" TitleP3Q
Lone Gaiie0 2#,@F0 Me. Chistmas0 3#3,F>
inti>
puts%Ente a new son":&> "ets^s%TitleP2Q#4ame0 2,&> puts%Ente its time:&>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAA
8/13/2019 Exercise 10 n Structures
36/45
s!an$^s%]$0 TitleP2Q#Len"th0&>
$o%i @> i 2> ++i& pint$%]d: ]s0 ]#2$n0 i + 10
TitlePiQ#4ame0 TitlePiQ#Len"th&>a# ?ememe that indexin"
stats at @ and ente theinitiali7ed *alues o$ theaa. in the dia"am#
# how the *alues in the$ollowin" dia"am that.ou po*ided in the aa.o$ stu!tues#
!# 5hat is the name o$ theaa.Z Bow man. slotsdoes it ha*eZ
d# 5hat is the name o$ thestu!tueZ 5hat ae thenames o$ its memesZ
e# 5h. is the indexpo*ided next to theaa. name and not thememe name0 in all theinstan!es o$ thisexe!iseZ 5h. the$ollowin" isn
8/13/2019 Exercise 10 n Structures
37/45
answes $om: memename0 aa. name0index#
"# )n aa. slot is spe!i$ied. usin" a!ets o a
peiodZh# ) stu!tue meme isspe!i$ied . usin"a!ets o a peiodZ
X# 4ext0 let us ha*e an aa. o$names within a stu!tue# Ente[atis$ied Mind\0 ['ill.\0 [Sill.\and [ill.\ $o the sample inputdata# how the output andanswe the Ruestions#
Hin!lude stdio#h;stu!t on" !ha 4ameP2,Q> !ha in"eP3QP2,Q> $loat Len"th>>*oid main%*oid&
int i> stu!t on" Title1 ee and desto.0 ?un $o.ou li$e0 'eauti$ul Sasmine0 Mae m.da.0 2#,@0 Title2>
puts%Ente a new son":&> "ets^s%Title2#4ame0 2,&> puts%Ente its 3 sin"es&>
s!an$^s%]s0 Title2#in"eP@Q02,&> s!an$^s%]s0 Title2#in"eP1Q02,&> s!an$^s%]s0 Title2#in"eP2Q02,&> pint$%]st]sn0 Title1#4ame0Title2#4ame&>
a# ) usual0 it is ased on the aa.Ns index#Index @ will stoe stin" 1 as inTitle2#in"eP@Q0 index 1 will stoe stin" 2as in Title2#in"eP1Q and index 2 will stoestin" 3 as in Title2#in"eP2Q#
8/13/2019 Exercise 10 n Structures
38/45
pint$%n&> $o%i @> i 2> ++i& pint$%]s0 ]sn0Title1#in"ePiQ0 Title2#in"ePiQ&>
a# o $a ou stu!tue hashad one meme thatwas an aa.# Bowe*e0we teated it as a sin"leunit o a stin"# Bee0 weha*e a 3Aslot aa. o$stin"s that hold thenames o$ sin"es# 5heninitiali7in"0 how do wenow whi!h stin" "oesinto whi!h slot o$ the
stu!tueZ# how the !ontents o$ alllo!ations in the $ollowin"Fi"ue#
!# 5hen eadin" in the namememe o$ Title20 wh. is noaa. index "i*enZ
# ###!# The name stin" is !onsideed as one
element# The aa. has een used to stoethe indi*idual !haa!te and the index o$the aa. is one dimensional#
d# In this !ase we ha*e thee stin"s to eead and stoed into thee di$$eent slotsand the aa.s ae two dimensional#
e# 4ot spe!i$ied e!ause the son" namesha*e een !onsideed one element andthe index is one dimensional#
$# The aa. indexes ae spe!i$ied e!ausee*e. sin"e name has een !onsideed adi$$eent stin"#
"# 'ased on the dimensional o$ the index# I$ itis two dimensional0 then we need to
spe!i$. an index#h# In exe!ise HW0 the stu!tue meme and
the stu!tue *aiale spe!i$ied with anindex and this is a stu!tue that !ontainsan aa. element and then de!laed as anaa. *aiale#
i# The use o$ the sRuae a!ets %P Qhi!h one o oth that ha*e the a!ets
8/13/2019 Exercise 10 n Structures
39/45
d# 5hen eadin" in the sin"es $oTitle20 wh. is an indexspe!i$iedZ
e# 5hen pintin" the son" names0ae aa. indexes spe!i$iedZ
5h. o wh. notZ$# 5hen pintin" out the sin"es0ae aa. indexes spe!i$iedZ5h. and wh. notZ
"# '. looin" at the stu!tuede$inition0 how !an .ou tellwhi!h meme needs an indexspe!i$iedZ
h# In exe!ise HW0 was the *aialename o the meme namespe!i$ied with an indexZ
i# '. looin" at the stu!tueand/o *aiale de$initions0 how!an .ou tell i$ the *aiale nameo the stu!tue meme name"ets an indexZ
8# In this exe!ise0 was the*aiale name o the memename spe!i$ied with an indexZBow !an .ou tell whi!h oneshould eZ
# how how .ou would ha*e to
!han"e the de$inition o$ thestu!tue and/o the *aiale sothat oth indexes would ha*e toe "i*en when a!!essin" asin"e#
"et an index#
8# In this exe!ise the stu!tue memenames spe!i$ied with an index as !ha4ameP2,Q> and !ha in"eP3QP2,Q>
# 5e need to de!lae the stu!tue memeand the stu!tue *aiales as an aa.t.pe as shown elow#
stu!t on" !ha 4ameP2,Q> !ha in"eP3QP2,Q> $loat Len"th>
>###stu!t on" Title3P3Q>###Title3P@Q#in"eP@Q Ma. da.>
&ore tructure uestionsFo the $ollowin" Ruestions0 use thede$initions o$ the "i*en stu!tue#
stu!t Date int ..0 mm0 dd>>stu!t Emp
8/13/2019 Exercise 10 n Structures
40/45
!ha Emp4ameP2,Q> $loat ala.> stu!t Date hied>>
stu!t Dep stu!t Emp mana"e> stu!t Emp woeP2,Q> $loat Go$its>>
1# De$ine a stu!t Date *aiale!alled Date1 and initiali7e it toFeua. 2,0 19,X0 in the
!oe!t $omat#
stu!t Date int ..0 mm0 dd>
>*oid main%*oid& stu!t Date Date1>
Date1#dd 2,> Date1#mm 2> Date1#.. 19,X>
pint$%The date ]d0 ]d0 ]dn0 Date1#mm0
Date1#dd0 Date1#..&>
2# De$ine a stu!t Emp *aiale!alled Geson1 and initiali7e itto [?o"e\0 with a sala. o$`,@0@@@0 who was hied onMa!h 1@0 2@@1#
Hin!lude stdio#h;stu!t Date int ..0 mm0 dd>
>stu!t Emp !ha Emp4ameP2,Q> $loat ala.> stu!t Date hied>>
8/13/2019 Exercise 10 n Structures
41/45
*oid main%*oid& stu!t Emp Geson1 ?o"e>
Geson1#ala. ,@@@@> Geson1#hied#mm 3> Geson1#hied#dd 1@> Geson1#hied#.. 2@@1>
pint$%Bied Date ]d0 ]d0 ]dn0Geson1#hied#mm0 Geson1#hied#dd0Geson1#hied#..&> pint$%Emplo.ee 4ame ]sn0Geson1#Emp4ame&> pint$%ala. `]#2$n0 Geson1#ala.&>
3# De$ine a stu!t Dep *aiale!alled To.s whose mana"e is[?o"e\0 $om Ruestion 2 ao*eand the po$its ae `Y@0@@@#This depatment has onl. two
emplo.ees as shown elow:[Moha*e\ with a sala. o$1@0@@@ and the date o$ hieein" )pil 2@0 2@@2[Bunte\ with a sala. o$Y0@@@ and the date o$ hieein" Sune 120 2@@@
Hin!lude stdio#h;stu!t Date int ..0 mm0 dd>
>stu!t Emp !ha Emp4ameP2,Q> $loat ala.> stu!t Date hied>>stu!t Dep
stu!t Emp mana"e> stu!t Emp woeP2,Q> $loat Go$its>>*oid main%*oid& // note the ode o$ the aan"ement
8/13/2019 Exercise 10 n Structures
42/45
// Emp4ame0ala.0..0mm0dd0Emp4ameP@Q0 ala.0..0mm0dd0 //Emp4ameP1Q0ala.0..0mm0dd0###0Emp4ameP2Q0ala.0..0mm0dd0 //Go$its
stu!t Dep To.s ?o"e0 Moha*e0Bunte>
To.s#Go$its Y@@@@> To.s#woeP@Q#hied#mm > To.s#woeP@Q#hied#dd 2@> To.s#woeP@Q#hied#.. 2@@2> To.s#woeP@Q#ala. 1@@@@>
To.s#woeP1Q#hied#mm W> To.s#woeP1Q#hied#dd 12>
To.s#woeP1Q#hied#.. 2@@@> To.s#woeP1Q#ala. Y@@@>
pint$%]s is a mana"en0To.s#mana"e#Emp4ame&> pint$%Compan. po$it is ]#2$n0 To.s#Go$its&> pint$%Bied Date $o ]s is ]d0 ]d0 ]dn0To.s#woeP@Q#Emp4ame0To.s#woeP@Q#hied#mm 0To.s#woeP@Q#hied#dd [email protected]#woeP@Q#hied#.. 2@@2&>
pint$%ala. `]#2$n0To.s#woeP@Q#ala.&> pint$%Bied Date $o ]s is ]d0 ]d0 ]dn0To.s#woeP1Q#Emp4ame0To.s#woeP1Q#hied#mm W0To.s#woeP1Q#hied#dd 120To.s#woeP1Q#hied#.. 2@@@&> pint$%ala. `]#2$n0To.s#woeP1Q#ala.&>
# Usin" one pint$%&0 pint the!ontent o$ Date1#
ee H1
8/13/2019 Exercise 10 n Structures
43/45
ee H2
,# Usin" two pint$%& stu!t Emp woeP2,Q> $loat Go$its>>
*oid main%*oid& // note the ode o$ the aan"ement // Emp4ame0ala.0..0mm0dd // Let mae it somethin" lie this $o easiness // stu!t Emp 4ames@ ?o"e> // stu!t Emp 4ames1 Moha*e0 1@@@@0 02@0 2@@2> // stu!t Emp 4ames2 Bunte0 Y@@@0W01202@@@> // Then !on*et it to an aa.
stu!t Emp 4amesP3Q ?o"e0 Moha*e01@@@@0 2@@20 0 2@0 Bunte0 Y@@@02@@@0W012> stu!t Dep To.s ?o"e0 Moha*e0Bunte>
GintEmp%4amesP@Q&> pint$%n&>
8/13/2019 Exercise 10 n Structures
44/45
GintEmp%4amesP1Q&> pint$%n&> GintEmp%4amesP2Q&> pint$%n&>
To.s#Go$its Y@@@@> To.s#woeP@Q#hied#mm > To.s#woeP@Q#hied#dd 2@> To.s#woeP@Q#hied#.. 2@@2> To.s#woeP@Q#ala. 1@@@@>
To.s#woeP1Q#hied#mm W> To.s#woeP1Q#hied#dd 12> To.s#woeP1Q#hied#.. 2@@@> To.s#woeP1Q#ala. Y@@@>
pint$%]s is a mana"en0To.s#mana"e#Emp4ame&> pint$%Compan. po$it is ]#2$n0 To.s#Go$its&> pint$%Bied Date $o ]s is ]d0 ]d0 ]dn0To.s#woeP@Q#Emp4ame0To.s#woeP@Q#hied#mm 0To.s#woeP@Q#hied#dd [email protected]#woeP@Q#hied#.. 2@@2&> pint$%ala. `]#2$n0To.s#woeP@Q#ala.&> pint$%Bied Date $o ]s is ]d0 ]d0 ]dn0
To.s#woeP1Q#Emp4ame0To.s#woeP1Q#hied#mm W0To.s#woeP1Q#hied#dd 120To.s#woeP1Q#hied#.. 2@@@&> pint$%ala. `]#2$n0To.s#woeP1Q#ala.&>*oid GintEmp%stu!t Emp 4ames& pint$%Emplo.ee name is ]sn04ames#Emp4ame&> pint$%Date Bied is ]d0 ]d0 ]dn04ames#hied#mm0 4ames#hied#dd04ames#hied#..&> pint$%ala. is `]#2$n0 4ames#ala.&>
8/13/2019 Exercise 10 n Structures
45/45