ceng 230 introduction to c programming - kovankovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf ·...
TRANSCRIPT
![Page 1: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/1.jpg)
CENG 230Introduction to C Programming
Week 12 – Arrays
Sinan Kalkan
Some slides/content are borrowed from Tansel Dokeroglu, NihanKesim Cicekli, and the lecture notes of the textbook by Hanly and
Koffman.
![Page 2: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/2.jpg)
Last week
• Arrays:• Storing and working collections of data
• Declaration & use
• Initialization
• Passing arrays to functions
• Multi-dimensional arrays
CENG 230 - Spring 2015 Sinan Kalkan 2
![Page 3: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/3.jpg)
char chr_arr[100];float flt_arry[100];double dbl_arry[20];
![Page 4: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/4.jpg)
![Page 5: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/5.jpg)
![Page 6: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/6.jpg)
![Page 7: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/7.jpg)
![Page 8: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/8.jpg)
![Page 9: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/9.jpg)
![Page 10: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/10.jpg)
Array initialization
CENG 230 - Spring 2015 Sinan Kalkan 10
![Page 11: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/11.jpg)
![Page 12: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/12.jpg)
Initializing an Array in a Definition with an initializer List
![Page 13: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/13.jpg)
Specifying an Array’s Size with a Symbolic Constant and Initializing Array Elementswith Calculations
![Page 14: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/14.jpg)
Summing the Elements of an Array
![Page 15: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/15.jpg)
Graphing Array Element Values with Histograms
![Page 16: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/16.jpg)
Rolling a Die 6000 Times and Summarizing the Results in an Array
![Page 17: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/17.jpg)
![Page 18: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/18.jpg)
modifyArray( a, SIZE );
modifyElement( a[ 3 ] ); /* pass array element a[ 3 ] by value */
Fig. 6.13:
![Page 19: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/19.jpg)
Today
• Continue with arrays• Some examples
• Multi-dimensional arrays
CENG 230 - Spring 2015 Sinan Kalkan 19
![Page 20: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/20.jpg)
Searching Arrays (linear)
![Page 21: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/21.jpg)
Example: Sorting Arrays
![Page 22: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/22.jpg)
Multi-dimensional Arrays
CENG 230 - Spring 2015 Sinan Kalkan 23
![Page 23: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/23.jpg)
Initialization:
![Page 24: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/24.jpg)
![Page 25: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/25.jpg)
Reading into an array
CENG 230 - Spring 2015 Sinan Kalkan 26
![Page 26: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/26.jpg)
![Page 27: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/27.jpg)
Homework
• Read two matrices (with integer elements) from the input. Let their sizes be KxL and MxN.
• Write a function to multiply these to matrices, and print the resultant matrix (size KxN) to the screen with another function.
CENG 230 - Spring 2015 Sinan Kalkan 28
![Page 28: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/28.jpg)
![Page 29: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/29.jpg)
![Page 30: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/30.jpg)
![Page 31: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/31.jpg)
![Page 32: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/32.jpg)
![Page 33: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/33.jpg)
![Page 34: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/34.jpg)
![Page 35: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/35.jpg)
![Page 36: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/36.jpg)
![Page 37: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/37.jpg)
![Page 38: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/38.jpg)
![Page 39: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/39.jpg)
![Page 40: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/40.jpg)
Strings
Basics Initializationstrcpy, strncpy,strcat, strncat, strcmp, strncmp functions
![Page 41: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/41.jpg)
![Page 42: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/42.jpg)
![Page 43: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/43.jpg)
![Page 44: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/44.jpg)
![Page 45: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/45.jpg)
![Page 46: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/46.jpg)
![Page 47: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/47.jpg)
Using Character Arrays to Store and Manipulate Strings
![Page 48: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/48.jpg)
![Page 49: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/49.jpg)
q1
q2
![Page 50: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/50.jpg)
q3
![Page 51: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/51.jpg)
q5
q6
![Page 52: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/52.jpg)
q7
![Page 53: CENG 230 Introduction to C Programming - KOVANkovan.ceng.metu.edu.tr/~sinan/ceng230/week12.pdf · CENG 230 Introduction to C Programming Week 12 –Arrays ... Example: Sorting Arrays](https://reader031.vdocuments.site/reader031/viewer/2022021802/5b76825b7f8b9a0c188e6baf/html5/thumbnails/53.jpg)