Docsity

Introduction to Programming Lecture 12: Strings, Arrays, and Algorithms, Slides of Computer Programming

Partial preview of the text

Download Introduction to Programming Lecture 12: Strings, Arrays, and Algorithms and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 12

Today’s Lecture Includes

 Strings ( character arrays )  Algorithms using arrays  Multi-dimensional arrays

  • \

In C we have Used

\n New Line

\t Tab Character

\0 Null Character

All C strings are terminated by Null character

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“ ; Docsity.com

Character Arrays

To read name from keyboard and

display it on screen

char name [ 100 ] ; cout << “ Please enter you name” ; cin >> name ; cout << name ;

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 :

Comparing Two Arrays

AZMAT HAMEED

Azmat Hameed

Sorting

Bubble Sort

Quick Sort

4

1

9

23

67

[0] [1] [2]

[16]

[99]

1

Brute-Force

Technique

Swapping Two

Numbers

int num [ ] ; int x ; x = num [ 0 ] ; num [ 0 ] = num [ 15 ] ; num [ 15 ] = x ;

Binary Search Algorithms

Divide and Conquer rule

5 6 7 8

1 2 3 4 5 6 7 8

1 2 3 4

1 2 3 4

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

100

3

2

1 Linear Search?

Binary Search?

Suppose they are Random

Suppose they are Ordered Suppose they are mixed-up

Functions and Arrays