0% found this document useful (0 votes)
9 views70 pages

Programming For Engineers Lecture 08

This document discusses arrays in C/C++. It explains that arrays allow us to store multiple values of the same data type. Arrays have a name, data type, and size. Values in an array are stored in contiguous memory locations that can be accessed using an index. The document provides examples of declaring, initializing, accessing, copying, comparing and manipulating arrays. It also discusses passing arrays to functions and two-dimensional arrays.

Uploaded by

Malik Adnan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
9 views70 pages

Programming For Engineers Lecture 08

This document discusses arrays in C/C++. It explains that arrays allow us to store multiple values of the same data type. Arrays have a name, data type, and size. Values in an array are stored in contiguous memory locations that can be accessed using an index. The document provides examples of declaring, initializing, accessing, copying, comparing and manipulating arrays. It also discusses passing arrays to functions and two-dimensional arrays.

Uploaded by

Malik Adnan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 70

In Previous lecture

• We used functions for breaking complex problems into smaller


pieces, which is a top-down structured approach.
• Each function should be a small module, self contained and it
should solve a well defined problem.
• Variable names and function names should be self explanatory.
• Always comment your code
Train your mind……….!!!

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 ] ;

• More than one array can be declared on a line


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

• Mix declaration of variables with declaration of arrays


int i , j , age [10] ;
Referring to Array Elements

Array name e.g. age


index number

age [ 4 ]
Initializing an Array

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

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 ++ )

‘ i ‘ will have value from 0 to 9


Example1: Initializing an array
• cout << "Element" << " " <<
#include <iostream> "Value" << endl;
using namespace std;
int main() • for ( i = 0; i < 10; i++ ) // print array
{ • cout << " " << i << " "<< n[ i ]
int i, n[ 10 ]; << endl;

for ( i = 0; i < 10; i++ ) // initialize • return 0;


array
•}
n[ i ] = 0;
Initializing an array with a declaration

• #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

• Size should be same


int a [ 10 ] ;
int b [ 10 ] ;
Copying Arrays
To copy from array “ a ” to array “ b ” :

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 ;

• It creates an identifier “ arraySize ” and assigns a


value 100. This is called integer constant . It is not
a variable
• Its value cannot be changed
char name [ 100 ] ;
\0
In C we have Used
\n New Line

\t Tab Character

\0 Null Character

All C strings are terminated by Null character


Character Array in Memory

char name [ 100 ] ;


cout << “ Please enter your name ” ;
cin >> name ;
Initializing an Array
Initializing array of integers

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 } ;

For character arrays


char name [ 100 ] = { ‘a’,b’,’c’,’0’,’1’ } ;
char name [ 100 ] = “abc01“ ;
char name [ ] = “Hello World“ ;
Character Arrays
To read name from keyboard and
display it on screen
char name [ 100 ] ;
cout << “ Please enter you name” ;
cin >> name ;
cout << name ;
Character Arrays
Displaying name on screen using loop

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


{
cout << name [ i ] ;
}
Comparing Two arrays
Condition :
Array size should be equal

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

• Determine the length of character array


Sorting
• Bubble Sort

• 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

• Total array size will be 2n and number


can be found in n times

• If 1000 numbers then 10 tries are max


210 = 1024
Is divide and conquer the fastest way, all the time, of
searching for a number in a list ?

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

• Name of the array


• Size of the array
Example 1

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

In case of arrays , call by reference is default


X is a variable which is a location in the
memory
Name [ ] is an array
Starting
address - - - - - - - -
Array called Name
Memory

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

After second outer


loop Input

[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 ;

Index of Start [0] 1 2 3


[1]
Index of Last Row = maxRows - 1 [2]
Example 2
for ( row = maxRows - 1 ; row >= 0 ; row -- )
{
Decrement Operator
for ( col = 0 ; col < maxCols ; col ++ )

}

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

cout << “The original matrix is” ;


for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << a [ row ] [ col ] ; << ‘\t‘ ;
}
} 15 42
Example 2: Formatted Output
for ( row = 0 ; row < maxRows ; row ++ )
{
for ( col = 0 ; col < maxCols ; col ++ )
{
cout << a [ row ] [ col ] << ‘\t’ ;
}
cout << ‘ \n ’ ;
}
15 42 26 7
Exercise
Enter the values in a matrix and print it in reverse Column order

[0] [1] [2] [2] [1] [0]


1 2 3 3 2 1
4 5 6 6 5 4
7 8 9 9 8 7
Transpose of a Matrix

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

temp = a [ row ] [ col ] ;


a [ row ] [ col ] = a [ col ] [ row ] ;
a [ col ] [ row ] = temp ;

You might also like