SlideShare a Scribd company logo
ARRAYS
Sunawar Khan Ahsan
MS(CS)
IIUI
ARRAYS & Matrices
SK Ahsan
WHAT IS ARRAYS?
 An array is a group of consecutive memory locations
with same name and data type.
 Simple variable is a single memory location with unique
name and a type. But an Array is collection of different
adjacent memory locations. All these memory locations
have one collective name and type.
 The memory locations in the array are known as
elements of array.The total number of elements in the
array is called length.
 The elements of array is accessed with reference to its
position in array, that is call index or subscript.
ARRAYS & Matrices
SK Ahsan
WHAT IS AN ARRAY?
■ We refer to all stored values in an array by its
name
■ If we would like to access a particular value
stored in an array, we specify its index (i.e. its
position relative to the first array value)
– The first array index is always 0
– The second value is stored in index 1
– Etc.
ARRAYS & Matrices
SK Ahsan
WHAT IS AN ARRAYS?
■ Arrays
– Structures of related data items
– Static entity (same size throughout program)
■ A few types
– Pointer-based arrays (C-like)
– Arrays as objects (C++)
ARRAYS & Matrices
SK Ahsan
Advantages / Uses of Arrays
■ Arrays can store a large number of value with single name.
■ Arrays are used to process many value easily and quickly.
■ The values stored in an array can be sorted easily.
■ The search process can be applied on arrays easily.
ARRAYS & Matrices
SK Ahsan
Types of Arrays:
–One-Dimensional Array
–Two-Dimensional Array
–N-Dimensional Array
ARRAYS & Matrices
SK Ahsan
One-D Array
A type of array in which all elements are arranged in the form of a list is known as 1-D array or
single dimensional array or linear list.
Declaring 1-DArray:
data_type identifier[length]; e.g: int marks[5];
o Data _type: Data type of values to be stored in the array.
o Identifier: Name of the array.
o Length: Number of elements.
ARRAYS & Matrices
SK Ahsan
ARRAY INITIALIZATION
■ Initializing arrays
– For loop
■ Set each element
– Initializer list
■ Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
■ If not enough initializers, rightmost elements 0
■ If too many syntax error
– To set every element to same value
int n[ 5 ] = { 0 };
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
■ 5 initializers, therefore 5 element array
8
ARRAYS & Matrices
SK Ahsan
One-D array Intialization
The process of assigning values to array elements at the time of array declaration is called array
initialization.
Syntax:
data_type identifier[length]={ List of values }; e.g: int marks[5]={70,54,82,96,49};
o Data _type: Data type of values to be stored in the array.
o Identifier: Name of the array.
o Length: Number of elements
o List of values: Values to initialize the array. Initializing values must be constant
70 54 82 96 49
ARRAYS & Matrices
SK Ahsan
Accessing element of array
■ Individual Element:
Array_name[index];
■ Using Loop:
int marks[5];
for(int i=0;i<=4;i++)
marks[i]=i;
ARRAYS & Matrices
SK Ahsan
INITIALIZE AND ACCESS INDIVIDUAL INDEX
// This program displays the number of days in each month.
// It uses a 12-element int array.
#include <iostream.h>
void main(void)
{
int days[12];
days[0] = 31; // January
days[1] = 28; // February
days[2] = 31; // March
days[3] = 30; // April
days[4] = 31; // May
days[5] = 30; // June
days[6] = 31; // July
ARRAYS & Matrices
SK Ahsan
Cont…
days[7] = 31; // August
days[8] = 30; // September
days[9] = 31; // October
days[10] = 30; // November
days[11] = 31; // December
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.n";
}
}
ARRAYS & Matrices
SK Ahsan
Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.
ARRAYS & Matrices
SK Ahsan
PARTIAL ARRAY INITIALIZATION
// This program displays the number of days in each month.
// It uses a 12-element int array.
#include <iostream.h>
void main(void)
{
int days[12] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.n";
}
}
ARRAYS & Matrices
SK Ahsan
OUTPUT
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.
15
ARRAYS & Matrices
SK Ahsan
Print ASCIIValues
// This program uses an array of ten characters to store the
// first ten letters of the alphabet. The ASCII codes of the
// characters are displayed.
#include <iostream.h>
void main(void)
{
char letters[10] = {'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J'};
cout << "Character" << "t" << "ASCII Coden";
cout << "--------" << "t" << "----------n";
for (int count = 0; count < 10; count++)
{
cout << letters[count] << "tt";
cout << int(letters[count]) << endl;
}
}
16
ARRAYS & Matrices
SK Ahsan
Output
Character ASCII Code
--------- ----------
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
ARRAYS & Matrices
SK Ahsan
// Histogram printing program.
#include <iostream>
#include <iomanip>
using std::setw;
int main() {
const int arraySize = 10;
int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
cout << "Element" << setw( 13 ) << "Value"<< setw( 17 ) << "Histogram"<< endl;
// for each element of array n, output a bar in histogram
for ( int i = 0; i < arraySize; i++ ) {
cout << setw( 7 ) << i << setw( 13 )<< n[ i ] << setw( 9 );
for ( int j = 0; j < n[ i ]; j++ ) // print one bar
cout << '*';
cout << endl; // start next line of output
} // end outer for structure
return 0; // indicates successful termination
} // end main
PRINT HISTOGRAM
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
ARRAYS & Matrices
SK Ahsan
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
float a[] = { 22.2,44.4, 66.6 };
float x=11.1;
cout << "I m going to Crash " << endl;
cin >> x;
a[3333] = 88.8; // ERROR: index is out of bounds!
system("PAUSE"); return 0;
}
ARRAY OUT OF BOUND
ARRAYS & Matrices
SK Ahsan
Searching In Array
Searching is a process of finding the required data in the array. Searching becomes more
important when the length of the array is very large.
There are two techniques to searching elements in array as follows:
■ Sequential search
■ Binary search
ARRAYS & Matrices
SK Ahsan
Sequential Search
Sequential search is also known as linear or serial search. It follows the following step to search a
value in array.
– Visit the first element of array and compare its value with required value.
– If the value of array matches with the desired value, the search is complete.
– If the value of array does not match, move to next element an repeat same process.
ARRAYS & Matrices
SK Ahsan
Binary Search
Binary search is a quicker method of searching for value in the array. Binary search is very quick but
it can only search an sorted array. It cannot be applied on an unsorted array.
o It locates the middle element of array and compare with desired number.
o If they are equal, search is successful and the index of middle element is returned.
o If they are not equal, it reduces the search to half of the array.
o If the search number is less than the middle element, it searches the first half of array.
Otherwise it searches the second half of the array.The process continues until the required
number is found or loop completes without successful search.
ARRAYS & Matrices
SK Ahsan
Sorting Arrays
Sorting is a process of arranging the value of array in a particular order. An array can be sorted
in two order.
o Ascending Order
o DescendingOrder
12 25 33 37 48
48 37 33 25 12
ARRAYS & Matrices
SK Ahsan
Techniques Of Sorting Array
There are two techniques of sorting array:
o Selection Sort
o Bubble Sort
ARRAYS & Matrices
SK Ahsan
Selection Sort
Selection sort is a technique that sort an array. It selects an element in the array and moves it
to its proper position. Selection sort works as follows:
1. Find the minimum value in the list.
2. Swap it with value in the first position.
3. Sort the remainder of the list excluding the first value.
ARRAYS & Matrices
SK Ahsan
Bubble Sort
Bubble Sort is also known as exchange sort. It repeatedly visits the array and compares two
items at a time. It works as follows:
o Compare adjacent element. If the first is greater than the second, swap them.
o Repeat this for each pair of adjacent element, starting with the first two and ending
with the last two. (at this point last element should be greatest).
o Repeat the step for all elements except the last one.
o Keep repeating for one fewer element each time until there are no pairs to compare.
ARRAYS & Matrices
SK Ahsan
Two-D Arrays
Two-D array can be considered as table that consists of rows and columns. Each element in 2-D array
is refered with the help of two indexes. One index indicates row and second indicates the column.
Declaring 2-D Array:
Data_type Identifier[row][column];
e.g: int arr[4][3];
o Data _type: Data type of values to be stored in the array.
o Identifier: Name of the array.
o Rows : # of Rows in the table of array.
o Column : # of Columns in the table of array.
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
ARRAYS & Matrices
SK Ahsan
Two-D array Intialization
The two-D array can also be initialized at the time of declaration. Initialization is performed by assigning
the initial values in braces seperated by commas.
Some important points :
o The elements of each row are enclosed within braces and seperated by comma.
o All rows are enclosed within braces.
o For number arrays, if all elements are not specified , the un specified elements are initialized by
zero.
ARRAYS & Matrices
SK Ahsan
Two-D array Intialization
Syntax:
int arr[4][3]={ {12,5,22},
{95,3,41},
{77,6,53},
{84,59,62} }
12 5 22
95 3 41
77 6 53
84 59 62
Column indexes
Row indexes
0
2
1
3
10 2
ARRAYS & Matrices
SK Ahsan
Case Study: Computing Mean, Median and Mode
Using Arrays
■ Mean
– Average (sum/number of elements)
■ Median
– Number in middle of sorted list
– 1, 2, 3, 4, 5 (3 is median)
– If even number of elements, take average of middle two
■ Mode
– Number that occurs most often
– 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
ARRAYS & Matrices
SK Ahsan
Case Study
■Find Set Properties
–Set Union
–Set Differences

More Related Content

What's hot

Array
ArrayArray
Array
PRN USM
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Arrays
ArraysArrays
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
Array
ArrayArray
Arrays in C
Arrays in CArrays in C
Arrays in C
Kamruddin Nur
 
Arrays
ArraysArrays
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Array in Java
Array in JavaArray in Java
Array in Java
Shehrevar Davierwala
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
primeteacher32
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
C arrays
C arraysC arrays

What's hot (20)

Array
ArrayArray
Array
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Set data structure
Set data structure Set data structure
Set data structure
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Arrays
ArraysArrays
Arrays
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Array
ArrayArray
Array
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Arrays
ArraysArrays
Arrays
 
Array ppt
Array pptArray ppt
Array ppt
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
2D Array
2D Array 2D Array
2D Array
 
C arrays
C arraysC arrays
C arrays
 

Viewers also liked

Fibre presentation1
Fibre presentation1Fibre presentation1
Fibre presentation1
Fibre-Tech-Swimming-Pools
 
Tax Season 2016
Tax Season 2016Tax Season 2016
Tax Season 2016
SSRS Market Research
 
3B-35G SIGINT School Certificate
3B-35G SIGINT School Certificate3B-35G SIGINT School Certificate
3B-35G SIGINT School CertificateZenie Noon
 
Favorite spring flower
Favorite spring flowerFavorite spring flower
Favorite spring flower
SSRS Market Research
 
Senior Recruitment AWNBG
Senior Recruitment AWNBGSenior Recruitment AWNBG
Senior Recruitment AWNBGSam Wilson
 
Metabolic basis of Muscular Fatigue
Metabolic basis of Muscular FatigueMetabolic basis of Muscular Fatigue
Metabolic basis of Muscular FatigueVisualBee.com
 
Pravin kantariya
Pravin kantariyaPravin kantariya
Pravin kantariya
Pravin Kantariya
 
AAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
AAPOR 2016 - Dutwin and Buskirk - Apples to OrangesAAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
AAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
SSRS Market Research
 
LF presentation
LF presentationLF presentation
LF presentation
Lem Francia
 
Interdisciplinary teaching
Interdisciplinary teachingInterdisciplinary teaching
Interdisciplinary teaching
Kerfoot Aaron
 
Watson Analytics Presentation
Watson Analytics PresentationWatson Analytics Presentation
Watson Analytics Presentation
fabianau
 

Viewers also liked (13)

Pkn
PknPkn
Pkn
 
Fibre presentation1
Fibre presentation1Fibre presentation1
Fibre presentation1
 
Tax Season 2016
Tax Season 2016Tax Season 2016
Tax Season 2016
 
3B-35G SIGINT School Certificate
3B-35G SIGINT School Certificate3B-35G SIGINT School Certificate
3B-35G SIGINT School Certificate
 
Resume_Mahesh
Resume_MaheshResume_Mahesh
Resume_Mahesh
 
Favorite spring flower
Favorite spring flowerFavorite spring flower
Favorite spring flower
 
Senior Recruitment AWNBG
Senior Recruitment AWNBGSenior Recruitment AWNBG
Senior Recruitment AWNBG
 
Metabolic basis of Muscular Fatigue
Metabolic basis of Muscular FatigueMetabolic basis of Muscular Fatigue
Metabolic basis of Muscular Fatigue
 
Pravin kantariya
Pravin kantariyaPravin kantariya
Pravin kantariya
 
AAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
AAPOR 2016 - Dutwin and Buskirk - Apples to OrangesAAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
AAPOR 2016 - Dutwin and Buskirk - Apples to Oranges
 
LF presentation
LF presentationLF presentation
LF presentation
 
Interdisciplinary teaching
Interdisciplinary teachingInterdisciplinary teaching
Interdisciplinary teaching
 
Watson Analytics Presentation
Watson Analytics PresentationWatson Analytics Presentation
Watson Analytics Presentation
 

Similar to Arrays

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptx
shivas379526
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Lists and arrays
Lists and arraysLists and arrays
Arrays
ArraysArrays
Array.pdf
Array.pdfArray.pdf
Array.pdf
DEEPAK948083
 
Data Structure
Data StructureData Structure
Data Structure
HarshGupta663
 
About Array
About ArrayAbout Array
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
heena94
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
11STEM2PGROUP1
 
Arrays
ArraysArrays
Arrays
sana younas
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 

Similar to Arrays (20)

Arrays
ArraysArrays
Arrays
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
arrayppt.pptx
arrayppt.pptxarrayppt.pptx
arrayppt.pptx
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Arrays
ArraysArrays
Arrays
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
Data Structure
Data StructureData Structure
Data Structure
 
About Array
About ArrayAbout Array
About Array
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Arrays
ArraysArrays
Arrays
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 

More from International Islamic University

Hash tables
Hash tablesHash tables
Binary Search Tree
Binary Search TreeBinary Search Tree
Graph 1
Graph 1Graph 1
Graph 2
Graph 2Graph 2
Graph 3
Graph 3Graph 3
Greedy algorithm
Greedy algorithmGreedy algorithm
Dynamic programming
Dynamic programmingDynamic programming
Quick sort
Quick sortQuick sort
Merge sort
Merge sortMerge sort
Linear timesorting
Linear timesortingLinear timesorting
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
International Islamic University
 
Data transmission
Data transmissionData transmission
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
International Islamic University
 
Sorting techniques
Sorting techniquesSorting techniques
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 

More from International Islamic University (20)

Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph 2
Graph 2Graph 2
Graph 2
 
Graph 3
Graph 3Graph 3
Graph 3
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Linear timesorting
Linear timesortingLinear timesorting
Linear timesorting
 
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
 
Lecture#4
Lecture#4Lecture#4
Lecture#4
 
Lecture#3
Lecture#3 Lecture#3
Lecture#3
 
Lecture#2
Lecture#2 Lecture#2
Lecture#2
 
Case study
Case studyCase study
Case study
 
Pcb
PcbPcb
Pcb
 
Data transmission
Data transmissionData transmission
Data transmission
 
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Arrays

  • 2. ARRAYS & Matrices SK Ahsan WHAT IS ARRAYS?  An array is a group of consecutive memory locations with same name and data type.  Simple variable is a single memory location with unique name and a type. But an Array is collection of different adjacent memory locations. All these memory locations have one collective name and type.  The memory locations in the array are known as elements of array.The total number of elements in the array is called length.  The elements of array is accessed with reference to its position in array, that is call index or subscript.
  • 3. ARRAYS & Matrices SK Ahsan WHAT IS AN ARRAY? ■ We refer to all stored values in an array by its name ■ If we would like to access a particular value stored in an array, we specify its index (i.e. its position relative to the first array value) – The first array index is always 0 – The second value is stored in index 1 – Etc.
  • 4. ARRAYS & Matrices SK Ahsan WHAT IS AN ARRAYS? ■ Arrays – Structures of related data items – Static entity (same size throughout program) ■ A few types – Pointer-based arrays (C-like) – Arrays as objects (C++)
  • 5. ARRAYS & Matrices SK Ahsan Advantages / Uses of Arrays ■ Arrays can store a large number of value with single name. ■ Arrays are used to process many value easily and quickly. ■ The values stored in an array can be sorted easily. ■ The search process can be applied on arrays easily.
  • 6. ARRAYS & Matrices SK Ahsan Types of Arrays: –One-Dimensional Array –Two-Dimensional Array –N-Dimensional Array
  • 7. ARRAYS & Matrices SK Ahsan One-D Array A type of array in which all elements are arranged in the form of a list is known as 1-D array or single dimensional array or linear list. Declaring 1-DArray: data_type identifier[length]; e.g: int marks[5]; o Data _type: Data type of values to be stored in the array. o Identifier: Name of the array. o Length: Number of elements.
  • 8. ARRAYS & Matrices SK Ahsan ARRAY INITIALIZATION ■ Initializing arrays – For loop ■ Set each element – Initializer list ■ Specify each element when array declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; ■ If not enough initializers, rightmost elements 0 ■ If too many syntax error – To set every element to same value int n[ 5 ] = { 0 }; – If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; ■ 5 initializers, therefore 5 element array 8
  • 9. ARRAYS & Matrices SK Ahsan One-D array Intialization The process of assigning values to array elements at the time of array declaration is called array initialization. Syntax: data_type identifier[length]={ List of values }; e.g: int marks[5]={70,54,82,96,49}; o Data _type: Data type of values to be stored in the array. o Identifier: Name of the array. o Length: Number of elements o List of values: Values to initialize the array. Initializing values must be constant 70 54 82 96 49
  • 10. ARRAYS & Matrices SK Ahsan Accessing element of array ■ Individual Element: Array_name[index]; ■ Using Loop: int marks[5]; for(int i=0;i<=4;i++) marks[i]=i;
  • 11. ARRAYS & Matrices SK Ahsan INITIALIZE AND ACCESS INDIVIDUAL INDEX // This program displays the number of days in each month. // It uses a 12-element int array. #include <iostream.h> void main(void) { int days[12]; days[0] = 31; // January days[1] = 28; // February days[2] = 31; // March days[3] = 30; // April days[4] = 31; // May days[5] = 30; // June days[6] = 31; // July
  • 12. ARRAYS & Matrices SK Ahsan Cont… days[7] = 31; // August days[8] = 30; // September days[9] = 31; // October days[10] = 30; // November days[11] = 31; // December for (int count = 0; count < 12; count++) { cout << "Month " << (count + 1) << " has "; cout << days[count] << " days.n"; } }
  • 13. ARRAYS & Matrices SK Ahsan Output Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days.
  • 14. ARRAYS & Matrices SK Ahsan PARTIAL ARRAY INITIALIZATION // This program displays the number of days in each month. // It uses a 12-element int array. #include <iostream.h> void main(void) { int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; for (int count = 0; count < 12; count++) { cout << "Month " << (count + 1) << " has "; cout << days[count] << " days.n"; } }
  • 15. ARRAYS & Matrices SK Ahsan OUTPUT Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days. 15
  • 16. ARRAYS & Matrices SK Ahsan Print ASCIIValues // This program uses an array of ten characters to store the // first ten letters of the alphabet. The ASCII codes of the // characters are displayed. #include <iostream.h> void main(void) { char letters[10] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}; cout << "Character" << "t" << "ASCII Coden"; cout << "--------" << "t" << "----------n"; for (int count = 0; count < 10; count++) { cout << letters[count] << "tt"; cout << int(letters[count]) << endl; } } 16
  • 17. ARRAYS & Matrices SK Ahsan Output Character ASCII Code --------- ---------- A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74
  • 18. ARRAYS & Matrices SK Ahsan // Histogram printing program. #include <iostream> #include <iomanip> using std::setw; int main() { const int arraySize = 10; int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; cout << "Element" << setw( 13 ) << "Value"<< setw( 17 ) << "Histogram"<< endl; // for each element of array n, output a bar in histogram for ( int i = 0; i < arraySize; i++ ) { cout << setw( 7 ) << i << setw( 13 )<< n[ i ] << setw( 9 ); for ( int j = 0; j < n[ i ]; j++ ) // print one bar cout << '*'; cout << endl; // start next line of output } // end outer for structure return 0; // indicates successful termination } // end main PRINT HISTOGRAM Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *
  • 19. ARRAYS & Matrices SK Ahsan #include <iostream> #include <stdlib.h> using namespace std; int main(){ float a[] = { 22.2,44.4, 66.6 }; float x=11.1; cout << "I m going to Crash " << endl; cin >> x; a[3333] = 88.8; // ERROR: index is out of bounds! system("PAUSE"); return 0; } ARRAY OUT OF BOUND
  • 20. ARRAYS & Matrices SK Ahsan Searching In Array Searching is a process of finding the required data in the array. Searching becomes more important when the length of the array is very large. There are two techniques to searching elements in array as follows: ■ Sequential search ■ Binary search
  • 21. ARRAYS & Matrices SK Ahsan Sequential Search Sequential search is also known as linear or serial search. It follows the following step to search a value in array. – Visit the first element of array and compare its value with required value. – If the value of array matches with the desired value, the search is complete. – If the value of array does not match, move to next element an repeat same process.
  • 22. ARRAYS & Matrices SK Ahsan Binary Search Binary search is a quicker method of searching for value in the array. Binary search is very quick but it can only search an sorted array. It cannot be applied on an unsorted array. o It locates the middle element of array and compare with desired number. o If they are equal, search is successful and the index of middle element is returned. o If they are not equal, it reduces the search to half of the array. o If the search number is less than the middle element, it searches the first half of array. Otherwise it searches the second half of the array.The process continues until the required number is found or loop completes without successful search.
  • 23. ARRAYS & Matrices SK Ahsan Sorting Arrays Sorting is a process of arranging the value of array in a particular order. An array can be sorted in two order. o Ascending Order o DescendingOrder 12 25 33 37 48 48 37 33 25 12
  • 24. ARRAYS & Matrices SK Ahsan Techniques Of Sorting Array There are two techniques of sorting array: o Selection Sort o Bubble Sort
  • 25. ARRAYS & Matrices SK Ahsan Selection Sort Selection sort is a technique that sort an array. It selects an element in the array and moves it to its proper position. Selection sort works as follows: 1. Find the minimum value in the list. 2. Swap it with value in the first position. 3. Sort the remainder of the list excluding the first value.
  • 26. ARRAYS & Matrices SK Ahsan Bubble Sort Bubble Sort is also known as exchange sort. It repeatedly visits the array and compares two items at a time. It works as follows: o Compare adjacent element. If the first is greater than the second, swap them. o Repeat this for each pair of adjacent element, starting with the first two and ending with the last two. (at this point last element should be greatest). o Repeat the step for all elements except the last one. o Keep repeating for one fewer element each time until there are no pairs to compare.
  • 27. ARRAYS & Matrices SK Ahsan Two-D Arrays Two-D array can be considered as table that consists of rows and columns. Each element in 2-D array is refered with the help of two indexes. One index indicates row and second indicates the column. Declaring 2-D Array: Data_type Identifier[row][column]; e.g: int arr[4][3]; o Data _type: Data type of values to be stored in the array. o Identifier: Name of the array. o Rows : # of Rows in the table of array. o Column : # of Columns in the table of array. 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2
  • 28. ARRAYS & Matrices SK Ahsan Two-D array Intialization The two-D array can also be initialized at the time of declaration. Initialization is performed by assigning the initial values in braces seperated by commas. Some important points : o The elements of each row are enclosed within braces and seperated by comma. o All rows are enclosed within braces. o For number arrays, if all elements are not specified , the un specified elements are initialized by zero.
  • 29. ARRAYS & Matrices SK Ahsan Two-D array Intialization Syntax: int arr[4][3]={ {12,5,22}, {95,3,41}, {77,6,53}, {84,59,62} } 12 5 22 95 3 41 77 6 53 84 59 62 Column indexes Row indexes 0 2 1 3 10 2
  • 30. ARRAYS & Matrices SK Ahsan Case Study: Computing Mean, Median and Mode Using Arrays ■ Mean – Average (sum/number of elements) ■ Median – Number in middle of sorted list – 1, 2, 3, 4, 5 (3 is median) – If even number of elements, take average of middle two ■ Mode – Number that occurs most often – 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
  • 31. ARRAYS & Matrices SK Ahsan Case Study ■Find Set Properties –Set Union –Set Differences