introduction to programming lecture 11. arrays they are special kind of data type they are special...
TRANSCRIPT
Introduction to Introduction to ProgrammingProgramming
Lecture 11Lecture 11
ARRAYSARRAYS
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
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
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
Array name e.g. ageArray name e.g. age
index number index number
age [ 4 ]age [ 4 ]
Referring to Array Referring to Array ElementsElements
for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ ){{
cin >> age [ i ] ;cin >> age [ i ] ;
}}
Example1: Using Example1: Using ArraysArrays
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
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
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 } ;
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
#include < iostream.h >#include < iostream.h >main ( )main ( ){{
int c [ 100 ] ;int c [ 100 ] ;
Example: 3Example: 3
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
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
– 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
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
for ( i =0 ; i < 10 ; i +for ( i =0 ; i < 10 ; i ++ )+ )
b [ i ] = a [ i ] ;b [ i ] = a [ i ] ;
Copying ArraysCopying Arrays
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
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
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
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
# include < stdlib.h ># include < stdlib.h >
0 - 327670 - 32767
rand ( )rand ( )
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 ( )
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 “ % ”
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
It has only two possibilities 0 / It has only two possibilities 0 / 11
rand ( ) % 2 ;rand ( ) % 2 ;
Example: Tossing a Example: Tossing a CoinCoin
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 ( )( )
data typedata type
namename
sizesize
Array Array DeclarationDeclaration
constconst
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