Upload
bilal-ahmed
View
26
Download
2
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
Introduction to ProgrammingIntroduction to Programming
Lecture 12Lecture 12
Today’s Lecture Today’s Lecture IncludesIncludes
Strings ( character arrays )Strings ( character arrays ) Algorithms using arraysAlgorithms using arrays Multi-dimensional arraysMulti-dimensional arrays
char name [ 100 ] ;char name [ 100 ] ;
\0\0
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
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 ;
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“ ;
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 ;
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 ] ;
}}
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 :
Comparing Two Comparing Two ArraysArrays
AZMAT HAMEED AZMAT HAMEED
Azmat HameedAzmat Hameed
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
Sorting Sorting
Bubble SortBubble Sort
Quick SortQuick Sort
44
11
99
2323
6767
[0]
[1]
[2]
[16]
[99]
11
Brute-Force Brute-Force TechniqueTechnique
6666
SwappingSwapping
33
3333
4444
100100
6666MemoryLocation
[0]
[1]
[2]
[16]
[99]
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 ;
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
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
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
Functions and Functions and ArraysArrays
Sending Arrays into Sending Arrays into Another FunctionsAnother Functions
Name of the arrayName of the array Size of the arraySize of the array
Example 1Example 1
cchar name [ 100 ] ;har name [ 100 ] ;
reverse ( name , reverse ( name , 100 ) ;100 ) ;
Declaration
Function Call
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
Example 1Example 1 main ( ) main ( )
{{
cin >> name [ ] ;cin >> name [ ] ;
reverse ( character [ ] , arraySize reverse ( character [ ] , arraySize ) ;) ;
cout << name [ ] ;cout << name [ ] ;
}}
What will itShow ?
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
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
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 ] ;
}}
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 ;
}}
f ( x [ 3 ] ) ;f ( x [ 3 ] ) ;
Executed with call Executed with call by value, not by by value, not by
referencereference
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
VectorVector
2 Dimensional2 Dimensional
3 Dimensional3 Dimensional
Dot ProductDot Product
Vector ProductVector Product
MatrixMatrix
RowsColumns
Two Dimensional Two Dimensional ArrayArray
int x [ 2 ] [ 3 ] ;int x [ 2 ] [ 3 ] ;
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 ] ;}}
}}
55 22 99
55 22 99
77 00 44
After first outer loop
After second outer loop
Input
Input
[0]
[1]
[0]
Three Dimensional Three Dimensional ArraysArrays
int x [ ] [ ] [ ] ;int x [ ] [ ] [ ] ;