Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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;
9
10 11 12 13 14 15 16 17 18 19
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
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 ]
c[ 0 ], c[ 1 ]...c[ n 1 ]
15
Arrays
Array elements are like normal variables
c[ 0 ] = 3; printf( "%d", c[ 0 ] );
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 ];
17
Arrays
Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
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
15
45
55 a[4]
21
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
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
4/14/2012
24
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
4/14/2012
27
Multi-dimensional arrays
int a[3][3]
4/14/2012
28