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

Data Structures Lab Programs

The document contains code for performing various operations on 1D and 2D data structures in C. For 1D arrays, it includes functions for insertion, deletion, and traversal. The main function allows the user to repeatedly select these operations via a menu. For 2D arrays, it includes programs to add two matrices and multiply two matrices by dynamically allocating memory. Both programs accept the matrix sizes as input and perform the respective operation on the entered matrices.

Uploaded by

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

Data Structures Lab Programs

The document contains code for performing various operations on 1D and 2D data structures in C. For 1D arrays, it includes functions for insertion, deletion, and traversal. The main function allows the user to repeatedly select these operations via a menu. For 2D arrays, it includes programs to add two matrices and multiply two matrices by dynamically allocating memory. Both programs accept the matrix sizes as input and perform the respective operation on the entered matrices.

Uploaded by

Varunkumar Raman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DATA STRUCTURES LAB PROGRAMS

1) Write a menu-driven program to perform the operations such as insertion,


deletion and traversing in an one-dimensional data structure.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

void insert(int [], int, int, int); //Insertion function


void insert(int arr[], int n, int ele, int pos)
{
int i;
for(i = n - 1;i >= pos;i--)
{
arr[i + 1] = arr[i];
}
arr[pos] = ele;
n += 1;
}

void delete(int [], int, int, int *); //Deletion function


void delete(int arr[], int n, int pos, int *ele)
{
int i;
*ele = arr[pos];
for(i = pos;i < n-1;i++)
{
arr[i] = arr[i + 1];
}
}

void traverse(int [], int); //Traversion function


void traverse(int arr[], int n)
{
int i;
printf("\nCurrent status of the array : \n");
for(i = 0;i < n;i++)
{
printf("%d ", arr[i]);
}
}

void drawline(); //A function to draw line (just for aesthetics)


void drawline()
{
printf("\n----------------------------------------------------------------------------------------------\n\n");
}

void main()
{
int arr[100], n, ele, pos, op, i;
clrscr();
drawline();
printf("\t\t\tProgram to perform various Array Operations");
drawline();

printf("Enter the number of elements in the array : ");


scanf("%d", &n);
for(i = 0;i < n;i++)
{

printf("Element %d : ", i);


scanf("%d", &arr[i]);
}

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 :

for(i = 0;i >= 0;i++)


{
printf("\n\nEnter the element you wish to insert : ");
scanf("%d", &ele);

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);

printf("\n\nThe deleted element is %d.", ele);


break;
}
else
{
printf("\n!!!!The number you entered is not within the bound of the array
size!!!!");
}
}
break;
case 3 :
traverse(arr, n);
break;
case 4 :
exit(0);
break;
default :
printf("\n!!! Enter a valid number !!!");

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);

a = (int*)calloc((m*n), sizeof(int)); // memroy allacation using calloc function


b = (int*)calloc((m*n), sizeof(int));
sum = (int*)calloc((m*n), sizeof(int));

printf("\nEnter the elements in array A : \n");

for(i = 0;i < m;i++)


{
for(j = 0;j < n;j++)
{
printf("Element a%d%d : ", i, j);
scanf("%d", &a[i*n + j]);
}
}

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();

printf("\nEnter the elements in array B : \n");


for(i = 0;i < m;i++)
{
for(j = 0;j < n;j++)
{
printf("Element b%d%d : ", i, j);
scanf("%d", &b[i*n + j]);
}
}

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();

for(i = 0;i < m;i++)


{
for(j = 0;j < n;j++)
{
sum[i*n + j] = a[i*n + j] + b[i*n + j];
}
}

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();

for(int f = 0;f >= 0;f++)


{
printf ("\nEnter the order of the matrix A : ");
scanf ("%d %d", &m1, &n1);
printf ("Enter the order of the matrix B : ");
scanf ("%d %d", &m2, &n2);

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));

pro = (int**)calloc(m1, sizeof(int));


for (i = 0; i < n1; i++)
pro[i] = (int*)calloc(n1, sizeof(int));

printf("\nEnter the elements in array A : \n");


for(i = 0;i < m1;i++)
{
for(j = 0;j < n1;j++)
{
printf("Element a%d%d : ", i, j);
scanf("%d", &a[i][j]);
}
}

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();

printf("\nEnter the elements in array B : \n");


for(i = 0;i < m2;i++)
{
for(j = 0;j < n2;j++)
{
printf("Element b%d%d : ", i, j);
scanf("%d", &b[i][j]);
}
}

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++)
{

for(j = 0;j < n2;j++)


{
pro[i][j] = 0;
for(k = 0;k < n1;k++)
pro[i][j] += a[i][k]*b[k][j];
}
}

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);
}
}
}

You might also like