SlideShare uma empresa Scribd logo
1 de 20
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

Mais conteúdo relacionado

Mais procurados

Array in c language
Array in c languageArray in c language
Array in c language
home
 

Mais procurados (20)

concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Python set
Python setPython set
Python set
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Python for loop
Python for loopPython for loop
Python for loop
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Python list
Python listPython list
Python list
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
2D Array
2D Array 2D Array
2D Array
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 

Semelhante a C++ Arrays

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
Week06
Week06Week06
Week06
hccit
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 

Semelhante a C++ Arrays (20)

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays
ArraysArrays
Arrays
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Arrays
ArraysArrays
Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
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
 
Week06
Week06Week06
Week06
 
Introduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptxIntroduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptx
 
arrayy.ppt
arrayy.pptarrayy.ppt
arrayy.ppt
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Array-part1
Array-part1Array-part1
Array-part1
 
Arrays
ArraysArrays
Arrays
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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