Sei sulla pagina 1di 22

DATASTRUCTURESAND

ALGORITHMS
LAB1
MelaniaNiu
melania.nitu@yahoo.com
FILS,2017
GRADING

Thelabrepresents50%ofthefinalgrade:
30%-3BigHomeworks
20%-Labs(in-classactivity+smallhomeworkfromweektoweek)
Bonusesforexceptionalhw

Conditionsforpassing:
10presencesatthelab(max13presences)
5outof10forhw+labs(ifnot,thelabwillhavetoberedothenext
yearandthestudentswontbeallowedintheexam)
HW+GRADING

The3mainassignmentsandthelabswillbepublishedonMoodle(fils.curs.pub.ro,
sectionCTI(E)-DataStructuresandAlgorithms).

Youwillusethesameplatformtouploadthehomeworks(withinthedeadline)and
youwillhavetopresentthemduringthenextlaboratory.Ifthereisanyproblemwith
theplatform,youcansendthehomeworksbyemail.

Yourcodewillbecheckedusingasoftwareagainstplagiarism.Twoalmostidentical
homeworkswillbenotedwith0points.

Youcanconsultthegradesforthelaboratoryat:
https://docs.google.com/spreadsheets/d/1fBgZ2CUe05fv1fvO766Qu9Vg52oycfjtrS00RNrnP9Q/edit#gid=1
816440752
QUESTIONS

Foranyotherquestionyoucancontactme
viaemail:melania.nitu@yahoo.com

YoucanusetheforumonMoodlewhereyou
canalsofindthecoursesandthe
laboratories.(http://fils.curs.pub.ro)
TOOLS

Code::Blocks(http://www.codeblocks.org/downloads)
C-Free4.0Standard/C-Free5.0Professional

AnyotherIDEorcompilerforC/C++
(ex.GCCunderLinux,C-free)
OBJECTIVES

torunandcompileCprograms
toidentifythestructureofaCprogram
tousestandardI/Ooperations
todefinevariables
todeclareandimplementfunctions
tomakestructures
IDENTIFYTHESTRUCTUREOFATYPICALC
PROGRAM!

ThestructureofaCprogram:
Pre-processingdirectives:stdio.hheaderfilefromtheClibrary,necessaryforusing
theprintfandscanffunctions
Oneormorefunctions(themainfunctionisnecessaryforaCprogramtobe
executed),withconditionalstructures,loopinstructionsetc.

Attention, C is case sensitive!


ACprogramiswritteninafilewiththe.cextension:thesourcecode.
Aftercompilation,anotherfile,withthe.oextensionappears:theobjectcode.
Afterexecution,anotherfile,withthe.exeextensionappears:theexecutable.
STANDARDOUTPUTOPERATIONS

Otherformatspecifiers:
SIGNATURE OF PRINTF FUNCTION

printf(control, par1, par2, , parn);

Where
control=astringwhichdefinesthetextsandtheformatsspecifiers
par1, par2, , parn =expressions;theirvaluesarewrittentakinginto
accounttheformatspecifiersfromcontrol(sameorder)

Exercise:Testtheformatspecifiersofthefunctionprintf.
EXCERCISE

Runthebelowexampleandseehoweachformatspecifierworks

#include<stdio.h>
intmain(void){
printf("%d\n",7);
printf("%3d\n",7);
printf("%03d\n",7);
printf("%3.2f\n",5.1);
printf("%.2f\n",4.245);
printf("%s\n","blue");
return0;
}
STANDARDINPUTOPERATIONS

Scanfhasthesamesignatureasprintfanditisdefinedinstdio.h.
EXERCISE

EX1:Writeaprogramtocalculatetheaveragebetween
twofloatnumbers.Theresultshallbedisplayedwith2
decimals.Usescanfandprintf!

Hint:%.2f->formatspecifierforfloatwith2decimals
FUNCTIONS:DECLARATIONANDIMPLEMENTATION
Signature:
type_of_the_returned_resultfunction_name(list_of_formal_params){
declaration_of_local_variables;
instructions;
}
Visibilitydomain:localvs.globalvariables
Parameterpassing:by-value(thevalueoftheformalparameteris
modifiedonlyinthelocalfunction,thevariablefrommainfunctionisnot
affected);wewilldiscussthepassingby-addressduringthelabwiththe
pointers.
GOLDBACHEXAMPLE

Notetheuseofmath.hlibrary:forsqrt
function(thesamemeaningasinJava)

Notethecontrolflowstructures(if,if-
else,for,)

Notethefunctiondefinitionandcall:
theimplementedfunctioncalculatesif
anumberisprimeornot
EXERCISES

EX2:Checkwhetheranumberisapalindromeornot.
Hint:apalindromeisanumberthatremainsthesamewhenits
digitsarereversed.(e.g.333isapalindrome,123isnotapalindrome)

Ex3:Displaytheminimumfromthethreefloatnumbers.

Ex4:Writeaprogramthatcalculatesthesumofthedigitsforall
integersinaninterval.Thelimitsoftheintervalarereadfromthe
keyboard.
STRUCTURES

auser-defineddatatypethatallowsgroupingofheterogeneouselements
acollectionofoneormorevariables(fields),groupedunderonename
themembersofastructureareaccessedwith.

Format:struct[structuretag,optional]
{members}
[structurealias]
DATEEXAMPLE
EX:UTILISATION

voidwriteDDMMYYYY(datamyDate)
{
printf(%d%d%4d,myDate.day,
myDate.month,myDate.year);
}
EX5: Designastructureforrepresentingdatesandwritefunctionsthat:
-Checkifavariablevalueofthestructureisavaliddate.
-Calculatethenextdateofagivendate(tomorrow).
-Calculatethedatebeforeagivendate(yesterday).

HOMEWORK

1.Writeaprogramthatreadsanumberfromtheconsoleandwritesbackifthe
numberisoddoreven.
2.Writeaprogramthatprintstheoddnumbersupto25.
3.Rarepolynomialswithintegercoefficientsarepolynomialsoflargedegrees
andmanycoefficientsequalto0.Theycanberepresentedbyadatastructure
definedas:
Writefunctionsforwriting,readingandadditionofrare
polynomials.
Ex:isararepolynomial;isamonom.
HOMEWORK

4.Writeaprogramtocalculatethegreatestcommondivisor
(cmmdc)forany2numbersinsertedfromthekeyboard.
REFERENCESFORTHECOURSE

C++ProgrammingLanguage,BjarneStroustroup
ThinkinginC++,byBruceEckel&ChuckAllison
C++PlusDataStructures,byNellDale
LimbajeleCsiC++pentruincepatori(vol1-C,vol2-C++),by
LiviuNegrescu(enroumain)
Tutorialspoint:http://www.tutorialspoint.com/cprogramming/
CProgrammingandC++Programming:http://www.cprogramming.com/

Potrebbero piacerti anche