SlideShare a Scribd company logo
2
Most read
8
Most read
9
Most read
Tareq Balhareth
Supersized by: Eng. Ibrahim Alodayni
 C++ provides a data structure, the array, which stores a fixed-size
sequential collection of elements of the same type. An array is used to
store a collection of data, but it is often more useful to think of an array
as a collection of variables of the same type. Instead of declaring
individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A
specific element in an array is accessed by an index. All arrays consist of
contiguous memory locations. The lowest address corresponds to the
first element and the highest address to the last element.
 To declare an array in C++, the programmer specifies the type of the elements and
the number of elements required by an array as follows:
This is called a single-dimension array. The arraySize must be an integer constant
greater than zero and type can be any valid C++ data type. For example, to declare a
10-element array called balance of type double, use this statement:
type arrayName [ arraySize ];
double balance[10];
 You can initialize C++ array elements either one by one or using a single statement
as follows:
The number of values between braces { } can not be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is an
example to assign a single element of the array.
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
 If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:
 You will create exactly the same array as you did in the previous example.
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
 The above statement assigns element number 5th in the array a value of 50.0. Array
with 4th index will be 5th, i.e., last element because all arrays have 0 as the index
of their first element which is also called base index. Following is the pictorial
representation of the same array we discussed above:
balance[4] = 50.0;
 An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example:
 The above statement will take 10th element from the array and assign the value to
salary variable. Following is an example, which will use all the above-mentioned
three concepts viz.
double salary = balance[9];
 Declaration, assignment and accessing arrays:
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main ()
{
int n[ 10 ]; // n is an array of 10 integers
// initialize elements of array n to 0
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100; // set element at location i to i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
 This program makes use of setw() function to format the output. When the above
code is compiled and executed, it produces the following result:
Element Value
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
 https://www.tutorialspoint.com/
C++ programming (Array)

More Related Content

What's hot (20)

PPT
Binary trees
Amit Vats
 
PPTX
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPT
Binary Search
kunj desai
 
PPSX
Stack
Seema Sharma
 
PPTX
Stressen's matrix multiplication
Kumar
 
PDF
Dbms 10: Conversion of ER model to Relational Model
Amiya9439793168
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PDF
Quick sort
Abdelrahman Saleh
 
PPTX
Basic Concept Of Database Management System (DBMS) [Presentation Slide]
Atik Israk
 
PPTX
Python array
Arnab Chakraborty
 
PPTX
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
PPTX
String handling in_java
774474
 
PPTX
Python: Basic Inheritance
Damian T. Gordon
 
PPTX
Queue - Data Structure - Notes
Omprakash Chauhan
 
PPT
03 mathematical anaylsis
Hira Gul
 
PDF
Array data structure
maamir farooq
 
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
PPTX
Arrays
Trupti Agrawal
 
PDF
Queue as data_structure
eShikshak
 
Binary trees
Amit Vats
 
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Dinive conquer algorithm
Mohd Arif
 
Binary Search
kunj desai
 
Stressen's matrix multiplication
Kumar
 
Dbms 10: Conversion of ER model to Relational Model
Amiya9439793168
 
linear search and binary search
Zia Ush Shamszaman
 
Quick sort
Abdelrahman Saleh
 
Basic Concept Of Database Management System (DBMS) [Presentation Slide]
Atik Israk
 
Python array
Arnab Chakraborty
 
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
String handling in_java
774474
 
Python: Basic Inheritance
Damian T. Gordon
 
Queue - Data Structure - Notes
Omprakash Chauhan
 
03 mathematical anaylsis
Hira Gul
 
Array data structure
maamir farooq
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Queue as data_structure
eShikshak
 

Viewers also liked (20)

PDF
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
PPTX
Array in c++
Mahesha Mano
 
PPT
Arrays
archikabhatia
 
PPTX
Array in c language
home
 
PPTX
Array in C
Kamal Acharya
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPT
02 c++ Array Pointer
Tareq Hasan
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PDF
CP Handout#2
trupti1976
 
PPT
8.3 program structure (1 hour)
akmalfahmi
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PDF
CP Handout#5
trupti1976
 
DOC
Java programming lab assignments
rajni kaushal
 
PPTX
intro to pointer C++
Ahmed Farag
 
PPTX
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
PPT
Apclass
geishaannealagos
 
PPT
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
PPT
Apclass (2)
geishaannealagos
 
PDF
Chapter 2 - Structure of C++ Program
Deepak Singh
 
PPT
Chapter 12 - File Input and Output
Eduardo Bergavera
 
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Array in c++
Mahesha Mano
 
Array in c language
home
 
Array in C
Kamal Acharya
 
C++ lecture 04
HNDE Labuduwa Galle
 
02 c++ Array Pointer
Tareq Hasan
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
CP Handout#2
trupti1976
 
8.3 program structure (1 hour)
akmalfahmi
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
CP Handout#5
trupti1976
 
Java programming lab assignments
rajni kaushal
 
intro to pointer C++
Ahmed Farag
 
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Apclass (2)
geishaannealagos
 
Chapter 2 - Structure of C++ Program
Deepak Singh
 
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Ad

Similar to C++ programming (Array) (20)

PDF
CP Handout#7
trupti1976
 
PDF
Array
hjasjhd
 
PDF
ARRAYS
muniryaseen
 
PPT
2 arrays
trixiacruz
 
PDF
Learn Java Part 8
Gurpreet singh
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Programming in c arrays
Uma mohan
 
PDF
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
PDF
Arrays-Computer programming
nmahi96
 
PPT
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Array
PreethyJemima
 
PPTX
Chapter 7.1
sotlsoc
 
PDF
Arrays
Steven Wallach
 
PDF
Introduction to Arrays in C
Thesis Scientist Private Limited
 
PPTX
Lecture 7
Mohammed Khan
 
PPT
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
CP Handout#7
trupti1976
 
Array
hjasjhd
 
ARRAYS
muniryaseen
 
2 arrays
trixiacruz
 
Learn Java Part 8
Gurpreet singh
 
Data structure array
MajidHamidAli
 
Programming in c arrays
Uma mohan
 
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
Arrays-Computer programming
nmahi96
 
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
Programming in c Arrays
janani thirupathi
 
Chapter 7.1
sotlsoc
 
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Lecture 7
Mohammed Khan
 
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
epi editorial commitee meeting presentation
MIPLM
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
infertility, types,causes, impact, and management
Ritu480198
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 

C++ programming (Array)

  • 1. Tareq Balhareth Supersized by: Eng. Ibrahim Alodayni
  • 2.  C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
  • 3.  To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows: This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement: type arrayName [ arraySize ]; double balance[10];
  • 4.  You can initialize C++ array elements either one by one or using a single statement as follows: The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array. double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
  • 5.  If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:  You will create exactly the same array as you did in the previous example. double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
  • 6.  The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representation of the same array we discussed above: balance[4] = 50.0;
  • 7.  An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:  The above statement will take 10th element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. double salary = balance[9];
  • 8.  Declaration, assignment and accessing arrays: #include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; }
  • 9.  This program makes use of setw() function to format the output. When the above code is compiled and executed, it produces the following result: Element Value 0 100 1 101 2 102 3 103 4 104 5 105 6 106 7 107 8 108 9 109