Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Introduction To

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?

12/11/2020 Programming Fundamental 3


How Computer Store
Information?

12/11/2020 Programming Fundamental


Data Structure

12/11/2020 Programming Fundamental


Agenda
Background Types of arrays
01 Who,why and when arrays 05 Explain types with examples
come in programming. in C++

Intoduction of Comparision with


arrays structure & pointer
02 Defination,concept of arrays 06 through chart
and why do we need arrays.

ADV, DisADV &


Objective of arrays Application of array
03 Short importance of arrays 07 Some of major adv and
disadvantages.

Array& programming Conclusion


04 Create& declare arrays in C+ 08 summary of all topic
+

12/11/2020 Programming Fundamental


12/11/2020 Programming Fundamental
01 Backround

John von Neumann wrote the first array-sorting


program (merge sort) in 1945, during the building
of the first stored-program computer.

12/11/2020 Programming Fundamental


01 Backround

Why arrays comes in programming ?

12/11/2020 Programming Fundamental


02 Indroduction to arrays

An array as a collection of variables of a similar


data type.
Instead of declaring each variable and assigning it
a value individually, you can declare one variable
(the array) and add the values of the various
variables to it.

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 ?

Arrays are best for storing multiple values in a


single variable.
Arrays are better at processing many values easily
and quickly.
Sorting and searching the values is easier in arrays.

1 2 3 4
12/11/2020 Programming Fundamental
03 Scope & Objective

The computer itself is nothing but a collection of


arrays of switches.

12/11/2020 Programming Fundamental


03 Scope & Objective

A byte is an array of 8 binary bits.


An integer (in many languages) is an array of 4
bytes.
A string is an array of characters (can be one or
two bytes per character depending on encoding).

1 2 3 4 5
12/11/2020 Programming Fundamental
04 Arrays & Programming

Suppose we need to store the marks of 50 students


in a class and calculate the average marks.
Declaring 50 separate variables will do the job but
no programmer would like to do so. And here
comes the array in action.

1 2 3 4 5
12/11/2020 Programming Fundamental
04 But how we use arrays in
programming?

12/11/2020 Programming Fundamental


04 Creating an Array in C+
+
#include <iostream>
using namespace std;

int main()
{
int balance[3] = { 300, 200, 100 };
for (int i = 0; i < 3; i++)
{
cout << "value of i: " << balance[i] << endl;
}
return 0;
}

12/11/2020 Programming Fundamental


Accessing an Array Element

1 2 3 4 5
12/11/2020 Programming Fundamental
05 Types of arrays

One dimensional Array


Multi-dimensional Array
Pointer to an Array

1 2 3 4 5
12/11/2020 Programming Fundamental
05 One-Dimensional Array

This is an array in which the data items are


arranged linearly in one dimension only. It is
commonly called a 1-D array.

1 2 3 4 5
12/11/2020 Programming Fundamental
05 Syntax:

The array-name is the name of the array.


The size is the number of items to be stored in the
array.

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:

The array-name is the name of the array.


The size is the number of items to be stored in the
array.
To declare a 2D array, use the following 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:

The array-name is the name of the array.


The size is the number of items to be stored in the
array.
To declare a 3D array, use the following syntax:

type array-Name [ x ][ y ][z];

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.

 An array can store the number


of elements, mentioned in the  A pointer variable can store the
size of array variable. address of only one variable at a
time.
1 2 3 4 5
12/11/2020 Programming Fundamental
Comparision with Structure
Arrays Structure
• An array is a collection of • A structure is a collection of
variables of same data type. variables of different data
type.
• Array elements are stored in
contiguous memory • Structure elements may not
location. be stored in a contiguous
memory location.
• Array elements are accessed
by their index number. • Structure elements are
accessed by their names.
• Array declaration and
element accessing operator • Structure element accessing
is "[ ]" (square bracket). operator is "." (Dot
operator).
12/11/2020 Programming Fundamental
07 Advantages & Disadvantages

12/11/2020 Programming Fundamental


Advantages

Arrays represent multiple data items


of the same type using a single name.

12/11/2020 Programming Fundamental


Advantages

In arrays, the elements can be


accessed randomly by using the
index number.

12/11/2020 Programming Fundamental


Advantages

Arrays allocate memory in


contiguous memory locations for all
its elements.

12/11/2020 Programming Fundamental


Disadvantages

The number of elements to be stored


in an array should be known in
advance.

12/11/2020 Programming Fundamental


Disadvantages

Insertion and deletion are quite


difficult in an array

12/11/2020 Programming Fundamental


Disadvantages

Allocating more memory than the


requirement leads to wastage of
memory space
12/11/2020 Programming Fundamental
1 2 3 4
12/11/2020 Programming Fundamental
Application

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

• Arrays can be used for


sorting data elements.
Different sorting
techniques like Bubble
sort, Insertion sort,
Selection sort etc use
arrays to store and sort
elements easily.

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?

12/11/2020 Programming Fundamental


Credits & Copyrights

Photos – many thanks to:


Freepik
Free font used:
Times New Roman

• Refrences
• www.differencebetween.net
www.faceprep.in/data-structures
www.guru99.com/array

12/11/2020 Programming Fundamental

You might also like