Unit 1 Arrays
Unit 1 Arrays
ARRAYS
BY :
MRS. DEEPTI A. CHAUDHARI
NEED OF ARRAYS :
• An array is a collection of similar data elements, it means it
can hold group of integers, group of characters etc.
• Suppose , we wish to read and store three integers, then one
may define three variables of integer type.
eg. Int a, int b, int c;
• Now if we wish to hold twenty integers , its difficult to define
twenty variables and to remember which variable is holding
what value , it is a big task for any programmer.
• So in this case, we think of using arrays.
Declaration of an Array :
• An array is declared as ,
datatype name [size];
Display integers using an
array
#include<stdio.h>
Void main()
{
int i, a[10];
clrscr();
for(i=0; i<5; i++)
{
printf(“Enter value %d : “, i+1);
scanf(“%d”, &a[i]);
printf(“\n”);
}
for(i =0; i<5; i++)
{
printf(“ value %d =%d\n”, i+1, a[i]);
}
getch();
}
Output :
Enter value 1 : 5
Enter value 2 : 6
Enter value 3 : 3
Enter value 4 : 4
Enter value 5 : 2
Value 1 = 5
Value 2 = 6
Value 3 = 3
Value 4 = 4
Value 5 = 2
Initialisation of ARRAY
• While using arrays we can accept values from user and we can
also initialised array when we declared it.
int a[5] = { 4, 7, 2, 11, 13 };
If we want to print square of given numbers in array :
printf(“ Numbers \t Square \n”);
For(i=0; i<5;i++)
{
Printf(“%d \t “, a[i]);
printf(“%d \n”, a[i] *a[i]);
}
Sum of array values
int I, sum =0, num[10];
Printf(“ enter the 10 array values”);
For(i=0; i<10;i++)
{
scanf(“%d”,&num[i]);
sum = num[i] + sum;
}
Printf(“ Sum of values of array is : %d”, sum);
Find maximum number in
array :
• STEP 1: START
• STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
• STEP 3: length= sizeof(arr)/sizeof(arr[0])
• STEP 4: max = arr[0]
• STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 i<length
• STEP 6: if(arr[i]>max)
max=arr[i]
• STEP 7: i=i+1.
• STEP 8: PRINT "Largest element in given array:" assigning max.
• STEP 9: RETURN 0
• STEP 9: END.
Passing arrays to Function
• Functions are the methods which are called from main ().
• Functions has prototype as
return data type func_name(arg1, arg2,..);
Function has to be declared in main () before it call.
Declaration is like :
int add(int a, int b);
If not returning a value use void datatype.
Continued……
• #include <stdio.h>
• #include <conio.h>
• void main()
• {
• int a[5]={10,5,45,12,19};
• int n=5,m;
• Int max(int a[],int n);
• clrscr();
• m=max(a,n);
• printf("\n MAXIMUM NUMBER IS %d", m);
• getch();
• }
• int max(int x[],int k)
• {
• int t, i;
• t = x[0];
• for(i=1; i<k; i++)
• {
• if(x[i]>t)
• t=x[i];
• }
• return(t);
• }
Inserting element in array
For inserting the element at required position, elements must be
moved downwards to new locations.
Insert element in an Array
int main()
{
int student[40],pos,i,size,value;
printf("enter no of elements in array of students:");
scanf("%d",&size);
printf("enter %d elements are:\n",size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is\n");
for(i=0;i<=size;i++)
printf("%d\n",student[i]);
return 0;
}
Algorithm to insert value in array :
1. (initialised the value of i] Set i= len
2. Repeat for i=len down to pos
( shift the elements down by 1 position)
set a[i+1] = a[i]
(End of loop)
3. Insert element at required position
set a[pos] =num;
4. (Reset len) Set len =len+1
5. Display the new list of arrays
6. End
Deleting an Element from array
Algorithm
1. Set item = a[pos]
2. Repeat for j = pos to n-1
[ shifting elements 1 position upwards]
set a[j] = a[j+1] (end of loop)
3. Reset n= n-1
4. Display the new list of element of arrays
5. End
Delete
int main()
element from array
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Resultant array:\n");
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}
return 0;
}
Hash Table
• Hash Table is a data structure which stores data in an associative manner.
• Hash Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be located from.
HASHING :
• Hashing is a technique to convert a range of key values into a range of indexes of
an array. We're going to use modulo operator to get a range of key values.
Consider an example of hash table of size 20, and the following items are to be
stored. Item are in the (key,value) format.
•(1,20)
•(2,70)
•(42,80)
•(4,25)
•(12,44)
•(14,32)
•(17,11)
Here we can see the key for particular values •(13,78)
•(37,98)
Indexing of Hash Table
Sr. no key Hash Array index
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
Linear Probing
As we can see, it may happen that the hashing technique is used to create an
already used index of the array. In such a case, we can search the next empty location
in the array by looking into the next cell until we find an empty cell. This technique
is called linear probing.
After Linear Probing, Array
Sr.No. Key Hash Array Index
Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18
Hash Collision
When the hash function generates the same index for multiple keys, there will be
a conflict (what value to be stored in that index). This is called a hash collision.
We can resolve the hash collision using one of the following techniques.
Collision resolution by chaining Open Addressing: Linear/Quadratic Probing and
Double Hashing.
1. Collision resolution by chaining
In chaining, if a hash function produces the same index for multiple elements,
these elements are stored in the same index by using a doubly-linked list.
If j is the slot for multiple elements, it contains a pointer to the head of the list of
elements. If no element is present, j contains NIL.
Open Addressing
Unlike chaining, open addressing doesn't store multiple elements into the same slot.
Here, each slot is either filled with a single key or left NIL.
1.Linear Probing
Refer slide no 17
2. Quadratic Probing
It works similar to linear probing but the spacing between the slots is increased
(greater than one) by using the following relation.
h(k, i) = (h′(k) + c1i + c2i2) mod m
where,
c1 and c2 are positive auxiliary constants,
i = {0, 1, ….}
3. Double hashing
If a collision occurs after applying a hash function h(k), then another hash function is
calculated for finding the next slot.
h(k, i) = (h1(k) + ih2(k)) mod m
• 3 4 5 1 3 2
• 1 3 2 2 1 1
• 6 1 1 4 1 2
• A[0][0]= 3
• A[0][1]=4
• A[0][2]= 5
• A[1][0]= 1
• A[1][1]= 3
• A[1][2]= 2
• A[2][0]= 6
• A[2][1]= 1
• A[2][2]= 1
Multi Dimensional Matrix
• For(i =1; i < r; i++) r=3 1<3
• {
• for(j = 2; j < 3 ; j++) c=3 2<3
• {
• scanf(“%d\n”,&a[i][j]); a[0][0]= 2 213
• } a[0][1]= 1 4 5 1
• printf(“\n”); a[0][2] = 3
• a[1][0]= 4 a[1][1] = 5
• } a[1][2] = 1
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
Matrix muliplication
matrix a
3 1 2
4 2 4
3 5 2
Matrix b
2 3 1
4 2 1
5 5 4
A[0][1] =1
B[1][0] = 4 4
A[0][2] = 2
B[2][0] = 3
3*2+1*4+2*3
Mul[0][0]
16
Sparse Matrix
Sparse matrices are those matrices that have the majority of their elements equal to
zero. In other words, the sparse matrix can be defined as the matrix that has a greater
number of zero elements than the non-zero elements.
Why do we need to use a sparse matrix instead of a simple matrix?
We can also use the simple matrix to store the elements in the memory; then why do we
need to use the sparse matrix. The following are the advantages of using a sparse matrix:
Storage: As we know, a sparse matrix that contains lesser non-zero elements than zero so
less memory can be used to store elements. It evaluates only the non-zero elements.
Computing time: In the case of searching n sparse matrix, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elements. It saves computing time
by logically designing a data structure traversing non-zero elements.
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. The
zeroes in the matrix are of no use to store zeroes with non-zero elements. To avoid such
wastage, we can store only non-zero elements. If we store only non-zero elements, it
reduces the traversal time and the storage space.
Continued……
Array Representation
usage. All are important, but the most concern is about the CPU time. Be careful to differentiate between:
r search when search data is present at the first location of large data then the best case occurs.