Lab ASSIGNMENT - I (BCAC303)
Lab ASSIGNMENT - I (BCAC303)
Lab ASSIGNMENT - I (BCAC303)
Engineering and
Management
24/1A, Chandi Ghosh Road,
Tollygunge, Kolkata-700040
ASSIGNMENT: I
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.
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);
return 0;
}
OUTPUT:
Q3. Write a C program 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()
{
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
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>
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
/*
* 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];
}
/*
* 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]);
}
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++;
}
}