36
Introduction to Programmin Introduction to Programmin Lecture 12 Lecture 12

CS201- Introduction to Programming- Lecture 12

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 12 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 12

Introduction to ProgrammingIntroduction to Programming

Lecture 12Lecture 12

Page 2: CS201- Introduction to Programming- Lecture 12

Today’s Lecture Today’s Lecture IncludesIncludes

Strings ( character arrays )Strings ( character arrays ) Algorithms using arraysAlgorithms using arrays Multi-dimensional arraysMulti-dimensional arrays

Page 3: CS201- Introduction to Programming- Lecture 12

char name [ 100 ] ;char name [ 100 ] ;

Page 4: CS201- Introduction to Programming- Lecture 12

\0\0

Page 5: CS201- Introduction to Programming- Lecture 12

In C we have UsedIn C we have Used

\n\n New LineNew Line

\t\t Tab CharacterTab Character

\0\0 Null CharacterNull Character

All C strings are terminated by All C strings are terminated by NullNull charactercharacter

Page 6: CS201- Introduction to Programming- Lecture 12

Character Array in Character Array in MemoryMemory

char name [ 100 ] ;char name [ 100 ] ;

cout << “ Please enter your name ” ;cout << “ Please enter your name ” ;

cin >> name ;cin >> name ;

Page 7: CS201- Introduction to Programming- Lecture 12

Initializing an Initializing an ArrayArray

Initializing array of integersInitializing array of integers

int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ;int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ;int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;

For character arraysFor character arrayschar name [ 100 ] = char name [ 100 ] =

{ ‘a’,b’,’c’,’0’,’1’ } ;{ ‘a’,b’,’c’,’0’,’1’ } ;char name [ 100 ] = “abc01“ ;char name [ 100 ] = “abc01“ ;char name [ ] = “Hello World“ ;char name [ ] = “Hello World“ ;

Page 8: CS201- Introduction to Programming- Lecture 12

Character ArraysCharacter Arrays

To read name from keyboard To read name from keyboard and display it on screen and display it on screen

char name [ 100 ] ;char name [ 100 ] ;

cout << “ Please enter you cout << “ Please enter you name” ;name” ;

cin >> name ;cin >> name ;

cout << name ;cout << name ;

Page 9: CS201- Introduction to Programming- Lecture 12

Character ArraysCharacter ArraysDisplaying name on screen using Displaying name on screen using

looploop

for ( i = 0 ; i < 100 ; i ++ )for ( i = 0 ; i < 100 ; i ++ )

{{

cout << name [ i ] ;cout << name [ i ] ;

}}

Page 10: CS201- Introduction to Programming- Lecture 12

Comparing Two arraysComparing Two arraysArray size should be equalArray size should be equal

int equal = 0 ;int equal = 0 ;int num1 [ 100 ] , num2 [ 100 ] ;int num1 [ 100 ] , num2 [ 100 ] ;for ( i = 0 ; i < 100 ; i ++ )for ( i = 0 ; i < 100 ; i ++ ){{

if ( num1 [ i ] != num2 [ i ] )if ( num1 [ i ] != num2 [ i ] ){{

equal = 1 ;equal = 1 ;break ;break ;

}}}}

if ( equal ==1 )if ( equal ==1 )cout << “ The arrays are not equal” ;cout << “ The arrays are not equal” ;

elseelsecout << “ The arrays are equal” ;cout << “ The arrays are equal” ;

Condition :

Page 11: CS201- Introduction to Programming- Lecture 12

Comparing Two Comparing Two ArraysArrays

AZMAT HAMEED AZMAT HAMEED

Azmat HameedAzmat Hameed

Page 12: CS201- Introduction to Programming- Lecture 12

ExerciseExercise Input your name and display it Input your name and display it

in reverse orderin reverse order

Determine the length of Determine the length of character arraycharacter array

Page 13: CS201- Introduction to Programming- Lecture 12

Sorting Sorting

Bubble SortBubble Sort

Quick SortQuick Sort

Page 14: CS201- Introduction to Programming- Lecture 12

44

11

99

2323

6767

[0]

[1]

[2]

[16]

[99]

11

Brute-Force Brute-Force TechniqueTechnique

Page 15: CS201- Introduction to Programming- Lecture 12

6666

SwappingSwapping

33

3333

4444

100100

6666MemoryLocation

[0]

[1]

[2]

[16]

[99]

Page 16: CS201- Introduction to Programming- Lecture 12

Swapping Two Swapping Two NumbersNumbers

int num [ ] ;int num [ ] ;

int x ;int x ;

x = num [ 0 ] ;x = num [ 0 ] ;

num [ 0 ] = num [ 15 ] num [ 0 ] = num [ 15 ] ;;

num [ 15 ] = x ;num [ 15 ] = x ;

Page 17: CS201- Introduction to Programming- Lecture 12

Binary Search Binary Search AlgorithmsAlgorithms

Divide and ConquerDivide and Conquer rulerule

55 66 77 88

11 22 33 44 55 66 77 88

11 22 33 44

11 22 33 44

Page 18: CS201- Introduction to Programming- Lecture 12

Binary Search Binary Search AlgorithmAlgorithm

If we think about it , it isIf we think about it , it is

loglognn loglog22

Total array size will be 2Total array size will be 2nn and number and number

can be found in n timescan be found in n times

If 1000 numbers then 10 tries are max If 1000 numbers then 10 tries are max

221010 = 1024 = 1024

Page 19: CS201- Introduction to Programming- Lecture 12

Is divide and conquer the fastest way, all Is divide and conquer the fastest way, all the time, of searching for a number in a the time, of searching for a number in a

list list ??

100100

33

22

11Linear Search ?

Binary Search ?

Suppose they are Random

Suppose they are Ordered

Suppose they are mixed-up

Page 20: CS201- Introduction to Programming- Lecture 12

Functions and Functions and ArraysArrays

Page 21: CS201- Introduction to Programming- Lecture 12

Sending Arrays into Sending Arrays into Another FunctionsAnother Functions

Name of the arrayName of the array Size of the arraySize of the array

Page 22: CS201- Introduction to Programming- Lecture 12

Example 1Example 1

cchar name [ 100 ] ;har name [ 100 ] ;

reverse ( name , reverse ( name , 100 ) ;100 ) ;

Declaration

Function Call

Page 23: CS201- Introduction to Programming- Lecture 12

Example 1Example 1

void reverse ( char [ ] , int ) ; void reverse ( char [ ] , int ) ;

void reverse ( char characters [ ] , int arraySize)void reverse ( char characters [ ] , int arraySize)

{{ reverse the character string; reverse the character string;

}}

Prototype

Definition

Page 24: CS201- Introduction to Programming- Lecture 12

Example 1Example 1 main ( ) main ( )

{{

cin >> name [ ] ;cin >> name [ ] ;

reverse ( character [ ] , arraySize reverse ( character [ ] , arraySize ) ;) ;

cout << name [ ] ;cout << name [ ] ;

}}

What will itShow ?

Page 25: CS201- Introduction to Programming- Lecture 12

Call by Call by ReferenceReference

& & Address OperatorAddress Operator

** Pointer OperatorPointer Operator

In case of arrays , call by reference is In case of arrays , call by reference is defaultdefault

Page 26: CS201- Introduction to Programming- Lecture 12

X is a variable which is a location in X is a variable which is a location in thethe

memorymemory

Name [ ] is an arrayName [ ] is an array-- -- -- -- -- -- -- --Array called Name

Starting address

Memory

name

Page 27: CS201- Introduction to Programming- Lecture 12

Example 2Example 2void f ( int [ ] , int ) ;void f ( int [ ] , int ) ;

main ( ) main ( )

{{

int numbers [ 100 ] ;int numbers [ 100 ] ;

f ( numbers , 100) ;f ( numbers , 100) ;

for ( int i = 0 ; i < 100 ; i ++)for ( int i = 0 ; i < 100 ; i ++)

cout << numbers [ i ] ;cout << numbers [ i ] ;

}}

Page 28: CS201- Introduction to Programming- Lecture 12

Example 2Example 2

void f ( int x [ ] , int arraySize )void f ( int x [ ] , int arraySize )

{{

int i ; int i ;

for ( i = 0 ; i < arraySize ; i for ( i = 0 ; i < arraySize ; i ++)++)

x [ i ] = i ;x [ i ] = i ;

}}

Page 29: CS201- Introduction to Programming- Lecture 12

f ( x [ 3 ] ) ;f ( x [ 3 ] ) ;

Executed with call Executed with call by value, not by by value, not by

referencereference

Page 30: CS201- Introduction to Programming- Lecture 12

Whenever a variable is Whenever a variable is passed , it is passed by valuepassed , it is passed by value

Whenever you pass an array Whenever you pass an array toto

function, it is called by function, it is called by referencereference

Page 31: CS201- Introduction to Programming- Lecture 12

VectorVector

2 Dimensional2 Dimensional

3 Dimensional3 Dimensional

Dot ProductDot Product

Vector ProductVector Product

Page 32: CS201- Introduction to Programming- Lecture 12

MatrixMatrix

RowsColumns

Page 33: CS201- Introduction to Programming- Lecture 12

Two Dimensional Two Dimensional ArrayArray

int x [ 2 ] [ 3 ] ;int x [ 2 ] [ 3 ] ;

Page 34: CS201- Introduction to Programming- Lecture 12

Example 3Example 3int maxRows = 2;int maxRows = 2;int maxCols = 3 ;int maxCols = 3 ;int matrix [ 2] [ 3 ];int matrix [ 2] [ 3 ];int row , col ;int row , col ;for ( row = 0 ; row < maxRows ; row ++ )for ( row = 0 ; row < maxRows ; row ++ ){{

for ( col = 0 ; col < maxCols ; col ++ )for ( col = 0 ; col < maxCols ; col ++ ){{

cout << “Please enter value of ”<< row << “ “ cout << “Please enter value of ”<< row << “ “ << col;<< col;

cin >> matrix [ row ] [ col ] ;cin >> matrix [ row ] [ col ] ;}}

}}

Page 35: CS201- Introduction to Programming- Lecture 12

55 22 99

55 22 99

77 00 44

After first outer loop

After second outer loop

Input

Input

[0]

[1]

[0]

Page 36: CS201- Introduction to Programming- Lecture 12

Three Dimensional Three Dimensional ArraysArrays

int x [ ] [ ] [ ] ;int x [ ] [ ] [ ] ;