+ All Categories
Home > Education > CS201- Introduction to Programming- Lecture 11

CS201- Introduction to Programming- Lecture 11

Date post: 18-Dec-2014
Category:
Upload: bilal-ahmed
View: 15 times
Download: 0 times
Share this document with a friend
Description:
Virtual University Course CS201- Introduction to Programming Lecture No 11 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Popular Tags:
30
Introduction to Introduction to Programming Programming Lecture 11 Lecture 11
Transcript
Page 1: CS201- Introduction to Programming- Lecture 11

Introduction to Introduction to ProgrammingProgramming

Lecture 11Lecture 11

Page 2: CS201- Introduction to Programming- Lecture 11

ARRAYSARRAYS

Page 3: CS201- Introduction to Programming- Lecture 11

They are special kind of data typeThey are special kind of data type They are like data structures in whichThey are like data structures in which

identical data types are storedidentical data types are stored In C each array has In C each array has

– namename– data type data type – sizesize

They occupy continuous area of They occupy continuous area of

memorymemory

ArraysArrays

Page 4: CS201- Introduction to Programming- Lecture 11

Storage of an array in Storage of an array in memorymemory

C[0]

C[1]

C[2]

C[3]

C[4]

C[5]

C[6]

C[7]

C[8]

C[9]

Name

...

35

59

24

...

...

...

...

...

...

memory

Index

Page 5: CS201- Introduction to Programming- Lecture 11

arrayType arrayName[numberOfElements ];arrayType arrayName[numberOfElements ];

For example , For example ,

int age [ 10 ] ;int age [ 10 ] ;

More than one array can be declared on a line More than one array can be declared on a line

int age [10] , height [10] , names [20] ;int age [10] , height [10] , names [20] ;

Mix declaration of variables with declaration of Mix declaration of variables with declaration of arraysarrays

int i , j , age [10] ;int i , j , age [10] ;

Declaration of Declaration of ArraysArrays

Page 6: CS201- Introduction to Programming- Lecture 11

Array name e.g. ageArray name e.g. age

index number index number

age [ 4 ]age [ 4 ]

Referring to Array Referring to Array ElementsElements

Page 7: CS201- Introduction to Programming- Lecture 11

for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ ){{

cin >> age [ i ] ;cin >> age [ i ] ;

}}

Example1: Using Example1: Using ArraysArrays

Page 8: CS201- Introduction to Programming- Lecture 11

totalAge = 0 ;totalAge = 0 ;

for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ ){{

totalAge + = age [ i ] totalAge + = age [ i ] ;;

}}

Example 2Example 2

Page 9: CS201- Introduction to Programming- Lecture 11

int age [ 10 ] ;int age [ 10 ] ;

for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ ){{

age [ i ] = 0 ;age [ i ] = 0 ;}}

Initializing an ArrayInitializing an Array

Page 10: CS201- Introduction to Programming- Lecture 11

Initializing an Initializing an ArrayArray

int age [ 10 ] = int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ;{ 0,0,0,0,0,0,0,0,0,0 } ;

int age[ 10 ] = { 0 } ;int age[ 10 ] = { 0 } ;

Page 11: CS201- Introduction to Programming- Lecture 11

int age [ ] = int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;{ 1,2,3,4,5,6,7,8,9,10 } ;

for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ )

Initializing an Initializing an ArrayArray

‘ i ‘ will have value from 0 to 9

Page 12: CS201- Introduction to Programming- Lecture 11

#include < iostream.h >#include < iostream.h >main ( )main ( ){{

int c [ 100 ] ;int c [ 100 ] ;

Example: 3Example: 3

Page 13: CS201- Introduction to Programming- Lecture 11

int z , i = 0 ;int z , i = 0 ;

dodo{{

cin >> z ;cin >> z ;if ( z != -1 )if ( z != -1 )c[ i ] = z ;c[ i ] = z ;

Example: 3Example: 3

assignment statement

Page 14: CS201- Introduction to Programming- Lecture 11

i ++ ;i ++ ;

} while ( z != -1 && i < 100 ) ;} while ( z != -1 && i < 100 ) ;

cout << “ The total number of positive cout << “ The total number of positive integers entered by user is “ << i -1;integers entered by user is “ << i -1;

}}

Example 3Example 3

Page 15: CS201- Introduction to Programming- Lecture 11

– Data types should beData types should be

identicalidentical

– Size should be same Size should be same

int a [ 10 ] ;int a [ 10 ] ;

int b [ 10 ] ;int b [ 10 ] ;

Copying ArraysCopying Arrays

Page 16: CS201- Introduction to Programming- Lecture 11

To copy from array “ a ” to array “ b ” :To copy from array “ a ” to array “ b ” :

b [ 0 ] = a [ 0 ] ;b [ 0 ] = a [ 0 ] ; b [ 1 ] = a [ 1 ] ;b [ 1 ] = a [ 1 ] ; b [ 2 ] = a [ 2 ] ;b [ 2 ] = a [ 2 ] ; b [ 3 ] = a [ 3 ] ;b [ 3 ] = a [ 3 ] ;… … …… … …… … …… … … b [ 10 ] = a [ 10 ] ;b [ 10 ] = a [ 10 ] ;

Copying ArraysCopying Arrays

Page 17: CS201- Introduction to Programming- Lecture 11

for ( i =0 ; i < 10 ; i +for ( i =0 ; i < 10 ; i ++ )+ )

b [ i ] = a [ i ] ;b [ i ] = a [ i ] ;

Copying ArraysCopying Arrays

Page 18: CS201- Introduction to Programming- Lecture 11

Take the sum of squares of 10 different Take the sum of squares of 10 different numbers which are stored in an array numbers which are stored in an array

int a [ 10 ] ;int a [ 10 ] ;int int arraySize =10 ;arraySize =10 ;int sumOfSquares = 0 ;int sumOfSquares = 0 ;for ( i = 0 ; i < arraySize ; i ++ )for ( i = 0 ; i < arraySize ; i ++ ){{

sumOfSquares = sumOfSquares + a [ i ] * sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ;a [ i ] ;}}

Example: 4Example: 4

Page 19: CS201- Introduction to Programming- Lecture 11

int z ;int z ;

int a [ 100 ] ;int a [ 100 ] ;

for ( i = 0 ; i < 100 ; i ++ )for ( i = 0 ; i < 100 ; i ++ )

{{

a [ i ] = i ;a [ i ] = i ;

}}

cout << “ Please enter a positive cout << “ Please enter a positive integer “ ;integer “ ;

cin >> z ;cin >> z ;

int found = 0 ;int found = 0 ;

Example 5Example 5

Page 20: CS201- Introduction to Programming- Lecture 11

for ( i =0 ; i < 100 ; i ++ )for ( i =0 ; i < 100 ; i ++ ){{

if ( z == a [ i ] )if ( z == a [ i ] ){{

found = 1 ;found = 1 ;break ;break ;

}}}}

Example 5Example 5

Page 21: CS201- Introduction to Programming- Lecture 11

if ( found == 1 )if ( found == 1 )

cout << “ We found the integer at position cout << “ We found the integer at position ” << i ;” << i ;

else else

cout << “ The number was not found” ;cout << “ The number was not found” ;

Example 5Example 5

Page 22: CS201- Introduction to Programming- Lecture 11

# include < stdlib.h ># include < stdlib.h >

0 - 327670 - 32767

rand ( )rand ( )

Page 23: CS201- Introduction to Programming- Lecture 11

x = rand ( ) ;x = rand ( ) ;

A call goes to ” rand ( ) “ , it A call goes to ” rand ( ) “ , it generates a number and returns generates a number and returns to xto x

Calling rand ( )Calling rand ( )

Page 24: CS201- Introduction to Programming- Lecture 11

It returns the remainderIt returns the remainder

rand ( ) % 6 = rand ( ) % 6 = ??

Result has to be between 0 and 5 inclusiveResult has to be between 0 and 5 inclusive

1 + rand ( ) % 6 1 + rand ( ) % 6

It will randomly generate number between It will randomly generate number between 1 and 61 and 6

Modulus “ % ”Modulus “ % ”

Page 25: CS201- Introduction to Programming- Lecture 11

If a die is rolled 10/100 million of time , If a die is rolled 10/100 million of time , then on average equal number of then on average equal number of 1’s ,equal number of 2’s , equal 1’s ,equal number of 2’s , equal number of 3’s etc. will be generatednumber of 3’s etc. will be generated

Fair DieFair Die

Page 26: CS201- Introduction to Programming- Lecture 11

It has only two possibilities 0 / It has only two possibilities 0 / 11

rand ( ) % 2 ;rand ( ) % 2 ;

Example: Tossing a Example: Tossing a CoinCoin

Page 27: CS201- Introduction to Programming- Lecture 11

It is shipped in every standard library It is shipped in every standard library with compilerwith compiler

Most major programming languages Most major programming languages give some kind of random number give some kind of random number generator as a function as part of generator as a function as part of library library

Writing a random number generator is Writing a random number generator is itself a fielditself a field

Importance of rand Importance of rand ( )( )

Page 28: CS201- Introduction to Programming- Lecture 11

data typedata type

namename

sizesize

Array Array DeclarationDeclaration

Page 29: CS201- Introduction to Programming- Lecture 11

constconst

Page 30: CS201- Introduction to Programming- Lecture 11

const int arraySize = 100 ;const int arraySize = 100 ;

It creates an identifier “ arraySize ” It creates an identifier “ arraySize ” and assigns a value 100. This is and assigns a value 100. This is

called integer constant . It is called integer constant . It is notnot a variablea variable

Its value cannot be changedIts value cannot be changed

constconst


Recommended