SlideShare a Scribd company logo
2
Most read
4
Most read
18
Most read
Arrays
AhmadMohammedSAlSubhi
EngIbraheemAlEdane
What Is An Array?
• An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
• Array Used to store a collection of elements (variables).
• That means that, for example, five values of type int can be
declared as an array without having to declare 5 different
variables (each with its own identifier). Instead, using an
array, the five int values are stored in contiguous memory
locations, and all five can be accessed using the same
identifier, with the proper index.
o int array [5];
8-2
Array int 0 1 2 3 4
Basic Array Definition
Type of
values in
list
BaseType Id [ SizeExp ] ;
Name
of list
Bracketed constant
expression
indicating number
of elements in list
double X [ 100 ] ;
// Subscripts are 0 through 99
Declaring an array
• The statement
int num[5];
declares an array num of 5 components of
the type int
• The components are num[0], num[1],
num[2], num[3], and num[4]
Array Storage in Memory
The definition
int tests[ISIZE]; // ISIZE is 5
allocates the following memory
8-5
Element 0 Element 1 Element 2 Element 3 Element 4
Processing One-Dimensional Arrays
• Some basic operations performed on a
one-dimensional array are:
– Initialize
– Input data
– Output data stored in an array
– Find the largest and/or smallest element
• Each operation requires ability to step
through the elements of the array
• Easily accomplished by a loop
Accessing Array Components (continued)
• Consider the declaration
int list[100]; //list is an array
//of the size 100
int i;
• This for loop steps through each element
of the array list starting at the first element
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
Accessing Array Components (continued)
• If processing list requires inputting data
into list
– The statement in Line 2 takes the from of
an input statement, such as the cin
statement
for (i = 0; i < 100; i++) //Line 1
cin >> list[i];
Array Initialization
• As with simple variables
– Arrays can be initialized while they are being declared
• When initializing arrays while declaring them
– Not necessary to specify the size of the array
• Size of array is determined by the number of initial values
in the braces
• For example:
double sales[] = {12.25, 32.50, 16.90, 23,
45.68};
Partial Initialization (continued)
• The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
• The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
– The first two components are initialized to 4 and 7
respectively
– All other components are initialized to 0
How To Declare Arrays
• To declare an array in C++, you should specify the
following things
– The data type of the values which will be stored in the array
– The name of the array (i.e. a C++ identifier that will be used to
access and update the array values)
– The dimensionality of the array:
• One dimensional (i.e. list of values ),
• Two-dimension array (a matrix or a table), etc.
– The size of each dimension
• Examples
– int x[10];int x[10]; // An integer array named x with size 10
– float GPA[30];float GPA[30]; // An array to store the GPA for 30 students
– int studentScores[30][5];int studentScores[30][5]; // A two-dimensional array to store the
scores of 5 exams for 30 students
One-dimensional Arrays
• We use one-dimensional (ID) arrays to store and
access list of data values in an easy way by
giving these values a common name, e.g.
int x[4];int x[4]; // all values are named x
x[0] = 10;x[0] = 10; // the 1st
value is 10
x[1] = 5;x[1] = 5; // the 2nd
value is 5
x[2] = 20;x[2] = 20; // the 3rd
value is 20
x[3] = 30;x[3] = 30; // the 4th
value is 30
10
5
20
30
x[0]
x[1]
x[2]
x[3]
Example: Read Values and Print
them in Reverse Order
#include <iomanip.h>
void main()
{
int x[5], index;
cout << “Please enter five integer values: “;
for( index=0; index<5; index++) cin >> x[index];
cout << “The values in reverse order are: “;
for (index=4; index>=0; index--)
cout << setw(3) << x[index];
cout << endl;
}
Two-Dimensional Arrays
• Two-dimensional arrays are stored in
row order
– The first row is stored first, followed by the
second row, followed by the third row and
so on
• When declaring a two-dimensional array
as a formal parameter
– can omit size of first dimension, but not the
second
• Number of columns must be specified
Example
• #include <iostream>
• using namespace std;
• int main()
• {
• int test[3][2] =
• {
• {2, -5},
• {4, 0},
• {9, 1}
• };
• // Accessing two dimensional array using
• // nested for loops
• for(int i = 0; i < 3; ++i)
• {
• for(int j = 0; j < 2; ++j)
• {
• cou t<< "test[" << i << "][" << j << "] = " << test[i][j] << endl;
• }
• }
• return 0;
• }
8-15
Multidimensional Arrays
• Multidimensional Array: collection of a fixed
number of elements (called components)
arranged in n dimensions (n >= 1)
• Also called an n-dimensional array
• General syntax of declaring an n-
dimensional array is:
dataType arrayName[intExp1][intExp2]...[intExpn];
where intExp1, intExp2, … are constant
expressions yielding positive integer values
Multidimensional Arrays (continued)
• The syntax for accessing a component of
an n-dimensional array is:
arrayName[indexExp1][indexExp2]...[indexExpn]
where indexExp1,indexExp2,..., and
indexExpn are expressions yielding
nonnegative integer values
• indexExpi gives the position of the array
component in the ith dimension
Example• #include <iostream>
• using namespace std;
• int main()
• {
• // This array can store upto 12 elements (2x3x2)
• int test[2][3][2];
• cout << "Enter 12 values: n";
•
• // Inserting the values into the test array
• // using 3 nested for loops.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k )
• {
• cin >> test[i][j][k];
• }
• }
• }
• cout<<"nDisplaying Value stored:"<<endl;
• // Displaying the values with proper index.
• for(int i = 0; i < 2; ++i)
• {
• for (int j = 0; j < 3; ++j)
• {
• for(int k = 0; k < 2; ++k)
• {
• cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl;
• }
• }
• }
• return 0;
• }
8-18
Summary
• An array is a structured data type with a fixed number of components
– Every component is of the same type
– Components are accessed using their relative positions in the array
• Elements of a one-dimensional array are arranged in the form of a list
• An array index can be any expression that evaluates to a non-negative
integer
• The value of the index must always be less than the size of the array
• The base address of an array is the address of the first array component
• In a function call statement, when passing an array as an actual
parameter, you use only its name
• As parameters to functions, arrays are passed by reference only
• A function cannot return a value of the type array
• In C++, C-strings are null terminated and are stored in character arrays
Resource
• https://www.programiz.com/cpp-programming/m
•
• https://www.tutorialspoint.com/cplusplus/cpp_nu
• http://www.cplusplus.com/doc/tutorial/arrays/
8-20

More Related Content

PPTX
Arrays In C++
Awais Alam
 
PPT
Multidimensional array in C
Smit Parikh
 
PPTX
Array
Anil Neupane
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
2D Array
Ehatsham Riaz
 
PPTX
Array in C
adityas29
 
PPTX
Array in C
Kamal Acharya
 
PDF
Arrays in C++
Maliha Mehr
 
Arrays In C++
Awais Alam
 
Multidimensional array in C
Smit Parikh
 
Constructors and destructors
Vineeta Garg
 
2D Array
Ehatsham Riaz
 
Array in C
adityas29
 
Array in C
Kamal Acharya
 
Arrays in C++
Maliha Mehr
 

What's hot (20)

PPTX
Array in c++
Mahesha Mano
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
Introduction to Array ppt
sandhya yadav
 
PPTX
Structures in c language
tanmaymodi4
 
PPTX
Pointer in c
lavanya marichamy
 
PPTX
Variables in C++, data types in c++
Neeru Mittal
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Templates in C++
Tech_MX
 
PPTX
Array Of Pointers
Sharad Dubey
 
PPTX
Presentation on array
topu93
 
PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
C Structures and Unions
Dhrumil Patel
 
PPTX
concept of Array, 1D & 2D array
Sangani Ankur
 
PPTX
C++ string
Dheenadayalan18
 
PPTX
Array in c language
home
 
PPT
Strings
Mitali Chugh
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Datatypes in c
CGC Technical campus,Mohali
 
Array in c++
Mahesha Mano
 
Pointers in c++
sai tarlekar
 
Introduction to Array ppt
sandhya yadav
 
Structures in c language
tanmaymodi4
 
Pointer in c
lavanya marichamy
 
Variables in C++, data types in c++
Neeru Mittal
 
Data types in c++
Venkata.Manish Reddy
 
Templates in C++
Tech_MX
 
Array Of Pointers
Sharad Dubey
 
Presentation on array
topu93
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Storage class in C Language
Nitesh Kumar Pandey
 
Programming in c Arrays
janani thirupathi
 
C Structures and Unions
Dhrumil Patel
 
concept of Array, 1D & 2D array
Sangani Ankur
 
C++ string
Dheenadayalan18
 
Array in c language
home
 
Strings
Mitali Chugh
 
Strings in C
Kamal Acharya
 
Ad

Similar to C++ Arrays (20)

PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PPTX
Arrays
Neeru Mittal
 
PPTX
Arrays_and_Strings_in_C_Programming.pptx
samreenghauri786
 
PPTX
Arrays_in_c++.pptx
MrMaster11
 
PPT
Chap 6 c++
Venkateswarlu Vuggam
 
PPTX
Array
Fahuda E
 
PDF
Chap 6 c++
Venkateswarlu Vuggam
 
PPT
Fp201 unit4
rohassanie
 
PPT
One dimensional 2
Rajendran
 
PPTX
Array,string structures. Best presentation pptx
Kalkaye
 
PDF
Arrays and library functions
Swarup Boro
 
PPTX
Arrays in C++
Kashif Nawab
 
PPTX
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
PPTX
ARRAYSCPP.pptx
727822TUCS116MOHANKU
 
PPT
Arrays
swathi reddy
 
PPTX
Arrays
Abdullahprince787
 
PPTX
Module1_arrays.pptx
HishamE1
 
PPTX
One dimensional arrays
Satyam Soni
 
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
ARRAYS.pptx
MamataAnilgod
 
Arrays
Neeru Mittal
 
Arrays_and_Strings_in_C_Programming.pptx
samreenghauri786
 
Arrays_in_c++.pptx
MrMaster11
 
Array
Fahuda E
 
Fp201 unit4
rohassanie
 
One dimensional 2
Rajendran
 
Array,string structures. Best presentation pptx
Kalkaye
 
Arrays and library functions
Swarup Boro
 
Arrays in C++
Kashif Nawab
 
"Understanding Arrays in Data Structures: A Beginners Guide."
saxenagarima2007
 
ARRAYSCPP.pptx
727822TUCS116MOHANKU
 
Arrays
swathi reddy
 
Module1_arrays.pptx
HishamE1
 
One dimensional arrays
Satyam Soni
 
Ad

Recently uploaded (20)

PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 

C++ Arrays

  • 2. What Is An Array? • An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. • Array Used to store a collection of elements (variables). • That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index. o int array [5]; 8-2 Array int 0 1 2 3 4
  • 3. Basic Array Definition Type of values in list BaseType Id [ SizeExp ] ; Name of list Bracketed constant expression indicating number of elements in list double X [ 100 ] ; // Subscripts are 0 through 99
  • 4. Declaring an array • The statement int num[5]; declares an array num of 5 components of the type int • The components are num[0], num[1], num[2], num[3], and num[4]
  • 5. Array Storage in Memory The definition int tests[ISIZE]; // ISIZE is 5 allocates the following memory 8-5 Element 0 Element 1 Element 2 Element 3 Element 4
  • 6. Processing One-Dimensional Arrays • Some basic operations performed on a one-dimensional array are: – Initialize – Input data – Output data stored in an array – Find the largest and/or smallest element • Each operation requires ability to step through the elements of the array • Easily accomplished by a loop
  • 7. Accessing Array Components (continued) • Consider the declaration int list[100]; //list is an array //of the size 100 int i; • This for loop steps through each element of the array list starting at the first element for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2
  • 8. Accessing Array Components (continued) • If processing list requires inputting data into list – The statement in Line 2 takes the from of an input statement, such as the cin statement for (i = 0; i < 100; i++) //Line 1 cin >> list[i];
  • 9. Array Initialization • As with simple variables – Arrays can be initialized while they are being declared • When initializing arrays while declaring them – Not necessary to specify the size of the array • Size of array is determined by the number of initial values in the braces • For example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
  • 10. Partial Initialization (continued) • The statement int list[] = {5, 6, 3}; declares list to be an array of 3 components and initializes list[0] to 5, list[1] to 6, and list[2] to 3 • The statement int list[25]= {4, 7}; declares list to be an array of 25 components – The first two components are initialized to 4 and 7 respectively – All other components are initialized to 0
  • 11. How To Declare Arrays • To declare an array in C++, you should specify the following things – The data type of the values which will be stored in the array – The name of the array (i.e. a C++ identifier that will be used to access and update the array values) – The dimensionality of the array: • One dimensional (i.e. list of values ), • Two-dimension array (a matrix or a table), etc. – The size of each dimension • Examples – int x[10];int x[10]; // An integer array named x with size 10 – float GPA[30];float GPA[30]; // An array to store the GPA for 30 students – int studentScores[30][5];int studentScores[30][5]; // A two-dimensional array to store the scores of 5 exams for 30 students
  • 12. One-dimensional Arrays • We use one-dimensional (ID) arrays to store and access list of data values in an easy way by giving these values a common name, e.g. int x[4];int x[4]; // all values are named x x[0] = 10;x[0] = 10; // the 1st value is 10 x[1] = 5;x[1] = 5; // the 2nd value is 5 x[2] = 20;x[2] = 20; // the 3rd value is 20 x[3] = 30;x[3] = 30; // the 4th value is 30 10 5 20 30 x[0] x[1] x[2] x[3]
  • 13. Example: Read Values and Print them in Reverse Order #include <iomanip.h> void main() { int x[5], index; cout << “Please enter five integer values: “; for( index=0; index<5; index++) cin >> x[index]; cout << “The values in reverse order are: “; for (index=4; index>=0; index--) cout << setw(3) << x[index]; cout << endl; }
  • 14. Two-Dimensional Arrays • Two-dimensional arrays are stored in row order – The first row is stored first, followed by the second row, followed by the third row and so on • When declaring a two-dimensional array as a formal parameter – can omit size of first dimension, but not the second • Number of columns must be specified
  • 15. Example • #include <iostream> • using namespace std; • int main() • { • int test[3][2] = • { • {2, -5}, • {4, 0}, • {9, 1} • }; • // Accessing two dimensional array using • // nested for loops • for(int i = 0; i < 3; ++i) • { • for(int j = 0; j < 2; ++j) • { • cou t<< "test[" << i << "][" << j << "] = " << test[i][j] << endl; • } • } • return 0; • } 8-15
  • 16. Multidimensional Arrays • Multidimensional Array: collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1) • Also called an n-dimensional array • General syntax of declaring an n- dimensional array is: dataType arrayName[intExp1][intExp2]...[intExpn]; where intExp1, intExp2, … are constant expressions yielding positive integer values
  • 17. Multidimensional Arrays (continued) • The syntax for accessing a component of an n-dimensional array is: arrayName[indexExp1][indexExp2]...[indexExpn] where indexExp1,indexExp2,..., and indexExpn are expressions yielding nonnegative integer values • indexExpi gives the position of the array component in the ith dimension
  • 18. Example• #include <iostream> • using namespace std; • int main() • { • // This array can store upto 12 elements (2x3x2) • int test[2][3][2]; • cout << "Enter 12 values: n"; • • // Inserting the values into the test array • // using 3 nested for loops. • for(int i = 0; i < 2; ++i) • { • for (int j = 0; j < 3; ++j) • { • for(int k = 0; k < 2; ++k ) • { • cin >> test[i][j][k]; • } • } • } • cout<<"nDisplaying Value stored:"<<endl; • // Displaying the values with proper index. • for(int i = 0; i < 2; ++i) • { • for (int j = 0; j < 3; ++j) • { • for(int k = 0; k < 2; ++k) • { • cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k] << endl; • } • } • } • return 0; • } 8-18
  • 19. Summary • An array is a structured data type with a fixed number of components – Every component is of the same type – Components are accessed using their relative positions in the array • Elements of a one-dimensional array are arranged in the form of a list • An array index can be any expression that evaluates to a non-negative integer • The value of the index must always be less than the size of the array • The base address of an array is the address of the first array component • In a function call statement, when passing an array as an actual parameter, you use only its name • As parameters to functions, arrays are passed by reference only • A function cannot return a value of the type array • In C++, C-strings are null terminated and are stored in character arrays