Download presentation
Presentation is loading. Please wait.
Published byMaximilian Newton Modified over 8 years ago
1
Introduction to Programming Lecture 12
2
Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms using arrays Multi-dimensional arrays Multi-dimensional arrays
3
char name [ 100 ] ;
4
\0
5
In C we have Used \nNew Line \tTab Character \0Null Character All C strings are terminated by Null character
6
Character Array in Memory char name [ 100 ] ; cout << “ Please enter your name ” ; cin >> name ;
7
Initializing an Array Initializing array of integers 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 } ; For character arrays char name [ 100 ] = { ‘a’,b’,’c’,’0’,’1’ } ; char name [ 100 ] = “abc01“ ; char name [ ] = “Hello World“ ;
8
Character Arrays To read name from keyboard and display it on screen char name [ 100 ] ; cout << “ Please enter you name” ; cin >> name ; cout << name ;
9
Character Arrays Displaying name on screen using loop for ( i = 0 ; i < 100 ; i ++ ) { cout << name [ i ] ; }
10
Comparing Two arrays Array size should be equal int equal = 0 ; int num1 [ 100 ], num2 [ 100 ] ; for ( i = 0 ; i < 100 ; i ++ ) { if ( num1 [ i ] != num2 [ i ] ) { equal = 1 ; break ; }} if ( equal ==1 ) cout << “ The arrays are not equal” ; else cout << “ The arrays are equal” ; Condition :
11
Comparing Two Arrays AZMAT HAMEED Azmat Hameed
12
Exercise Input your name and display it in reverse order Input your name and display it in reverse order Determine the length of character array Determine the length of character array
13
Sorting Bubble Sort Bubble Sort Quick Sort Quick Sort
14
4 1 9 23 67 [0] [1] [2] [16] [99] 1 Brute-Force Technique
15
66 Swapping 3 33 44 100 66 Memory Location [0] [1] [2] [16] [99]
16
Swapping Two Numbers int num [ ] ; int x ; x = num [ 0 ] ; num [ 0 ] = num [ 15 ] ; num [ 15 ] = x ;
17
Binary Search Algorithms Divide and Conquer rule 5678 12345678 1234 1234
18
Binary Search Algorithm If we think about it, it is If we think about it, it is log n log 2 Total array size will be 2 n and number Total array size will be 2 n and number can be found in n times can be found in n times If 1000 numbers then 10 tries are max If 1000 numbers then 10 tries are max 2 10 = 1024
19
Is divide and conquer the fastest way, all the time, of searching for a number in a list ? 100 3 21 Linear Search ? Binary Search ? Suppose they are Random Suppose they are Ordered Suppose they are mixed-up
20
Functions and Arrays
21
Sending Arrays into Another Functions Name of the array Name of the array Size of the array Size of the array
22
Example 1 c har name [ 100 ] ; reverse ( name, 100 ) ; reverse ( name, 100 ) ; Declaration Function Call
23
Example 1 void reverse ( char [ ], int ) ; void reverse ( char [ ], int ) ; void reverse ( char characters [ ], int arraySize) { reverse the character string; reverse the character string; } Prototype Definition
24
Example 1 main ( ) { cin >> name [ ] ; reverse ( character [ ], arraySize ) ; cout << name [ ] ; } What will it Show ?
25
Call by Reference & Address Operator * Pointer Operator In case of arrays, call by reference is default
26
X is a variable which is a location in the memory Name [ ] is an array -------- Array called Name Starting address Memory name
27
Example 2 void f ( int [ ], int ) ; main ( ) { int numbers [ 100 ] ; f ( numbers, 100) ; for ( int i = 0 ; i < 100 ; i ++) cout << numbers [ i ] ; }
28
Example 2 void f ( int x [ ], int arraySize ) { int i ; for ( i = 0 ; i < arraySize ; i ++) x [ i ] = i ; }
29
f ( x [ 3 ] ) ; Executed with call by value, not by reference
30
Whenever a variable is passed, it is passed by value Whenever a variable is passed, it is passed by value Whenever you pass an array to Whenever you pass an array to function, it is called by reference function, it is called by reference
31
Vector 2 Dimensional 3 Dimensional Dot Product Vector Product
32
Matrix Rows Columns
33
Two Dimensional Array int x [ 2 ] [ 3 ] ;
34
Example 3 int maxRows = 2; int maxCols = 3 ; int matrix [ 2] [ 3 ]; int row, col ; for ( row = 0 ; row < maxRows ; row ++ ) { for ( col = 0 ; col < maxCols ; col ++ ) { cout << “Please enter value of ”<< row << “ “ << col; cin >> matrix [ row ] [ col ] ; }}
35
529 529704 After first outer loop After second outer loop Input [0] [1] [0]
36
Three Dimensional Arrays int x [ ] [ ] [ ] ;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.