Programming For Engineers Lecture 08
Programming For Engineers Lecture 08
2
How to Organize Books?
3
ARRAYS
Arrays
• They are special kind of data type
• They are like data structures in which
identical data types are stored
• In C/C++ each array has
• name
• data type
• size
•
They occupy continuous area of
memory
Name memory
C[0] 24
C[1] 59
C[2] 35
C[3] ... Storage of
C[4]
C[5]
...
...
an array in
C[6] ... memory
C[7] ...
C[8] ...
C[9] ...
Index
Declaration of Arrays
arrayType arrayName[numberOfElements ];
For example ,
int age [ 10 ] ;
age [ 4 ]
Initializing an Array
int age[ 10 ] = { 0 } ;
Initializing an Array
int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;
for ( i = 0 ; i < 10 ; i ++ )
• #include <iostream>
• Using namespace std; • for ( int i = 0; i < 10; i++ )
• int main() • cout << “ ” << i << “ ” <<
• { n[ i ] << endl;
• int n[ 10 ] = { 32, 27, 64, 18, 95,
14, 90, 70, 60, 37 }; • return 0;
• cout << "Element" << “ ” << •}
"Value" << endl;
12
Copying Arrays
• Data types should be
identical
b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
b[3]=a[3];
………
………
b [ 10 ] = a [ 10 ] ;
Copying Arrays
for ( i =0 ; i < 10 ; i ++ )
b[i]=a[i];
Practice :
Take the sum of squares of 10 different numbers which are
stored in an array
int a [ 10 ] ;
int arraySize =10 ;
int sumOfSquares = 0 ;
for ( i = 0 ; i < arraySize ; i ++ )
{
sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ;
}
Practice :
int z ;
int a [ 100 ] ;
for ( i = 0 ; i < 100 ; i ++ )
{
a[i]=i;
}
cout << “ Please enter a positive integer “ ;
cin >> z ;
int found = 0 ;
Practice :
for ( i =0 ; i < 100 ; i ++ )
{
if ( z == a [ i ] )
{
found = 1 ;
break ;
}
}
Practice :
if ( found == 1 )
cout << “ We found the integer at position ” << i ;
else
cout << “ The number was not found” ;
Array Declaration
data type
name
size
const
const
const int arraySize = 100 ;
\t Tab Character
\0 Null Character
int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ;
int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;
int equal = 0 ;
int num1 [ 100 ] , num2 [ 100 ] ;
for ( i = 0 ; i < 100 ; i ++ )
{
if ( num1 [ i ] != num2 [ i ] )
{
equal = 1 ;
break ;
}
}
if ( equal ==1 )
cout << “ The arrays are not equal” ;
else
cout << “ The arrays are equal” ;
Comparing Two Arrays
IRFAN YOUNAS
Irfan younas
Exercise
• Input your name and display it in reverse order
• Quick Sort
Brute-Force Technique
[0] 4
[1] 23
[2] 9
[16] 1
[99] 67
Swapping
[0] 66
Memory
[1] 44
Location
[2] 33
66
[16] 3
[99] 100
Swapping Two Numbers
int num [ ] ;
int x ;
x = num [ 0 ] ;
num [ 0 ] = num [ 15 ] ;
num [ 15 ] = x ;
Binary Search Algorithms
Divide and Conquer rule
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4
Binary Search Algorithm
• If we think about it , it is
logn log2
1
2 Linear Search ?
3
Binary Search ?
Suppose they are Random
100 Suppose they are Ordered
Suppose they are mixed-up
Functions and Arrays
Sending Arrays into Another Functions
Declaration
char name [ 100 ] ;
Function Call
reverse ( name , 100 ) ;
Example 1
Prototype
void reverse ( char [ ] , int ) ;
Definition
void reverse ( char characters [ ] , int arraySize)
{
reverse the character string;
}
Example 1
main ( )
{
cin >> name [ ] ;
reverse ( character [ ] , arraySize ) ;
cout << name [ ] ;
} What will it
Show ?
Call by Reference
& Address Operator
* Pointer Operator
name
Example 2
void f ( int [ ] , int ) ;
main ( )
{
int numbers [ 100 ] ;
f ( numbers , 100) ;
for ( int i = 0 ; i < 100 ; i ++)
cout << numbers [ i ] ;
}
Example 2
void f ( int x [ ] , int arraySize )
{
int i ;
for ( i = 0 ; i < arraySize ; i ++)
x[i]=i;
}
f(x[3]);
Executed with call by value, not by
reference
• Whenever a variable is passed , it is
passed by value
• Whenever you pass an array to
function, it is called by reference
Vector
2 Dimensional
3 Dimensional
Dot Product
Vector Product
Matrix
Rows
Columns
Two Dimensional Array
int x [ 2 ] [ 3 ] ;
Example 3
int maxRows = 2;
int maxCols = 3 ;
int matrix [ 2] [ 3 ];
int row , col ;
for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << “Please enter value of ”<< row << “ “ << col;
cin >> matrix [ row ] [ col ] ;
}
}
After first outer loop
Input
[0]
5 2 9
[0] 5 2 9
[1] 7 0 4
Array Manipulation
Example 1
Input
Row 1 1 2 3 Memory
Row 2 4 5 6
Row 3 7 8 9
Row 3 7 8 9
Row 2 4 5 6
Row 1 1 2 3
Output
Addressing Array Elements
a [rowIndex ] [ columnIndex ]
Example 1
int row ;
int col ;
const maxRows = 3 ;
const maxCols = 3 ;
int a [ maxRows ] [ maxCols ] ;
Example 1
for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << “Please enter value of element number ”<<row<< “,” <<
col ;
cin >> a [ row ] [ col ] ;
}
}
Example 2
maxRows = 3 ;
maxCols = 3 ;
Row 1 1 2 3 Row 3 7 8 9
Row 2 4 5 6 Row 2 4 5 6
Row 3 7 8 9 Row 1 1 2 3
Example 2: Formatted Output
1 2 3
4 5 6
7 8 9
Square Matrix
Number of rows are equal to number of columns
arraySize = rows
cols
Square Matrix
a ij = a ji
i = rows
j = columns
Square Matrix
int a [ row ] [ col ] ;
int arraySize ;
for ( row = 0 ; row < arraySize ; row ++ )
{
for ( col = 0 ; col < arraySize ; col ++ )
{
//Swap values
}
}
Swap Mechanisms