0% found this document useful (0 votes)
9 views13 pages

Assignment

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9 views13 pages

Assignment

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

Stream: BCA Year: 2 Semester:3

nd rd

Subject: Data Structure and Algorithm


Lab
Subject Code: BCAC393
NAME:- Gourav Jain
UNIVERSITY ROLL NO:- 29901220025
Data Structure and Algorithm Lab (BCAC393)
2021
ROLL 29901220025
NAME Gourav Jain

ASSIGNMENT: I

Sl. Program Listing


No
1 Write a C program to insert an element in a specific position of an array.
2 Write a C program to delete an element from a specified position of an array.
3 Write a C program to merge two sorted arrays.
4 Write a C program to remove duplicates elements of an array.
5 Write a C program that will take a sparse matrix as input and display its three-tuple
form.
Q1. Write a C program to insert an element in a specific position of an array.

AIM: To insert a element in a specific position of an array.

Code:
#include<stdio.h>

//function prototypes
//void insert(int[],int*,int,int);

int main()
{
int arr[100],n,i,item,pos;
//prompt to enter n
printf("Enter n: ");
scanf("%d",&n);
//read the array elements
for(i=0; i<n;i++)
{
scanf("%d",&arr[i]);
}
//read inserted element
printf("Enter the location \n");
scanf("%d",&item);
//read position
printf("enter the value \n");
scanf("%d",&pos);
//insert an item at pos
// insert(arr,&n,pos,item);
//display the array
for(i=n-1;i>=item-1;i--)
arr[i+1]=arr[i];
arr[item-1]=pos;
printf("resultant array is \n");
for(i=0;i<=n;i++)
{
printf("%d",arr[i]);
}
return 0;
}
OUTPUT:
Q2. Write a C program to delete an element from a specified position of an array.

AIM: To delete an element from a specific position of an array

Code:
#include<stdio.h>

//function prototypes
//void insert(int[],int*,int,int);

int main()
{
int arr[100],n,i,item,pos;
//prompt to enter n
printf("Enter no of element:\n ");
scanf("%d",&n);
//read the array elements
printf("enete %d element\n", n);
for(i=0; i<n;i++)
{
scanf("%d",&arr[i]);
}
//read inserted element
printf("Enter the location wish to delete \n");
scanf("%d",&item);

if(item >= n+1)


printf("deletion not posible.\n");
else
{
for(i=item-1;i< n-1;i++)
arr[i]=arr[i+1];
printf("result arr:\n");
for(i=0;i<n-1;i++)
printf("%d\n",arr[i]);
}

return 0;
}
OUTPUT:
Q3. Write a C program to merge two sorted arrays.

AIM:To merge two sorted arrays.

Code:
#include <stdio.h>
#include <conio.h>
void sort(int c[],int n3) //Function to sort the elements of the array
{
for(int i=0;i<n3;i++)
{
int temp;
for(int j=i+1; j<n3 ;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
}
int main()
{

int n1,n2,n3; //Array size declaration


printf("\nEnter the size of first array ");
scanf("%d",&n1);
printf("\nEnter the size of second array ");
scanf("%d",&n2);

n3=n1+n2;
printf("\nEnter the array elements");
int a[n1],b[n2],c[n3]; //Array Declaration
for(int i=0;i<n1;i++) //Array Initialization
{
scanf("%d",&a[i]);
}
sort(a,n1); //Function call to sort the array
int k=n1;
printf("\nEnter the array elements");
for(int i=0;i<n2;i++) //Array Initialization
{
scanf("%d",&b[i]);
}
sort(b,n2); //Function call to sort the array

for(int i=0;i<n1;i++) //Merge two arrays


{
c[i]=a[i];
}
for(int j=0;j<n2;j++)
{
c[k]=b[j];
k++;
}
printf("\nAfter sorting...\n");
sort(c,n3); //Function call to sort the array
for(int i=0 ; i<n3 ; i++) //Print the resultant sorted array
{
printf(" %d ",c[i]);
}
return 0;
}

Output:
Q4. Write a C program to remove duplicates elements of an array.
AIM : To remove duplicate elements of an array

Code:

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main()
{
int arr[MAX_SIZE]; // Declares an array of size 100
int size; // Total number of elements in array
int i, j, k; // Loop control variables

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Find duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k < size - 1; k++)
{
arr[k] = arr[k + 1];
}

/* Decrement size after removing duplicate element */


size--;
/* If shifting of elements occur then don't increment j */
j--;
}
}
}

/*
* Print array after deleting duplicate elements
*/
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}

Output:
Q5. Write a C program that will take a sparse matrix as input and display its three-
tuple form.

AIM: To take a sparse matrix as input and display its three-tuple form.

Code:
#include<stdio.h>
#define srow 100
#define mrow 60
#define mcolumn 60

/*Begin of main*/
int main()
{
int mat[mrow][mcolumn],sparse[srow][3];
int i,j,d=0,mr,mc,sr,s;

//taking inputs
printf("Enter number of rows : ");
scanf("%d",&mr);
printf("Enter number of columns : ");
scanf("%d",&mc);

for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
//taking inputs of rows and columns
printf("Enter element for row %d,column %d : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}

//printing entered matrix


printf("Entered matrix is : \n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
printf("%6d",mat[i][j]);
if(mat[i][j]!=0)
d++;
}
printf("\n");
}

sr=d+1;
sparse[0][0]=mr;
sparse[0][1]=mc;
sparse[0][2]=d;
s=1;

for(i=0;i<mr;i++)
for(j=0;j<mc;j++)
{
if(mat[i][j]!=0)
{
sparse[s][0]=i+1;
sparse[s][1]=j+1;
sparse[s][2]=mat [i][j];
s++;
}
}

//printing sparse matrix


printf("Sparse matrix is :\n");
for(i=0;i<sr;i++)
{
for(j=0;j<3;j++)
printf("%5d",sparse[i][j]);
printf("\n");
}
}
Output:

You might also like