Programming Fundamental (Array)
Programming Fundamental (Array)
C++ Arrays
11/10/2019
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
by
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: [email protected]
1
C++ Arrays
Programming Fundamentals by
11/10/2019
2
Consider this situation, you are taking a survey of 100 people and you have to
11/10/2019
store their age. To solve this problem in C++, you can create an integer array
having 100 elements.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
An array is a collection of data that holds fixed number of values of same type.
For example:
int age[100];
Here, the age array can hold maximum of 100 elements of integer type.
The size and type of arrays cannot be changed after its declaration.
3
How to declare an array in C++?
11/10/2019
For example,
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
float mark[5];
4
Elements of an Array and How to access
them?
You can access elements of an array by using indices.
11/10/2019
Suppose you declared an array mark as above. The first element is mark[0],
second element is mark[1] and so on.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
5
Few key notes:
Arrays have 0 as the first index not 1. In this example, mark[0] is the
11/10/2019
first element.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
used. In this example, mark[4] is the last element.
address, a[1], will be 2124d, address of a[2] will be 2128d and so on.
6
How to initialize an array in C++
programming?
11/10/2019
• int mark[5] = {19, 10, 8, 17, 9};Another method to initialize array
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
during declaration:
7
Here,
11/10/2019
mark[0] is equal to 19
mark[1] is equal to 10
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
8
How to insert and print array elements?
int mark[5] = {19, 10, 8, 17, 9}
11/10/2019
mark[3] = 9;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
cin >> mark[2];
#include <iostream>
using namespace std;
11/10/2019
int main()
{
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0; 10
}
Output
2
4
5
4
3
Sum = 18
Enter 5 numbers:
Programming Fundamentals by
11/10/2019
11
11/10/2019
Suppose you declared an array of 10 elements. Let's say,
int testArray[10];
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
You can use the array members from testArray[0] to testArray[9].
If you try to access array elements outside of its bound, let's say testArray[14];
the compiler may not show any error. However, this may cause unexpected
11/10/2019
array. For example:
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int x[3][4];Here, x is a two dimensional array. It can hold a maximum of 12
elements.
You can think this array as table with 3 rows and each row has 4 columns
as shown below.
13
Programming Fundamentals by
11/10/2019
14
11/10/2019
float x[2][4][3];
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
This array x can hold a maximum of 24 elements. You can think this
example as: Each of the 2 elements can hold 4 elements, which
makes 8 elements and each of those 8 elements can hold 3 elements.
Hence, total number of elements this array can hold is 24.
15
Multidimensional Array Initialization
11/10/2019
Initialization of two dimensional array
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int test[2][3] = {2, 4, -5, 9, 0, 9};
Better way to initialize this array with same array elements as above.
16
Initialization of three dimensional array
int test[2][3][4] =
11/10/2019
{3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int test[2][3][4] =
};
17
Example 1:
Two Dimensional Array C++ Program to display all elements
of an initialized two dimensional array.
#include <iostream> // nested for loops
using namespace std; for(int i = 0; i < 3; ++i)
11/10/2019
{
int main() for(int j = 0; j < 2; ++j)
{ {
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int test[3][2] = cout<< "test[" << i << "][" <<
{ j << "] = " << test[i][j] << endl;
{2, -5}, }
{4, 0}, }
{9, 1}
}; return 0;
}
// Accessing two dimensional array
using 18
Output
11/10/2019
test[0][0] = 2
test[0][1] = -5
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
19