cs201- introduction to programming- lecture 11
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 11 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 11Lecture 11
![Page 2: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/2.jpg)
ARRAYSARRAYS
![Page 3: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/12.jpg)
#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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/15.jpg)
– 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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/22.jpg)
# include < stdlib.h ># include < stdlib.h >
0 - 327670 - 32767
rand ( )rand ( )
![Page 23: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/28.jpg)
data typedata type
namename
sizesize
Array Array DeclarationDeclaration
![Page 29: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/29.jpg)
constconst
![Page 30: CS201- Introduction to Programming- Lecture 11](https://reader035.vdocuments.site/reader035/viewer/2022062616/5492e781b47959834a8b4688/html5/thumbnails/30.jpg)
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