Anda di halaman 1dari 28

Unit 4

Basic Program Control

4/14/2012

Loops
Repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through loop control instruction. (a) for statement (b) while statement (c) do-while statement
4/14/2012 2

for statement
The for allows us to specify three things about a loop in a single line: (a) Setting a loop counter to an initial value. (b) Testing the loop counter to determine whether its value has reached the number of repetitions desired. (c) Increasing the value of loop counter each time the program segment within the loop has been executed
4/14/2012 3

The for statement provides a compact way to iterate over a range of values.
for (initialization; termination; increment) { statements } All elements in the for loop are optional: for (; ; )
4/14/2012 4

Flowchart representation

4/14/2012

break can be used to interrupt the loop without waiting for the termination condition to evaluate to true continue can be used to skip execution of the body of the loop and re-evaluate the termination condition

4/14/2012

1 2 3 4 5 6 7 8

/* Using the continue statement in a for structure */ #include <stdio.h> int main() { int x;

1. Initialize variable 2. Loop

9
10 11 12 13 14 15 16 17 18 19

for ( x = 1; x <= 10; x++ ) {


if ( x == 5 ) continue; /* skip remaining code in loop only if x == 5 */ printf( "%d ", x ); } printf( "\nUsed continue to skip printing the value 5\n" ); return 0;

3. Print

20 }
1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5

while statement
The while statement is used to continually execute a block of statements while a condition remains true. Initialise counter; while (expression) { Statements; Increment counter; }

4/14/2012

Flowchart representation

4/14/2012

Example
As before, break and continue can be used to terminate the loop or to finish an iteration and go back to the evaluation of the expression for and while are equivalent 1. while (x<i000) x++; 2.while (x<i000) { x++; y=y+10; }
4/14/2012 10

do-while statement
The do-while is similar to the while statement except that the loop is always executed once and the condition is checked at the end of each iteration. do { statement } while (expression) break and continue have the same effect as before
4/14/2012 11

Flowchart representation

4/14/2012

12

Example
do { x++; }while (x<i000);

4/14/2012

13

Arrays
An array is a finite set of variables of the same basic type Instead of giving each variable a name, we use enumeration and group all of them in an array Indexing always starts with 0. If the array has N elements, the last element is in position N-1 The C compiler does not check the array boundaries

4/14/2012

14

Arrays
Array
Group of consecutive memory locations Same name and type

Name of array (Note that all elements of this array have the same name, c)
c[0] c[1] c[2] c[3] -45 6 0 72

To refer to an element, specify


Array name Position number

c[4]
c[5] c[6] c[7] c[8] c[9] c[10] c[11]

1543
-89 0 62 -3 1 6453 78

Format:
arrayname[ position number ]

First element at position 0 n element array named c:


4/14/2012

c[ 0 ], c[ 1 ]...c[ n 1 ]

Position number of the element within array c

15

Arrays
Array elements are like normal variables
c[ 0 ] = 3; printf( "%d", c[ 0 ] );

Perform operations in subscript. If x equals 3


c[ 5 - 2 ] == c[ 3 ] == c[ x ]

4/14/2012

16

Declaring Arrays
When declaring arrays, specify
Name Type of array Number of elements arrayType arrayName[ numberOfElements];

Examples:
int c[ 10 ]; float myArray[ 3284 ];

Declaring multiple arrays of same type


Format similar to regular variables
4/14/2012

Example: int b[ 100 ], x[ 27 ];

17

Arrays
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };

If too many, a syntax error is produced C arrays have no bounds checking

If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };

5 initializers, therefore 5 element array

4/14/2012

18

Example of array
/* Input 5 element array and display it*/ void main() #define s 5 { int a[s],i; clrscr(); for(i=0;i<s;i++) { printf(\n enter a[%d]element:,i); scanf(%d,a[i]); } printf(Array elements are \n); for(i=0;i<s;i++) printf(a[%d]=%d\n,a[i]); getch( ); }

Output: Enter a[0] element:10 Enter a[1] element:20 Enter a[2] element:30 Enter a[3] element:40 Enter a[4] element:50 Array elements are a[0]=10 a[1]=20 a[2]=30 a[3]=40 a[4]=50

4/14/2012

19

Example-2
/*initialization of array*/ void main() { int a[]={30,20,15,45,55}; int i; clrscr(); printf(Array elements are \n); for(i=0;i<5;i++) printf(a[%d]=%d\n,a[i]); getch(); }
4/14/2012 20

Output for example-2


Array elements are: A[0]=30 A[1]=20 A[2]=15 A[3]=45 A[4]=55 30 20

15

45

55 a[4]
21

a[0] a[1] a[2] a[3]


4/14/2012

Another Approach of array initialization

main() { int a[ ]={10,20,30,40,50}; int I; clrscr(); const int s=sizeof (a)/sizeof (int);//(10/2=5) printf(Array elements are \n); for(i=0;i<s;i++) printf(a[%d]=%d\n,i,a[i]); getch(); }
4/14/2012 22

Sorting Arrays
Sorting data
Important computing application
Virtually every organization must sort some data

Bubble sort (sinking sort)


Several passes through the array Successive pairs of elements are compared If increasing order (or identical ), no change If decreasing order, elements exchanged Repeat

Example:
original: 3 4 2 6 7 pass 1: 3 2 4 6 7 pass 2: 2 3 4 6 7 4/14/2012 Small elements "bubble" to the top

23

Searching Arrays: Linear Search and Binary Search


Search an array for a key value Linear search
Simple Compare each element of array with key value Useful for small and unsorted arrays

4/14/2012

24

Searching Arrays: Linear Search and Binary Search


Binary search
For sorted arrays Compares middle element with key
If equal, match found If key < middle, looks in first half of array If key > middle, looks in last half 5 Repeat

Very fast; at most n steps, where 2n > number of elements


30 element array takes at most 5 steps
4/14/2012

25 > 30 so at most 5 steps

25

Multiple-Subscripted Arrays
Multiple subscripted arrays
Tables with rows and columns (m by n array)
Like matrices: specify row, then column
Column 0 Column 1 Column 2 Column 3
Row 0 Row 1 Row 2 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]

a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript Array name Row subscript

4/14/2012

26

Multiple-Subscripted Arrays
Initialization
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
1 3 2 4

1 3

0 4

Referencing elements Specify row, then column


printf( "%d", b[ 0 ][ 1 ] );

4/14/2012

27

Multi-dimensional arrays
int a[3][3]

4/14/2012

28