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

PPT
Command line arguments.21
PPTX
PPTX
PROGRAMMING LANGUAGE AND TYPES
PDF
Java Programming
PPT
How to execute a C program
PPTX
File handling in C
PPTX
Handling I/O in Java
PPTX
Linux Basic commands and VI Editor
Command line arguments.21
PROGRAMMING LANGUAGE AND TYPES
Java Programming
How to execute a C program
File handling in C
Handling I/O in Java
Linux Basic commands and VI Editor

What's hot (20)

PPTX
Express js
PPT
One dimensional 2
PPTX
C Programming Unit-5
PPTX
Arrays in java
PDF
Class and Objects in Java
PPTX
Java program structure
PPSX
ADO.NET
PPTX
[OOP - Lec 19] Static Member Functions
PDF
Golang 101
PDF
Shell scripting
PPT
C++ Arrays
PPTX
Iterarators and generators in python
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Java interface
PPTX
Introduction to kotlin
PPTX
Inheritance in java
PPTX
REST API
PPT
C# basics
PPTX
Control statements in c
Express js
One dimensional 2
C Programming Unit-5
Arrays in java
Class and Objects in Java
Java program structure
ADO.NET
[OOP - Lec 19] Static Member Functions
Golang 101
Shell scripting
C++ Arrays
Iterarators and generators in python
Python Programming Essentials - M31 - PEP 8
Java interface
Introduction to kotlin
Inheritance in java
REST API
C# basics
Control statements in c
Ad

Viewers also liked (20)

PDF
C++ ARRAY WITH EXAMPLES
PPTX
Array in c++
PPTX
Arrays In C++
PPT
PPTX
Array in c language
PPTX
Introduction to Array ppt
PPTX
Array in C
PPTX
C++ lecture 04
PPT
02 c++ Array Pointer
PPT
Chapter 13 - Inheritance and Polymorphism
PDF
CP Handout#2
PPT
8.3 program structure (1 hour)
PPTX
Constructs (Programming Methodology)
PDF
CP Handout#5
DOC
Java programming lab assignments
PPTX
intro to pointer C++
PPTX
Pf cs102 programming-9 [pointers]
PPT
PPT
C++ functions presentation by DHEERAJ KATARIA
PPT
Apclass (2)
C++ ARRAY WITH EXAMPLES
Array in c++
Arrays In C++
Array in c language
Introduction to Array ppt
Array in C
C++ lecture 04
02 c++ Array Pointer
Chapter 13 - Inheritance and Polymorphism
CP Handout#2
8.3 program structure (1 hour)
Constructs (Programming Methodology)
CP Handout#5
Java programming lab assignments
intro to pointer C++
Pf cs102 programming-9 [pointers]
C++ functions presentation by DHEERAJ KATARIA
Apclass (2)
Ad

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

PPTX
Array In C++ programming object oriented programming
PPTX
One dimensional arrays
PPTX
Chapter-Five.pptx
PDF
ARRAYS
PDF
Array Data Structure for programing language
PPTX
COM1407: Arrays
PPTX
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
PPTX
Data structure array
PPTX
Arrays_in_c++.pptx
PDF
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
PPTX
Module1_arrays.pptx
PPTX
Array and string in C++_093547 analysis.pptx
PPTX
Programming in c arrays
PPT
Fp201 unit4
PPTX
Lecture 9
PPT
Lecture#8 introduction to array with examples c++
PPTX
Lecture_01.2.pptx
PPT
PDF
PPTX
ARRAYS.pptx
Array In C++ programming object oriented programming
One dimensional arrays
Chapter-Five.pptx
ARRAYS
Array Data Structure for programing language
COM1407: Arrays
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
Data structure array
Arrays_in_c++.pptx
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Module1_arrays.pptx
Array and string in C++_093547 analysis.pptx
Programming in c arrays
Fp201 unit4
Lecture 9
Lecture#8 introduction to array with examples c++
Lecture_01.2.pptx
ARRAYS.pptx

Recently uploaded (20)

PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
advance database management system book.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
HVAC Specification 2024 according to central public works department
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
advance database management system book.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Journal of Dental Science - UDMY (2021).pdf
International_Financial_Reporting_Standa.pdf
Empowerment Technology for Senior High School Guide
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
Complications of Minimal Access-Surgery.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Environmental Education MCQ BD2EE - Share Source.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
HVAC Specification 2024 according to central public works department
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα

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