0% found this document useful (0 votes)
13 views

Arrays

Uploaded by

alwahabmalik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Arrays

Uploaded by

alwahabmalik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Array

Introduction

● Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations.
● It is a linear data structures where each data element can be randomly accessed by
using its index number.
● In C++, they are the derived data types that can store the primitive type of data such
as int, char, double, float, etc. For example, if we want to store the marks of a student
in 6 subjects, then we don't need to define a different variable for the marks in
different subjects. Instead, we can define an array that can store the marks in each
subject at the contiguous memory locations.
● In array, space complexity is O(n).
Properties of Array

● There are some of the properties of an array that are listed as follows -

○ Each element in an array is of the same data type and carries the same size
depending on the data type.
○ Elements in the array are stored at contiguous memory locations from which the
first element is stored at the smallest memory location.
○ Elements of the array can be randomly accessed.
Representation of an Array

● Index starts with 0.


● The array's length is 10, which means we can store 10 elements.
● Each element in the array can be accessed via its index.
Array Example

● // A character array in C++


char arr1[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘’};

● // An Integer array in C++


int arr2[] = {10, 20, 30, 40, 50};

● // A float array in C++


float arr3[]={30.1, 67.89, 34.78, 90.123};
Why are Arrays required?

● Arrays are useful because -

○ Sorting and searching a value in an array is easier.


○ Arrays are best to process multiple values quickly and easily.
○ Arrays are good for storing multiple values in a single variable - In computer
programming, most cases require storing a large number of data of a similar
type. To store such an amount of data, we need to define a large number of
variables. It would be very difficult to remember the names of all the variables
while writing the programs. Instead of naming all the variables with a different
name, it is better to define an array and store all the elements into it.
Assignment

Are there any other indexing techniques of array other than zero based indexing?

● If so write a detailed explanation on other techniques.


● What languages use other indexing techniques?
● Why zero-based indexing technique is mostly followed?

Assignment Deadline: 24th October, 2022


Traversal operation

Algorithm: C++:
#include <iostream>
Given an array of size n using namespace std;
Loop from i=0 to n: int main() {
print array element at index i int arr[5] = {18, 30, 15, 70, 12};
cout<<"Elements of the array are:\n";
Time Complexity: O(n) for(i = 0; i<5; i++) {
cout<<arr[i]<<" ";
}
}
Insertion Operation - Single Element

C++:
Algorithm:
#include <iostream>
using namespace std;
Given an index an array element
int main() {
Insert element at array[index]
int arr[5] = {18, 30, 15, 70, 12};
arr[ 2 ] = 10;
Time Complexity: O(1)
cout<<arr[ 2 ];
}
Insertion Operation - Rearranging

Algorithm
● Given an array of maximum size k, element, position, current array size n
● Loop from i=n to position
Assign array[i]=arr[i-1]
● array[position]=element
● Increment the value of n by 1

Time Complexity: O(n)


Insertion Operation - Rearranging

C++:
#include <iostream> n++;
using namespace std; cout<<"Array elements after insertion";
int main() for (i = 0; i < n; i++)
{ cout<<arr[i]<<" ";
int arr[20] = { 18, 30, 15, 70, 12 }; return 0;
int i, x, pos, n; }
n = 5; //current array size
x = 50; // element to be inserted
pos = 3; //position
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
Insertion Operation - All Elements

C++:
Algorithm:
#include <iostream>
using namespace std;
Declare an array of size n
int main() {
Loop from i=0 to n
int arr[5];
Insert element at index i
for(int i=0;i<5;i++){
cout<<"Enter Array Elements:";
cin>>arr[i];
Time Complexity: O(n)
}
}
Deletion Operation

Algorithm
● Given an array of maximum size k, element, position, current array size n
● Loop from i=position-1 to n
Assign array[i]=arr[i+1]
● Decrement the value of n by 1

Time Complexity: O(n)


Deletion Operation

C++ n--;
#include <iostream> cout<<"Array elements after insertion\n";
using namespace std; for (i = 0; i < n; i++)
int main() cout<<arr[i]<<" ";
{ return 0;
int arr[20] = { 18, 30, 15, 70, 12 }; }
int i, x, pos, n;
n = 5; //current array size
pos = 3; //position
for (i = pos-1; i < n; i++)
arr[i] = arr[i + 1];
Searching Operation

Algorithm
● Given an array of size n, element
● Loop from i=0 to n
Check array[i]==element
Show index i

Time Complexity: O(n)


Searching Operation

C++
#include <iostream>
using namespace std;
int main()
{
int arr[20] = { 18, 30, 15, 70, 12 };
int x=15;
for (i = 0; i < n; i++){
if(arr[i]==x)
cout<<"Element found at index:"<<i;
}
return 0;
}
2D Array

● It is a list of lists of the variable of the same data type.


● It also allows random access and all the elements can be accessed with the help of
their index.
● Its dimension can be increased from 2 to 3 and 4 so on. They all are referred to as a
multi-dimension array.
● The most common multidimensional array is a 2D array.
2D Array
1D Array vs 2D Array

Basis One Dimension Array Two Dimensional Array

Store a single list of the element of a similar Store a ‘list of lists’ of the element of a similar data
Definition
data type. type.

Represent multiple data items as a table consisting of


Representation Represent multiple data items as a list.
rows and columns.

The declaration varies for C++ The declaration varies for different programming
Declaration programming language: language:,
datatype variable_name[row]; datatype variable_name[row][column];
1D Array vs 2D Array

Basis One Dimension Array Two Dimensional Array

size of(datatype of the variable of the array) size of(datatype of the variable of the array)* the
Size(bytes)
* size of the array number of rows* the number of columns.

int arr[2][5]; //an array with two rows and five columns
int arr[5]; //an array with one row and five will be created.
Example columns will be created.
a b c d e
{a , b , c , d , e}
f g h i j

You might also like