0% found this document useful (0 votes)
44 views14 pages

Chapter 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views14 pages

Chapter 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Faculty of Computer Science

Introduction to Programming

Lecturer: Lutfullah “Haqnesar”


Introduction to Programming

Chapter 8

Array
Introduction to Programming

Learning outcomes:
 Array
 Types array
 Advantages of Array
 Single Dimensional Array
 Multidimensional Array
Arrays
• A group of similar types of elements that have contiguous
memory location is called as Array.
• In C++, array index starts from 0 and end to n-1. We can store
only fixed set of elements in an array.
Advantages of Array

Code Optimization (less code)

Random Access

Easy to traverse data

Easy to manipulate data

Easy to sort data etc.


Types of Array
There are 2 types of arrays in C++ programming:

1. Single Dimensional Array


2. Multidimensional Array
1. Single Dimensional Array
 A One-Dimensional Array is the simplest form of an Array in
which the elements are stored linearly.
 The element of array can be accessed individually by
specifying the index value of each element stored in the array.
Declaring Arrays
 To declare an array, the programmer specifies the type of the elements
and the number of elements required by an array.
Syntax:
type ArrayName [ ArraySize ];
 The arraySize must be an integer constant greater than zero and type
can be any valid C++ data type.
For example:
int myarr[10];
Initializing Arrays
 We can initialize array elements either one by one or using a single
statement as follows −
int myarr[5] = {100, 2, 3, 17, 50};

 The number of values between braces { } can not be larger than the size
of array.

 Example to assign a single element of the array:


myarr[4] = 50;
Accessing Array Elements
 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
int salary = myarr[3];

 The above statement will take 10th element from the array and assign
the value to salary variable.
Example

#include <iostream>
using namespace std;
int main() {
int arr[5] ={10, 40, 20, 50, 30};
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
} }
Multidimensional Arrays
• The multidimensional array is also known as rectangular
arrays in C++.
• It can be two dimensional or three dimensional.
• The data is stored in tabular form (row ∗ column) which is
also known as matrix.
Example
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
{4, 0},
{9, 1}};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 2; ++j) {
cout << test[i][j] << endl;
}}}

You might also like