Professional Documents
Culture Documents
Amna Amjid f20-0020 Sec A Bs 1st Semester
Amna Amjid f20-0020 Sec A Bs 1st Semester
Arrays
Programming Fundamental
12/11/2020
Nice to meet you.
Name: Amna Amjid
BS-Software Engineering 1st Semester
The Universty Of Haripur
amnaamjid002@gmail.com
12/11/2020 2
Welcome!
Why we learn arrays and why it is
important in programming?
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
1 2 3 4 5
12/11/2020 Programming Fundamental
Concept Diagram
1 3 4 5
12/11/2020 Programming Fundamental
Understand Syntax of Arrays
1 2 3 4 5
12/11/2020 Programming Fundamental
02 Why arrays come in
programming ?
1 2 3 4
12/11/2020 Programming Fundamental
03 Scope & Objective
1 2 3 4 5
12/11/2020 Programming Fundamental
04 Arrays & Programming
1 2 3 4 5
12/11/2020 Programming Fundamental
04 But how we use arrays in
programming?
int main()
{
int balance[3] = { 300, 200, 100 };
for (int i = 0; i < 3; i++)
{
cout << "value of i: " << balance[i] << endl;
}
return 0;
}
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Types of arrays
1 2 3 4 5
12/11/2020 Programming Fundamental
05 One-Dimensional Array
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Syntax:
datatype array-name[size] ;
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Example
#include <iostream>
using namespace std;
int main()
{
int age[5] = { 19, 18, 21, 20, 17 };
for (int x = 0; x < 5; x++)
{
cout <<age[x]<<"\n";
}
}
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Two-Dimensional Array
A 2D array stores data in a list with 1-D array. It is
a matrix with rows and columns.
1 2 3 4
12/11/2020 Programming Fundamental
05 Syntax:
type array-Name [ x ][ y ];
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Example
#include <iostream>
using namespace std;
int main()
{
int a[3][2] = { {0, 2}, {1, 4}, {3, 7} };
for (int i=0; i<3; i++)
for (int j=0; j<2; j++)
{
cout << "a[" <<i<< "][" <<j<< "]: ";
cout << a[i][j] << endl;
}return 0;
}
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Three-Dimensional Array
A 3D array is an array of arrays. Each element in a
3D array is identified by a set of 3 indexes. To
access the elements of a 3D array, we use three for
loops.
1 2 3 4 5
12/11/2020 Programming Fundamental
05 Syntax:
1 2 3 4
12/11/2020 Programming Fundamental
05 Example
#include<iostream>
using namespace std;
int main()
{
int a[2][3][2] = {{{4, 8},{2, 4},{1, 6}}, {{3,
6},{5, 4},{9, 3}}};
cout << "a[0][1][0] = " << a[0][1][0] <<
"\n";
cout << "a[0][1][1] = " << a[0][1][1] <<
"\n";
return 0;
} 1 2 3 4 5
12/11/2020 Programming Fundamental
06 Comparision with Pointers
Arrays Pointers
Stores the value of the variable Store the address of the another
of homogeneous datatype. variable of same datatype as the
pointer variable's datatype.
A normal array stores values of
variable and pointer array stores Pointers are specially designed
the address of variables. to store the address of variables.
1 2 3 4 5
12/11/2020 Programming Fundamental
Application
• Maintains multiple
variable names using a
single name. Arrays
help to maintain large
data under a single
variable name. This
avoid the confusion of
using multiple variables.
1 2 3 4 5
12/11/2020 Programming Fundamental
Application
1 2 3 4 5
12/11/2020 Programming Fundamental
“Data dominates. If you've chosen
the right data structures and
organized things well, the
algorithms will almost always be
self-evident. Data structures, not
algorithms, are central to
programming.”
Rob Pike
12/11/2020 Programming Fundamental
Thank You!
Do you have any questions?
• Refrences
• www.differencebetween.net
www.faceprep.in/data-structures
www.guru99.com/array