SlideShare a Scribd company logo
Data Structure using C++
Lecturer
Majid Hamid Ali
2020 - 2021
Tikrit University
Collageof ComputerScienceandMathematics
Arrays
Arrays
1. One-Dimensional Arrays
2. Accessing Array Elements
3. Representation of Arrays in Memory
4. Example: Finding the Maximum
5. No Array-to-Array Assignments
NEED FOR AN ARRAY
 To store large number of variables of same type under a
single variable.
 Easy understanding of the program.
 E.g.
To store Marks of 50 students.
Record of sales of 100 salesman.
Represent a Linear Array in memory
 The elements of linear array are stored in consecutive
memory locations. It is shown below:
WHAT IS AN ARRAY
 An array is a derived data type ( derived from
fundamental data type )
 It is a collection of variables of the same type that are
referenced by a common name.
 Consist of contiguous memory locations.
 Lowest address corresponds to first element
 Highest address corresponds to the last element.
 Can have data items of type like: int, char, float and
also user-defined types like : structures, objects.
Accessing Array Elements
 An individual element within an array is
accessed by use of an index.
 An index describes the position of an
element within an array.
 Note: In C++ the first element has the
index zero!
Representation of Arrays in Memory
In C++, any array is mapped to a contiguous
memory location.
All memory elements reside next to each
other.
One-Dimensional Arrays
A one-dimensional array is a list of related variables.
The general form of a one-dimensional array declaration
is:
Data type Array_name [size];
• Data type: base type of the array,
determines the data type of each element in the array
• size: how many elements the array will hold
• variable_name: the name of the array
Examples:
int sample[10];
float float_numbers[100];
char last_name[40];
Data structure  array
Memory Representation of Single Dimension
Array
 If the array is float arr [ 5 ];
memory representation would be as follows:
Arr [ 0 ] Arr [ 1 ] Arr [ 2 ] Arr [ 3 ] Arr [ 4 ]
5016
5012
5008
5004
5000
Total Memory requirement is : size of ( type ) * size of array 4*5 = 20 bytes
ARRAY INITIALISATION
int list [ 5 ] ; // declaration
int list [ 5 ] = { 10, 20, 30, 40, 50 } ;
// declaration & initialization
UNSIZED ARRAY INITIALISATION
 Can skip the size of an array in array initialization
 Elements of an array can be added or removed
without changing array dimensions.
E.g.
float price [ ] = { 50.5, 63.97, 84.6, 779.8 };
Data structure  array
for ( i = 0 ; i < 10 ; i + + )
{
cout << a [ i ] << endl;
}
// display the 10 elements of the array
// replay.cpp
// gets four ages from user, displays them
#include <iostream>
using namespace std;
int main()
{
int age[4]; //array ‘age’ of 4 ints
for(int j=0; j<4; j++) //get 4 ages
{
cout << “Enter an age: “;
cin >> age[j]; //access array element
}
for(j=0; j<4; j++) //display 4 ages
cout << “You entered “ << age[j] << endl;
return 0;
}
Example:
int a[8];
int j;
for(j=0; j<8; j++)
a[j] = 7-j;
Then the memory representation of array a
looks like this:
Example: Finding the Maximum
#include <iostream.h>
int main()
{
int i, max = 0;
int list[100];
// initialize the array with random values
for(i=0; i<100; i++)
list[i] = rand();
// find maximum value
for(i=0; i<100; i++)
if(max < list[i])
max = list[i];
cout << “Maximum value: “ << max;
return(0);
}
No Array-to-Array Assignments
You cannot assign one array to another in C++.
The following is illegal:
int a[10], b[10];
// do something
// assign all elements of array b to array a
a = b; // error -- illegal
Instead, you have to do the assignments for each
element:
int i;
// assign all elements of array b to array a
for(i=0; i<10; i++)
a[i] = b[i];
For example, you can compile and run the
following program, even though the array crash
is being overrun:
// An incorrect program. Do not execute!
int main()
{
int crash[10], i;
for(i=0; i<100; i++) crash[i] = i;
return(1);
}
Creating an Array
void main( )
{
int a[10]; // declaration of an array ‘a’
int n;
// input 10 elements in an array
for ( n = 0; n < 10 ; n + +)
{
cin >> a [ n ];
}
// display the 10 elements of the array input
for ( n = 0 ; n < 10 ; n + + )
{
cout << a [ n ] << endl;
}
}
Program to count the no. of employees earning more than Rs. 1 lakh per
annum. Monthly salaries of 10 employees are given.
void main ( )
{
const int size = 10 ;
float sal [ size ] , an_sal ;
int count = 0;
// loop to accept monthly salaries of 10 employees
for ( int j = 0 ; j < size ; j + + )
{
cout << “ Enter the monthly salary of employee “ << j + 1 ;
cin >> sal [ j ];
}
// loop to count employees earning more than Rs. 1 lakh per annum
for ( j = 0 ; j < size ; j + + )
{
an_sal = sal [ j ] * 12 ;
if ( an_sal > 100000 )
{
count ++ ;
}
}
cout << count << “ employees out of “ << size << “ employees are
earning more than Rs. 1 lakh per annum “ ;
}
WAP to input 10 numbers in an array and replace all even
no.s by 0 and odd no.s by 1
void main ( )
{
int a [ 10 ], n;
// loop to accept 10 values in an array ‘a’
for ( n = 0; n < 10 ; n + +)
{
cin >> a [ n ];
}
// loop to check if the element of an array is even replace by 0
// and if odd replace by 1
for ( n = 0; n < 10 ; n + +)
{
if ( ( a [ n ] % 2 ) == 0 )
{
a [ n ] = 0;
}
else
{
a [ n ] = 1 ;
} } }
WAP to find the largest and smallest no. in an array of 10 elements
// input an array
// display the array
// to find the largest element
int largest = a [ 0 ] ;
for ( int i = 1 ; i < 10 ; i + + )
{
if ( a [ i ] > largest )
{
largest = a [ i ];
}
}
cout << “ largest value is : “ << largest ;
WAP to find the largest and smallest no. in an array of 10 elements
// input an array
// display the array
// to find the lowest element
int lowest = a [ 0 ];
for ( n = 1 ; n < 10 ; n + + )
{
if ( a [ n ] < lowest )
{
lowest = a [ n ];
}
}
cout << “ lowest value is : “ << lowest ;
Two-Dimensional Arrays
A two-dimensional array is a list of one-dimensional arrays.
To declare a two-dimensional integer array two_dim of size 10,20 we
would write:
int matrix[3][4];
This corresponds to a table with 3 rows and 4 columns (for example).
We can generate the array above by using
this program:
#include <iostream.h>
int main()
{
int row=3, col=4;
int matrix[row][col];
for(row=0; row < 3; ++row) {
for(col=0; col < 4; ++col) {
matrix[row][col] = (row*4)+ col +1;
cout << matrix[row][col] << ‘ ‘;
}
cout << ‘n’;
}
return(0);}
Memory Allocation for Two-Dimensional Arrays
 Storage for all array elements is determined at compile time.
 The memory used to hold an array is required the entire time that
the array is in existence.
 The following formula determines the number of bytes of memory
that will be allocated:
bytes = rows * columns * number_of_bytes_in_type
 For example, an integer array (with two-byte integers) with
dimensions 100,100 would require 100 * 100 * 2 = 20,000 bytes.
Aggregate Operations
Aggregate operations on arrays are not allowed in C++. Given
int arrayOne[20];
int arrayTwo[20];
arrayOne = arrayTwo; // assignment - not allowed
if(arrayOne == arrayTwo) // comparison - not allowed
cout << arrayOne; // output - not allowed (except C-strings)
cin >> arrayOne; // input - not allowed (except C-strings)
arrayTwo = arrayTwo - arrayOne; // arithmetic - not allowed
return arrayOne; // returning an entire array - not allowed
SomeFunc(arrayTwo); // pass as an argument to a function - allowed
Advantages of Array:
 It is used to represent multiple data items of same
type by using single name.
 It can be used to implement other data structures
like linked lists, stacks, queues, tree, graphs etc.
 Two-dimensional arrays are used to represent
matrices.
 Many databases include one-dimensional arrays
whose elements are records.
Disadvantages of Array
 We must know in advance the how many elements are to be
stored in array.
 Array is static structure. It means that array is of fixed size.
The memory which is allocated to array cannot be increased
or decreased.
 Array is fixed size; if we allocate more memory than
requirement then the memory space will be wasted.
 The elements of array are stored in consecutive memory
locations. So insertion and deletion are very difficult and
time consuming.
Accessing to data
1.Sequential.
2.Computed address.
3.Linked address.
Sequential
 This method is slow because the access to record take
long time
 sequential reading from first record to last record
especially when the file size is large
 the record to be searched at the end of the file.
Computed address
One dimension array
LOC( A[I] ) = LO + I
One dimension array
Ex :- find location of A[4] from array has size (20) where base
address = 100?
LOC( A[I] ) = LO + I
LOC( A[4] ) = 100 + 4
=100+4
=104
Tow dimension
Row wise method
LOC( A[I] [J] ) = LO + N * I + J
B. Colum wise method
LOC( A[I] [J] ) = LO + M * J + I
Tow dimension
• LO = Base address
• I = Row subscript of element whose address is
to be found
• J = Column subscript of element whose
address is to be found
• M = Number of row of the given matrix
• N = Number of column of the given matrix
Tow dimension
Ex :- find location of ( A[1] [2]) from matrix is stored in
col_ wise method where A array
A [3] [3] and base address=500?
LOC( A[I] [J] ) = LO +M*J + I
( A[1] [2])= 500+3*2+1
= 500+7
= 507
Three dimension array
int z[n1] [n2] [n3]
A. Row wise method
LOC( z[i1] [i2] [i3] ) = LO + n2 * n3 * i1 + n3 * i2 + i3
B. Colum wise method
LOC(z[i1] [i2] [i3]) = LO + i1 + n1 * i2 + n1 * n2 * i3
Three dimension array
Let X : box[9][6] [8] of integer
Compute the location of the element box[5][3][6] using Row-wise and
column- wise when the base address is 1200.
n1= 9 n2= 6 n3= 8 i1= 5 i2= 3 i3= 6
A.Row wise method
LOC( box[i1] [i2] [i3] ) = LO + n2 * n3 * i1 + n3 * i2 + i3
LOC( box[5] [3] [6] ) = 1200 + 6 * 8 * 5 + 8 * 3 + 6
= 1200+ 240 + 24+6
= 1470
Three dimension array
B. Colum wise method
LOC(z[i1] [i2] [i3]) = LO + i1 + n1 * i2 + n1 * n2 * i3
LOC( box[5] [3] [6] ) = 1200 + 5 + 9* 3 + 9 * 6 * 6
= 1200+5 + 27+324
= 1556
n1= 9 n2= 6 n3= 8 i1= 5 i2= 3 i3= 6

More Related Content

What's hot (20)

PPT
Abstract data types
Poojith Chowdhary
 
PPT
Unit 1 introduction to data structure
kalyanineve
 
PPTX
Array operations
ZAFAR444
 
PPTX
Arrays In C++
Awais Alam
 
PDF
UNIT I LINEAR DATA STRUCTURES – LIST
Kathirvel Ayyaswamy
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PDF
Queues
Hareem Aslam
 
PPTX
Binary Heap Tree, Data Structure
Anand Ingle
 
PPTX
Binary search
AparnaKumari31
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PPTX
Recursion in Data Structure
khudabux1998
 
PPT
Lec 17 heap data structure
Sajid Marwat
 
PPTX
Presentation on array
topu93
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPT
Queue
Nabeel Ahsen
 
PPT
Binary tree
Rajendran
 
PPTX
Abstract Data Types
karthikeyanC40
 
PPTX
Insertion sort
almaqboli
 
Abstract data types
Poojith Chowdhary
 
Unit 1 introduction to data structure
kalyanineve
 
Array operations
ZAFAR444
 
Arrays In C++
Awais Alam
 
UNIT I LINEAR DATA STRUCTURES – LIST
Kathirvel Ayyaswamy
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Queues
Hareem Aslam
 
Binary Heap Tree, Data Structure
Anand Ingle
 
Binary search
AparnaKumari31
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Recursion in Data Structure
khudabux1998
 
Lec 17 heap data structure
Sajid Marwat
 
Presentation on array
topu93
 
Priority Queue in Data Structure
Meghaj Mallick
 
Binary tree
Rajendran
 
Abstract Data Types
karthikeyanC40
 
Insertion sort
almaqboli
 

Similar to Data structure array (20)

PPTX
Arrays_in_c++.pptx
MrMaster11
 
PPT
Fp201 unit4
rohassanie
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PDF
Arrays and library functions
Swarup Boro
 
PPT
Arrays and vectors in Data Structure.ppt
mazanali7145
 
PPTX
Arrays
Neeru Mittal
 
PPTX
arrays.pptx
NehaJain919374
 
DOCX
Array assignment
Ahmad Kamal
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PDF
1-Intoduction ------------- Array in C++
ab6399671
 
PPTX
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
PPTX
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
PPTX
arrays.pptx
HarmanShergill5
 
PPTX
Array
PralhadKhanal1
 
PPT
Lec2&3 data structure
Ibrahim El-Torbany
 
PPTX
Arrays in C++
Kashif Nawab
 
Arrays_in_c++.pptx
MrMaster11
 
Fp201 unit4
rohassanie
 
ARRAYS.pptx
MamataAnilgod
 
Arrays and library functions
Swarup Boro
 
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Arrays
Neeru Mittal
 
arrays.pptx
NehaJain919374
 
Array assignment
Ahmad Kamal
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
1-Intoduction ------------- Array in C++
ab6399671
 
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
arrays.pptx
HarmanShergill5
 
Lec2&3 data structure
Ibrahim El-Torbany
 
Arrays in C++
Kashif Nawab
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Ad

Data structure array

  • 1. Data Structure using C++ Lecturer Majid Hamid Ali 2020 - 2021 Tikrit University Collageof ComputerScienceandMathematics
  • 3. Arrays 1. One-Dimensional Arrays 2. Accessing Array Elements 3. Representation of Arrays in Memory 4. Example: Finding the Maximum 5. No Array-to-Array Assignments
  • 4. NEED FOR AN ARRAY  To store large number of variables of same type under a single variable.  Easy understanding of the program.  E.g. To store Marks of 50 students. Record of sales of 100 salesman.
  • 5. Represent a Linear Array in memory  The elements of linear array are stored in consecutive memory locations. It is shown below:
  • 6. WHAT IS AN ARRAY  An array is a derived data type ( derived from fundamental data type )  It is a collection of variables of the same type that are referenced by a common name.  Consist of contiguous memory locations.  Lowest address corresponds to first element  Highest address corresponds to the last element.  Can have data items of type like: int, char, float and also user-defined types like : structures, objects.
  • 7. Accessing Array Elements  An individual element within an array is accessed by use of an index.  An index describes the position of an element within an array.  Note: In C++ the first element has the index zero!
  • 8. Representation of Arrays in Memory In C++, any array is mapped to a contiguous memory location. All memory elements reside next to each other.
  • 9. One-Dimensional Arrays A one-dimensional array is a list of related variables. The general form of a one-dimensional array declaration is: Data type Array_name [size]; • Data type: base type of the array, determines the data type of each element in the array • size: how many elements the array will hold • variable_name: the name of the array Examples: int sample[10]; float float_numbers[100]; char last_name[40];
  • 11. Memory Representation of Single Dimension Array  If the array is float arr [ 5 ]; memory representation would be as follows: Arr [ 0 ] Arr [ 1 ] Arr [ 2 ] Arr [ 3 ] Arr [ 4 ] 5016 5012 5008 5004 5000 Total Memory requirement is : size of ( type ) * size of array 4*5 = 20 bytes
  • 12. ARRAY INITIALISATION int list [ 5 ] ; // declaration int list [ 5 ] = { 10, 20, 30, 40, 50 } ; // declaration & initialization
  • 13. UNSIZED ARRAY INITIALISATION  Can skip the size of an array in array initialization  Elements of an array can be added or removed without changing array dimensions. E.g. float price [ ] = { 50.5, 63.97, 84.6, 779.8 };
  • 15. for ( i = 0 ; i < 10 ; i + + ) { cout << a [ i ] << endl; } // display the 10 elements of the array
  • 16. // replay.cpp // gets four ages from user, displays them #include <iostream> using namespace std; int main() { int age[4]; //array ‘age’ of 4 ints for(int j=0; j<4; j++) //get 4 ages { cout << “Enter an age: “; cin >> age[j]; //access array element } for(j=0; j<4; j++) //display 4 ages cout << “You entered “ << age[j] << endl; return 0; }
  • 17. Example: int a[8]; int j; for(j=0; j<8; j++) a[j] = 7-j; Then the memory representation of array a looks like this:
  • 18. Example: Finding the Maximum #include <iostream.h> int main() { int i, max = 0; int list[100]; // initialize the array with random values for(i=0; i<100; i++) list[i] = rand(); // find maximum value for(i=0; i<100; i++) if(max < list[i]) max = list[i]; cout << “Maximum value: “ << max; return(0); }
  • 19. No Array-to-Array Assignments You cannot assign one array to another in C++. The following is illegal: int a[10], b[10]; // do something // assign all elements of array b to array a a = b; // error -- illegal Instead, you have to do the assignments for each element: int i; // assign all elements of array b to array a for(i=0; i<10; i++) a[i] = b[i];
  • 20. For example, you can compile and run the following program, even though the array crash is being overrun: // An incorrect program. Do not execute! int main() { int crash[10], i; for(i=0; i<100; i++) crash[i] = i; return(1); }
  • 21. Creating an Array void main( ) { int a[10]; // declaration of an array ‘a’ int n; // input 10 elements in an array for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // display the 10 elements of the array input for ( n = 0 ; n < 10 ; n + + ) { cout << a [ n ] << endl; } }
  • 22. Program to count the no. of employees earning more than Rs. 1 lakh per annum. Monthly salaries of 10 employees are given. void main ( ) { const int size = 10 ; float sal [ size ] , an_sal ; int count = 0; // loop to accept monthly salaries of 10 employees for ( int j = 0 ; j < size ; j + + ) { cout << “ Enter the monthly salary of employee “ << j + 1 ; cin >> sal [ j ]; }
  • 23. // loop to count employees earning more than Rs. 1 lakh per annum for ( j = 0 ; j < size ; j + + ) { an_sal = sal [ j ] * 12 ; if ( an_sal > 100000 ) { count ++ ; } } cout << count << “ employees out of “ << size << “ employees are earning more than Rs. 1 lakh per annum “ ; }
  • 24. WAP to input 10 numbers in an array and replace all even no.s by 0 and odd no.s by 1 void main ( ) { int a [ 10 ], n; // loop to accept 10 values in an array ‘a’ for ( n = 0; n < 10 ; n + +) { cin >> a [ n ]; } // loop to check if the element of an array is even replace by 0 // and if odd replace by 1 for ( n = 0; n < 10 ; n + +) { if ( ( a [ n ] % 2 ) == 0 ) { a [ n ] = 0; } else { a [ n ] = 1 ; } } }
  • 25. WAP to find the largest and smallest no. in an array of 10 elements // input an array // display the array // to find the largest element int largest = a [ 0 ] ; for ( int i = 1 ; i < 10 ; i + + ) { if ( a [ i ] > largest ) { largest = a [ i ]; } } cout << “ largest value is : “ << largest ;
  • 26. WAP to find the largest and smallest no. in an array of 10 elements // input an array // display the array // to find the lowest element int lowest = a [ 0 ]; for ( n = 1 ; n < 10 ; n + + ) { if ( a [ n ] < lowest ) { lowest = a [ n ]; } } cout << “ lowest value is : “ << lowest ;
  • 27. Two-Dimensional Arrays A two-dimensional array is a list of one-dimensional arrays. To declare a two-dimensional integer array two_dim of size 10,20 we would write: int matrix[3][4]; This corresponds to a table with 3 rows and 4 columns (for example).
  • 28. We can generate the array above by using this program: #include <iostream.h> int main() { int row=3, col=4; int matrix[row][col]; for(row=0; row < 3; ++row) { for(col=0; col < 4; ++col) { matrix[row][col] = (row*4)+ col +1; cout << matrix[row][col] << ‘ ‘; } cout << ‘n’; } return(0);}
  • 29. Memory Allocation for Two-Dimensional Arrays  Storage for all array elements is determined at compile time.  The memory used to hold an array is required the entire time that the array is in existence.  The following formula determines the number of bytes of memory that will be allocated: bytes = rows * columns * number_of_bytes_in_type  For example, an integer array (with two-byte integers) with dimensions 100,100 would require 100 * 100 * 2 = 20,000 bytes.
  • 30. Aggregate Operations Aggregate operations on arrays are not allowed in C++. Given int arrayOne[20]; int arrayTwo[20]; arrayOne = arrayTwo; // assignment - not allowed if(arrayOne == arrayTwo) // comparison - not allowed cout << arrayOne; // output - not allowed (except C-strings) cin >> arrayOne; // input - not allowed (except C-strings) arrayTwo = arrayTwo - arrayOne; // arithmetic - not allowed return arrayOne; // returning an entire array - not allowed SomeFunc(arrayTwo); // pass as an argument to a function - allowed
  • 31. Advantages of Array:  It is used to represent multiple data items of same type by using single name.  It can be used to implement other data structures like linked lists, stacks, queues, tree, graphs etc.  Two-dimensional arrays are used to represent matrices.  Many databases include one-dimensional arrays whose elements are records.
  • 32. Disadvantages of Array  We must know in advance the how many elements are to be stored in array.  Array is static structure. It means that array is of fixed size. The memory which is allocated to array cannot be increased or decreased.  Array is fixed size; if we allocate more memory than requirement then the memory space will be wasted.  The elements of array are stored in consecutive memory locations. So insertion and deletion are very difficult and time consuming.
  • 33. Accessing to data 1.Sequential. 2.Computed address. 3.Linked address.
  • 34. Sequential  This method is slow because the access to record take long time  sequential reading from first record to last record especially when the file size is large  the record to be searched at the end of the file.
  • 35. Computed address One dimension array LOC( A[I] ) = LO + I
  • 36. One dimension array Ex :- find location of A[4] from array has size (20) where base address = 100? LOC( A[I] ) = LO + I LOC( A[4] ) = 100 + 4 =100+4 =104
  • 37. Tow dimension Row wise method LOC( A[I] [J] ) = LO + N * I + J B. Colum wise method LOC( A[I] [J] ) = LO + M * J + I
  • 38. Tow dimension • LO = Base address • I = Row subscript of element whose address is to be found • J = Column subscript of element whose address is to be found • M = Number of row of the given matrix • N = Number of column of the given matrix
  • 39. Tow dimension Ex :- find location of ( A[1] [2]) from matrix is stored in col_ wise method where A array A [3] [3] and base address=500? LOC( A[I] [J] ) = LO +M*J + I ( A[1] [2])= 500+3*2+1 = 500+7 = 507
  • 40. Three dimension array int z[n1] [n2] [n3] A. Row wise method LOC( z[i1] [i2] [i3] ) = LO + n2 * n3 * i1 + n3 * i2 + i3 B. Colum wise method LOC(z[i1] [i2] [i3]) = LO + i1 + n1 * i2 + n1 * n2 * i3
  • 41. Three dimension array Let X : box[9][6] [8] of integer Compute the location of the element box[5][3][6] using Row-wise and column- wise when the base address is 1200. n1= 9 n2= 6 n3= 8 i1= 5 i2= 3 i3= 6 A.Row wise method LOC( box[i1] [i2] [i3] ) = LO + n2 * n3 * i1 + n3 * i2 + i3 LOC( box[5] [3] [6] ) = 1200 + 6 * 8 * 5 + 8 * 3 + 6 = 1200+ 240 + 24+6 = 1470
  • 42. Three dimension array B. Colum wise method LOC(z[i1] [i2] [i3]) = LO + i1 + n1 * i2 + n1 * n2 * i3 LOC( box[5] [3] [6] ) = 1200 + 5 + 9* 3 + 9 * 6 * 6 = 1200+5 + 27+324 = 1556 n1= 9 n2= 6 n3= 8 i1= 5 i2= 3 i3= 6