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

Various topics in c programming, including strings as character arrays, algorithms using arrays, multi-dimensional arrays, and comparing two arrays. It also discusses character arrays for reading and displaying names, displaying names using loops, and sorting algorithms such as bubble sort and binary search. Exercises for inputting names in reverse order and determining the length of character arrays.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming
Lecture 12
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

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