0% found this document useful (0 votes)
119 views40 pages

Unit 2 Complete

An array is a linear data structure that stores a collection of data items of the same type in contiguous memory locations. Each item in an array has an index, which identifies its location. One-dimensional arrays store elements in a single sequence, while multi-dimensional arrays have multiple indices to access elements. Arrays provide efficient access to elements by index and allow storing and accessing a large number of homogeneous data items.

Uploaded by

Divyanshu Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views40 pages

Unit 2 Complete

An array is a linear data structure that stores a collection of data items of the same type in contiguous memory locations. Each item in an array has an index, which identifies its location. One-dimensional arrays store elements in a single sequence, while multi-dimensional arrays have multiple indices to access elements. Arrays provide efficient access to elements by index and allow storing and accessing a large number of homogeneous data items.

Uploaded by

Divyanshu Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Concept of Arrays:

 An Array is a Linear data structure which is a collection of data items having


similar data types stored in contiguous memory locations.
 By knowing the address of the first item we can easily access all
items/elements of an array.

Array Properties :

 Linear data structure


 Homogeneous
 Contiguous Memory
Memory Representation of array
Array Basics
Array Index: The location of an element in an array has an index,
which identifies the element. Array index starts from 0.

Array element: Items stored in an array is called an element. The


elements can be accessed via its index.

Array Length: The length of an array is defined based on the number


of elements an array can store. In the above example, array length is
6 which means that it can store 6 elements.
Array Basics

• When an array of size and type is declared, the compiler allocates enough memory to hold all
elements of data.
• For example, an array face [10] will have 10 elements with index starting from 0 to 9 and the
memory allocated contiguously will be 20 bytes. The compiler knows the address of the first byte
of the array only. Also, the address of the first byte is considered as the memory address for the
whole array.
• Normal variables (a1, a2, a3, ….) can be used when we have a small number of elements, but if
we want to store a large number of elements, it becomes difficult to manage them with normal
variables. Representing many elements with one variable name is the basic idea of arrays.
Types of Arrays

The various types of arrays are as follows.

One dimensional array


Multi-dimensional array

One-Dimensional Array

A one-dimensional array is also called a single dimensional array where the


elements will be accessed in sequential order. This type of array will be accessed by
the indexes (starting from 0, A[i]) or array name (*(A+i)).
Multi-Dimensional Array

When the number of dimensions specified is more than one, then it is called as a
multi-dimensional array. Multidimensional arrays include 2D arrays and 3D arrays.

Example 1: Storing marks of all students of a class , define 1D Array as


float Marks[N], where N are total students in a class.

Example 2: Storing marks of different CAs of all the students of a class, define
2D Array as
float Marks[M][N], where M represent no. of CAs and N is no. of students
Two Dimensional Array
A two-dimensional array will be accessed by using the
subscript of row and column index.

For traversing the two dimensional array, the value of the rows
and columns will be considered.

Example: In the two-dimensional array arr [3] [4], the first


index specifies the number of rows and the second index
specifies the number of columns and the array can hold 12
elements (3 * 4).
// A sample program for Array Declaration and Display
#include <iostream>
using namespace std;
int main()
{
int A [5] = {10,20,30,40,50}; // declaration of 1D array
int B [2][2] = {{10,20}, {30,40}}; //declaration of 2D array
for(int i =0;i<5;i++){
cout<< A[i]<<“ ”;
}
for(int i =0; i<2; i++){
for(int j=0; j<2; j++){
cout<< B[i][j]<<“ ”;
}
cout<<endl;
}
return 0;
}
Array Pointer
• An array name is a constant pointer to the first element of the array.

for ( int i = 0; i < 5; i++ ) {


cout << "*(p + " << i << ") : ";
#include <iostream> cout << *(p + i) << endl;
using namespace std; }
cout << "Array values using array
int main () { as address " << endl;
int arr[5] = {1,2,3,4,5};
int *p; for ( int i = 0; i < 5; i++ ) {
cout << "*(arr+ " << i << ") : ";
p = arr; cout << *(arr + i) << endl;
cout << "Array values using pointer " << endl; }
return 0;
}
Array Pointer
• An array name is a constant pointer to the first element of the array.

for ( int i = 0; i < 5; i++ ) {


cout << "*(p + " << i << ") : ";
#include <iostream> cout << *(p + i) << endl;
using namespace std; }
cout << "Array values using array
int main () { as address " << endl;
int arr[5] = {1,2,3,4,5};
int *p; for ( int i = 0; i < 5; i++ ) {
cout << "*(arr+ " << i << ") : ";
p = arr; cout << *(arr + i) << endl;
cout << "Array values using pointer " << endl; }
return 0;
}
Matrix(2 D Array) operations
• Addition
• Subtraction
• Multiplication
• Sum of diagonal elements matrix
Hands On Exercise 1
• A chocolate factory is packing chocolates into the packets. The
chocolate packets here represent an array of N number of integer
values. The task is to find the empty packets(0) of chocolate and push
it to the end of the conveyor belt(array).
• Example 1 :
• N=7 and arr = [4,5,0,1.9,0,5,0].
• There are 3 empty packets in the given set. These 3 empty packets
represented as O should be pushed towards the end of the array
Exercise 2
• A supermarket maintains a pricing format for all its products. A value
N is printed on each product. When the scanner reads the value N on
the item, the product of all the digits in the value N is the price of the
item. The task here is to design the software such that given the code
of any item N the product (multiplication) of all the digits of value
should be computed(price).
Memory Representation of Array (1 D
Array)
• As array are contiguous, so they are represented in contiguous
memory locations.
• If we know the base address of the array we can easily figure out the
address of any location of the index of the array.
• Location arr[k] = BaseLocation(&arr[0]) + sizeof(arr[0]) x k
int A[5];
A[0] = 1;
for (int i = 1; i< 5; i++)
{
A[i] = A[i-1]* 2;
}
Memory Representation of Array (2 D
Array)
• Let a be a two dimensional m x n array. Though a is pictured as a
rectangular pattern with m rows and n columns, it is represented in
memory by a block of m*n sequential memory locations.

• However the sequence can be stored in two different ways:


Column Major Order
Row Major Order
Memory Representation of Array (2 D
Array)
Column Major Order
• In the column major order, the elements are stored column by column. First
column, second column and so on
• For int number[3][12]; column major order would look like:

• Address calculation in column major order


• loc a[i][j] = base(a) + w(m х j + i)

where base(a) is base address of array a, m is number of rows in array a, n is


number of column in array, w is the number of bytes per storage location for one
element of the array.
Memory Representation of Array (2 D
Array)
Row Major Order
• In row majaor order the elements area stored row by row. First row,
second row and so on.
For int number[3][2]; Row major order will look like:

• Address calculation in row major order


• loc a[i][j] = base (a) + w (n x i +j)
where base(a) is base address of array a, m is number of rows in
array a, n is number of column in array, w is the number of bytes per
storage location for one element of the array.
Operations on Array:

The basic operations supported by an array are:

•Traverse − print all the array elements one by one.


•Insertion − Adds an element at the given index.
•Deletion − Deletes an element at the given index.
•Search − Searches an element using the given index or by the value.
•Update − Updates an element at the given index.
•Concatenation- Concatenate two Arrays
•Merging – Merging of two Arrays
Insertion Operation
Inserting one or more data elements into an array
is known as Insert operation. A new element is
either added at beginning, end or at any given
index of array, based on the requirement.
It is better understood with the practical
implementation of insertion operation, where the
data is added at the end of the array.
Algorithm
Let Array be a linear unordered array of MAX elements.
Let LA be a Linear Array (unordered) with N elements and K is
a positive integer such that K<=N. Following is the algorithm
where ITEM is inserted into the Kth position of LA −
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
Example
Following is the implementation of the above algorithm −

#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n ");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
printf("The array elements after insertion : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
}
When the above program is compiled and executed, the output
produced is as follows:
Output

The original array elements are :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after insertion :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 10
LA[4] = 7
LA[5] = 8
Deletion Operation
To remove an existing element from the array
and to re-organize the other elements of an
array, is known as Deletion operation.
Algorithm
Consider LA is a linear array with N elements and K is
a positive integer such that K<=N. Following is the
algorithm to delete an element available at the
Kthposition of LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J-1] = LA[J]
5. Set J = J+1
6. Set N = N-1
7. Stop
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
j = k;
while( j < n) {
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
}
When the above program is compiled and executed, the output
produced is as follows:
Output

The original array elements are :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after deletion :
LA[0] = 1
LA[1] = 3
LA[2] = 7
LA[3] = 8
Search Operation
A search is performed for an array element based on its
value or its index.

Algorithm
Consider LA is a linear array with N elements and K is
a positive integer such that K<=N. Following is the
algorithm to find an element with a value of ITEM
using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −

#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
while( j < n){
if( LA[j] == item ) {
break;
}
j = j + 1;
}
printf("Found element %d at position %d \n", item, j+1);
}
When the above program is compiled and executed, the output
produced is as follows:
Output

The original array elements are :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
Update Operation
Updating of an existing element from the array at a given
index is known as Update Operation.

Algorithm
Consider LA is a linear array with N elements and K is a
positive integer such that K<=N. Following is the
algorithm to update an element available at the Kth position
of LA.
Algorithm
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −

#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
LA[k-1] = item;
printf("The array elements after updation : \n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n ", i, LA[i]);
}
}
When the above program is compiled and executed, the output
produced is as follows:
Output

The original array elements are :


LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8
Merge Operation
Combining two arrays into a single array is known as merging two
arrays. For example, if the first array has 5 elements and the second
array has 4, the resultant array will have 9 elements. A merged array is
the result of this process.
Merge Operation

Algorithm
Consider LA1 and LA2 are two given arrays having N1 and N2 elements, respectively. Merge
these two arrays in a new array LA3 having size N1 + N2.

1. Start
2. Input the length N1 and N2 of the arrays LA1 and LA2 .
3. Input the arrays elements from user.
4. Create a merged array LA3 and set length as N1 + N2.
5. Copy the elements of the first array to the merged array LA3.
6. Append the elements of the second array to the merged array LA3.
7. Display the merged array.
8. Stop
Merge Operation : Example
#include <stdio.h>
int main()
{
int N1 = 5, N2 = 5, N3, i, j;
int LA1[5] = { 1, 2, 3, 4, 5 };
int LA2[5] = { 6, 7, 8, 9, 10 };
N3 = N1 + N2;
int LA3[N3];
// copying array 1 elements into an array
for (i = 0; i < N1; i++) {
LA3[i] = LA1[i];
}
// copying array 2 elements into an array
for (i = 0, j = N1; j < N3 && i < N2; i++, j++) {
LA3[j] = LA2[i];
}
// Array Elements After Merging
for (i = 0; i < N3; i++) {
printf("%d ", LA3[i]);
}
return 0;
}
Thanks

You might also like