c++ programing for beginners - lmp.uni-saarland.de filelecture 5 • a collection of a fixed number...
TRANSCRIPT
Lecture5
C++programingforbeginners
9May2019
Lecturer:Dr.AnleWang&Dr.SergeySukhomlinov
�1
E-mail:[email protected]
Lecture5
• Learnaboutarray;1-and2-dimensional• Explorehowtodeclareandmanipulatedataintoarrays;• Discoverhowtopassanarrayasparameterstoafunction;
• Afterthislecture,youshouldbeableto:- Defineandapply1Dand2Darraystocode.
9May2019 �2
ContentsofLecture5
Lecture5
• Acollectionofafixednumberofcomponentswhereinallcomponentshavethesamedatatype;• Declarationruleisthesamewithvariables.• dataTypemeansthedatatypeofdatawhichstoresinthearray;• interExpisanindex,isanyexpressionwhosevalueisanonnegativeinteger;Mustbeconstantinteger!!!• []isthearraysubscriptingoperator;• Thearrayindexalwaysstartsfrom0.
9May2019 �3
Whatisanarray?
Lecture5
• Callarraycomponents:- array[2]=12;- array[2]=array[4]+array[5];- doublevar1=array[8];
• Initializethe1Darray:- inta[10]={0,1,2,3,4,5,6,7,8,9};- inta[10]={0,1,2,3};- inta[5]={1,2,3,4,5};inta[]={1,2,3,4,5};
9May2019 �4
Arraycomponents
0
10
12
3
5
7
8
6
2
array[0]array[1]
array[6]
array[5]array[4]
array[3]
array[2]
array[8]
array[7]
Lecture5
• Findthemaximumandminimumvalueofgivenintegerarray.• 14,17,25,3,65,4,9,0,12
9May2019 �5
1Darrayprogramingexample
Lecture5
• Findthemaximumandminimumvalueofgivenintegerarray.• 14,17,25,3,65,4,9,0,12
9May2019 �6
1Darrayprogramingexample
Lecture5
• Sortingdatainorderwithapplicationofarrays.- bubblesort;- selectsort;- insertsort;- quicksort;- mergesort;
9May2019 �7
Sortingmethod(application)
step1 step2 step3 step4 step5
9 8 8 8 8
8 9 5 5 5
5 5 9 4 4
4 4 4 9 2
2 2 2 2 9
bubblesort
Lecture59May2019 �8
Sortingmethod(application)
step1 step2 step3 step4 step5
9 8 8 8 8
8 9 5 5 5
5 5 9 4 4
4 4 4 9 2
2 2 2 2 9
bubblesort
Lecture515May2018 �9
Sortingmethod(application)
selectsort
a[0] a[1] a[2] a[3] a[4]
3 6 1 9 4 step1
1 6 3 9 4 step2
1 3 6 9 4 step3
1 3 4 9 6 step4
1 3 4 6 9 step5
Lecture515May2018 �10
Sortingmethod(application)
insertsort
a[0] a[1] a[2] a[3] a[4]
3 6 1 9 4 step1
1 3 6 9 4 step2
1 3 4 9 6 step3
1 3 4 6 9 step4
Lecture5
• Acollectionofafixednumberofcomponentswhereinallcomponentshavethesamedatatypeintwodimensions;• interExp1andinterExp2:numbersofrowandcolumninthematrices;
9May2019 �11
Twodimensionalarray
[0] [1] [2]
[0]
[1]
[2]
[3]
num[2][1]
Lecture5
• Initializeallthecomponents:
• Initializepartialcomponents:
• Specialinitialization:
9May2019 �12
Twodimensionalarray
Lecture5
• Rowprocessing:processaparticularrowofthearray.
• Columnprocessing:processaparticularcolumnofarray;
• Wholeprocessing:
15May2018 �13
Twodimensionalarrayexample
Lecture5
• Findthemaximumofamatrixandoutputthevalueandrowandcolumn;• a[3][4]={{5,12,23,56},{19,28,36,46},{31,9,24,6}};
9May2019 �14
Twodimensionalarrayexample
• Thewholearraypassedasparameterstofunction.- Arrayarepassedbyreferenceonly;- Symbol¬usedwhendeclaringanarrayasaformalparameter;- Omitthesizeofarray.- Donotallowthefunctiontoreturnavalueofthetypearray.
Arrayasparametersinfunction
�16Lecture59May2019
• Sortingthenumberswithfollowingalgorithm
• Useafunctionselect_sort(intarray[],intn);Herenisthenumberofcomponents.
Arrayasparametersinfunction(example)
�17Lecture59May2019
a[0] a[1] a[2] a[3] a[4]
3 6 1 9 4
1 6 3 9 4
1 3 6 9 4
1 3 4 9 6
1 3 4 6 9
• Thebaseaddressofanarrayistheaddress,ormemorylocationofthefirstarraycomponent.• Ifarray[10]isa1-dimensionalarray,thebaseaddressistheaddressofarray[0].• Whenpassanarrayasaparameter,thebaseaddressoftheactualarrayispassedtotheformaladdress.
Arrayasparametersinfunction
�20Lecture59May2019
• Inputa2Dmatrix,andtrytoaveragetheelementsofeverycolumnandeveryrow.• Dumpoutthefollowingfigure(10rows):11112113311464115101051…
Exerciseofarrayapplication
�21Lecture59May2019
• Array:structureddatatypewithafixednumberofcomponentswithsamedatatype;• Declarationofarrayandhowtocallacomponentofarray;• Passarrayasparametertofunctions:onlybyreference;• Apairofindextoaccessanelementin2Darray;
Summaryofthislecture
�24Lecture59May2019
• Basicprogramwriting(realcode,pseudocode).• Understandingwhatthecodedoes?• Findthebugsinthecode.• Algorithmdesignandimplement.
• Nextweek(16/05)or23/05?
NextLecture(midTerm)
�25Lecture59May2019