SlideShare a Scribd company logo
ARRAYS
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.
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.
TYPES OF ARRAYS Single Dimensional Array: Element specified by single subscript Syntax: type array_name [ size ] Base type of array Name of array No. of elements that can be stored: Can be a integer value without the sign
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; } }
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 };
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 ; } } }
// display the 10 elements of the array for ( i = 0 ; i < 10 ; i + + ) { cout << a [ i ] << endl; }
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 ;

More Related Content

What's hot (20)

PPTX
Arrays
Trupti Agrawal
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPT
1 D Arrays in C++
poonam.rwalia
 
PPTX
Functional Programming Advanced
OleksiyTereshchenko
 
PPT
Arrays
archikabhatia
 
PPTX
Array within a class
AAKASH KUMAR
 
PPTX
2- Dimensional Arrays
Education Front
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
PDF
Arrays
Shakila Mahjabin
 
PPTX
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
PDF
Lecture17 arrays.ppt
eShikshak
 
PDF
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
ODP
Beginning Scala Svcc 2009
David Pollak
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPTX
C programming slide c05
pradeep dwivedi
 
C++ lecture 04
HNDE Labuduwa Galle
 
Programming in c Arrays
janani thirupathi
 
1 D Arrays in C++
poonam.rwalia
 
Functional Programming Advanced
OleksiyTereshchenko
 
Array within a class
AAKASH KUMAR
 
2- Dimensional Arrays
Education Front
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
Lecture17 arrays.ppt
eShikshak
 
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Beginning Scala Svcc 2009
David Pollak
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Unit 6. Arrays
Ashim Lamichhane
 
C programming slide c05
pradeep dwivedi
 

Viewers also liked (17)

PDF
Better Search UX
Ravi Mynampaty
 
PDF
How we spiked the HBS water supply with Solr
Ravi Mynampaty
 
PDF
Citiscapes
miguelvaldivieso
 
PPTX
Linux
afzal pa
 
PDF
How We Incrementally Improved Search
Ravi Mynampaty
 
PDF
Go Creations A Profile
chasealive
 
PPT
I D
Marian Pike
 
PDF
Developing a Search & Findability Practice for the Enterprise
Ravi Mynampaty
 
PDF
Clustering Search Log Data
Ravi Mynampaty
 
PPT
Timers
afzal pa
 
PDF
Clustering as presented at UX Poland 2013
Ravi Mynampaty
 
PDF
wot pdf
guestfb6f46c
 
PDF
Trabajo Oscar
racso1687
 
PDF
What to Feed Your Search Engine: The Evolution of Search Analytics at HBS
Ravi Mynampaty
 
PDF
Building a Solr-driven Web Portal
Ravi Mynampaty
 
PDF
Findability Standards
Ravi Mynampaty
 
Better Search UX
Ravi Mynampaty
 
How we spiked the HBS water supply with Solr
Ravi Mynampaty
 
Citiscapes
miguelvaldivieso
 
Linux
afzal pa
 
How We Incrementally Improved Search
Ravi Mynampaty
 
Go Creations A Profile
chasealive
 
Developing a Search & Findability Practice for the Enterprise
Ravi Mynampaty
 
Clustering Search Log Data
Ravi Mynampaty
 
Timers
afzal pa
 
Clustering as presented at UX Poland 2013
Ravi Mynampaty
 
wot pdf
guestfb6f46c
 
Trabajo Oscar
racso1687
 
What to Feed Your Search Engine: The Evolution of Search Analytics at HBS
Ravi Mynampaty
 
Building a Solr-driven Web Portal
Ravi Mynampaty
 
Findability Standards
Ravi Mynampaty
 
Ad

Similar to Arrays (20)

PPTX
Data structure array
MajidHamidAli
 
PDF
C++ Course - Lesson 2
Mohamed Ahmed
 
PDF
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
DOCX
Array assignment
Ahmad Kamal
 
PPTX
Arrays
Neeru Mittal
 
PPT
Fp201 unit4
rohassanie
 
PPTX
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
PPTX
Arrays in C++
Kashif Nawab
 
PPT
2 arrays
trixiacruz
 
PDF
C++ L04-Array+String
Mohammad Shaker
 
PPT
arrayy.ppt
ssuserb82af5
 
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
PPT
Lecture#8 introduction to array with examples c++
NUST Stuff
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PPT
Chandan
Chandan Walia
 
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
PPTX
Arrays_in_c++.pptx
MrMaster11
 
PDF
Arrays and library functions
Swarup Kumar Boro
 
PPTX
Lecture 9
Mohammed Khan
 
PDF
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Data structure array
MajidHamidAli
 
C++ Course - Lesson 2
Mohamed Ahmed
 
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Array assignment
Ahmad Kamal
 
Arrays
Neeru Mittal
 
Fp201 unit4
rohassanie
 
Arrays matrix 2020 ab
Dr .Ahmed Tawwab
 
Arrays in C++
Kashif Nawab
 
2 arrays
trixiacruz
 
C++ L04-Array+String
Mohammad Shaker
 
arrayy.ppt
ssuserb82af5
 
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
Lecture#8 introduction to array with examples c++
NUST Stuff
 
ARRAYS.pptx
MamataAnilgod
 
Chandan
Chandan Walia
 
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
Arrays_in_c++.pptx
MrMaster11
 
Arrays and library functions
Swarup Kumar Boro
 
Lecture 9
Mohammed Khan
 
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Ad

Recently uploaded (20)

PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 

Arrays

  • 2. 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.
  • 3. 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.
  • 4. TYPES OF ARRAYS Single Dimensional Array: Element specified by single subscript Syntax: type array_name [ size ] Base type of array Name of array No. of elements that can be stored: Can be a integer value without the sign
  • 5. 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; } }
  • 6. 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
  • 7. ARRAY INITIALISATION int list [ 5 ] ; // declaration int list [ 5 ] = { 10, 20, 30, 40, 50 } ; // declaration & initialization
  • 8. 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 };
  • 9. 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 ]; }
  • 10. // 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 “ ; }
  • 11. 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 ; } } }
  • 12. // display the 10 elements of the array for ( i = 0 ; i < 10 ; i + + ) { cout << a [ i ] << endl; }
  • 13. 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 ;
  • 14. 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 ;