Data Structures Lab Programs
Data Structures Lab Programs
void main()
{
int arr[100], n, ele, pos, op, i;
clrscr();
drawline();
printf("\t\t\tProgram to perform various Array Operations");
drawline();
traverse(arr, n);
for(i = 0;i >= 0;i++) // A loop so that the operations can be performed repeatedly without
just exiting after one operation
{
drawline();
printf("\t1 - Insertion\n");
printf("\t2 - Deletion\n");
printf("\t3 - Traversion\n");
printf("\t4 - Exit");
drawline();
printf("\nEnter the number to perform the corresponding operation : ");
scanf("%d", &op);
switch (op)
{
case 1 :
printf("Enter the position where you wish to add this element : ");
scanf("%d", &pos);
if(pos <= n)
{
insert(arr, n, ele, pos);
traverse(arr, n);
break;
}
else
{
printf("\n!!!!The number you entered is not within the bound of the array
size!!!!");
}
}
break;
case 2 :
for(i = 0;i >= 0;i++)
{
printf("\n\nEnter the position of the element you wish to delete : ");
scanf("%d", &pos);
if(pos <= n)
{
delete(arr, n, pos, &ele);
traverse(arr, n);
break;
}
}
}
2) –
A) Write a program to perform the addition of two matrices using 2D data structures.
Read the number of rows and columns as an input form the user and dynamically
allocate the memory space(using single pointer).
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void drawline()
{
printf("\n-----------------------------------------------------------------------\n");
}
void main()
{
int *a, *b, *sum; // Pointers thats gonna store the array elements
int m, n, i, j;
clrscr();
drawline();
printf(" Program to perform Matrix addition using Dynamic memory allocation"); //
Program title
drawline();
printf("Inorder to perform addition of two matrices, the rows and columns of both must
be equal");
printf("\nEnter the number of rows in the arrays : ");
scanf("%d", &m);
printf("Enter the number of columns in the arrays : ");
scanf("%d", &n);
drawline();
printf("\nThe matrix A is : \n");
for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
printf("%d ", a[i*n + j]);
}
printf("\n");
}
drawline();
drawline();
printf("\nThe matrix B is : \n");
for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
printf("%d ", b[i*n + j]);
}
printf("\n");
}
drawline();
drawline();
printf("\nThe sum of the matrices A and B is : \n");
for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
printf("%d ", sum[i*n + j]);
}
printf("\n");
}
drawline();
}
B) Write a program to perform the multiplication of two matrices using 2D data
structure. Read the number of rows and columns from the user and dynamically
allocate the memory(pointers to pointers).
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void drawline()
{
printf("\n----------------------------------------------------------------------------\n");
}
void main ()
{
int **a, **b, **pro; //Pointers to pointers to represent arrays
int m1, m2, n1, n2, i, j, k;
drawline();
printf(" Program to perform Matrix Multiplication Using Dynamic Memory Allocation");
drawline();
if (n1 != m2)
{
printf("!!! MATRIX MULTIPLICATION not possible for these two Matrices !!!");
}
else
{
a = (int**)calloc(m1, sizeof(int));
for (i = 0; i < n1; i++)
a[i] = (int*)calloc(n1, sizeof(int));
b = (int**)calloc(m1, sizeof(int));
for (i = 0; i < n1; i++)
b[i] = (int*)calloc(n1, sizeof (int));
drawline();
printf("\nThe matrix A is : \n");
for(i = 0;i < m1;i++)
{
for(j = 0;j < n1;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
drawline();
drawline();
printf("\nThe matrix B is : \n");
for(i = 0;i < m2;i++)
{
for(j = 0;j < n2;j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
drawline();
for(i = 0;i < m1;i++)
{
drawline();
printf("\nThe product of the matrices A and B is : \n");
for(i = 0;i < m1;i++)
{
for(j = 0;j < n2;j++)
{
printf("%d ", pro[i][j]);
}
printf("\n");
}
drawline();
exit(0);
}
}
}