53
CENG 230 Introduction to C Programming Week 12 – Arrays Sinan Kalkan Some slides/content are borrowed from Tansel Dokeroglu, Nihan Kesim Cicekli, and the lecture notes of the textbook by Hanly and Koffman.

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

  • Upload
    tranque

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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
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
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
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
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
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
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

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
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

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

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

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

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

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
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

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

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

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

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

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

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
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

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
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

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
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
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
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
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
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
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
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
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
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
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
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
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

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
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
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
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
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
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
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

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
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

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

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

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

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