Professional Documents
Culture Documents
Chapter 8
Chapter 8
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
Random Access
The number of values between braces { } can not be larger than the size
of array.
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;
}}}