Array
Array
Array
int a = 0, b = 0;
for (i = 0; i < N; i++)
{
a = a + random();
}
for (j = 0; j < M; j++)
{
b = b + random();
}
Problem : 5
int a = 0, b = 0;
for (i = 0; i < N; i++)
{
a = a + random();
}
for (j = 0; j < M; j++)
{
b = b + random();
}
Answer :- O(N + M)
Problem :- 6
int a = 0;
for (i = 0; i < N; i++)
{
for (j = N; j > 0; j--)
{
a = a + i + j;
}
}
Problem :- 6
int a = 0;
for (i = 0; i < N; i++)
{
for (j = N; j > 0; j--)
{
a = a + i + j;
}
}
Answer:- O(N*N)
Problem:- 7
int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
Problem:- 7
int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
Answer:- O(Log2N)
Problem:- 8
for(i=0;i<n;i++)
i*=k
Problem:- 8
for(i=0;i<n;i++)
i*=k
Answer:- O(LogkN)
Unit-2
Linear Data Structure
(Array)
Array
• Definition & Declaration of array
• Reason of introducing Array data structure
• Representation of array
– Single dimensional array
– Multi dimensional array
• Sparse matrix
• Application of array
Concept of Array:-
Consider a situation in which we have 20 students in a class and we have been asked to
write a program that reads and prints the marks of all the 20 students.
#include <stdio.h>
int main()
{
int array[10], i;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
return 0;
}
Operations on array
• Traversing
• Insertion
• Deletion
• Searching
• Etc..
Algorithm for traversing an array (Traversing a Linear Array)
Here LA is a linear array with lower bound LB and upper bound UB.
This algorithm traverses LA applying an operation PROCESS to each
element of LA.
5 . Exit.
Algorithm for inserting an element into an array (Inserting
into a linear array) INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer
such that K <= N. This algorithm inserts an element ITEM into the Kth
position in LA.
2. Repeat for J = K to N - 1.
[Move J + 1st element upward.] Set LA[J] := LA[J + 1].
[End of loop.]
4. Exit.
Traversing example
WAP to find largest number from an array:
#include <stdio.h>
int main() 5
{
int array[10], size, i, largest;
3
10
printf("\n Enter the size of the array: ");
scanf("%d", &size); 3
4
printf("\n Enter %d elements of the array: ", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
largest = array[0];
#include <stdio.h> 2
int main()
5
{ int array[10], position, c, n, value;
99 10
printf("Enter number of elements in array\n"); 22
scanf("%d", &n);
array[position-1] = value;
printf("Enter the location where you wish to
printf("Resultant array is\n");
insert an element\n");
scanf("%d", &position); for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
printf("Enter the value to insert\n");
scanf("%d", &value); return 0;
}
Deletion operation
WAP to delete an element at given position from an array 5
#include <stdio.h> 10
#define MAX_SIZE 10
20
int main()
{ int arr[MAX_SIZE]; 4
int i, size, pos;
clrscr();
else
printf("Enter size of the array : "); {for(i=pos-1; i<size-1; i++)
scanf("%d", &size); {
printf("Enter elements in array : "); arr[i] = arr[i+1];
for(i=0; i<size; i++) }
size--;
{
}
scanf("%d", &arr[i]);
printf("\nElements of array after
} delete are:");
printf("Enter the element position to delete : for(i=0; i<size; i++)
"); {
scanf("%d", &pos); printf("%d\t", arr[i]);
if(pos==size+1 || pos<0) }
{ getch();
printf("Invalid position! Please enter return 0;
position between 1 to %d", size); } }
Searching operation 5
10
#include<stdio.h> 1
44
int main() { 32
int a[30], ele, num, i;
clrscr(); i = 0;
printf("\nEnter no of elements :"); while (i < num && ele != a[i])
scanf("%d", &num); {
i++;
printf("\nEnter the values :"); }
for (i = 0; i < num; i++) { if (i < num) {
scanf("%d", &a[i]);
} printf("Number found at location = %d", i +1);
printf("\nEnter the elements to be }
searched :"); else {
scanf("%d", &ele); printf("Number not found");
}
getch();
return (0);
}
2 D array
• One-dimensional arrays are organized linearly in only one
direction.
• But at times, we need to store data in the form of grids or
tables.
• Here, the concept of single-dimension arrays is extended to
incorporate two-dimensional data structures.
• A two-dimensional array is specified using two subscripts
where the first subscript denotes the row and the second
denotes the column.
• Declaration of 2D array
• Ex:
– Int marks[3][3];
Write a program to represent 2D array matrix.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("\nenter first matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ scanf("%d",&a[i][j]); }
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
getch();
}
Ex: addition in 2D array
Write a program to perform addition of matrix.
#include<conio.h>
for(i=0;i<3;i++)
#include<stdio.h>
{for(j=0;j<3;j++)
void main()
{
{int a[3][3],b[3][3],c[3][3],i,j; c[i][j]=a[i][j]+b[i][j];
clrscr(); }
printf("\nenter first matrix\n"); }
for(i=0;i<3;i++)
{ for(i=0;i<3;i++)
for(j=0;j<3;j++) {
for(j=0;j<3;j++)
{
{
scanf("%d",&a[i][j]);
printf("%d",c[i][j]);
}} }
printf("\n");
printf("\nenter second matrix\n"); }
for(i=0;i<3;i++)
{for(j=0;j<3;j++) getch();
{ }
scanf("%d",&b[i][j]);
}}
Multiplication of array
Write a program to perform multiplication of matrix.