Ds 2
Ds 2
Ds 2
Aim – Write A program to implement sparse matrix & check whether it is sparse or not.
ALGORITHM
1. First create an array a [ 10 ] [ 10 ] , i ,j , m , n & the program takes the number of rows and columns of the
matrix.
2. Then the elements are entered.
3. If the matrix contains maximum number of elements as 0, then it is a sparse matrix.
4. Else not.
5. The result is printed.
6. Exit.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
static int a[10][10];
int i,j,m,n;
int count=0;
clrscr();
printf("Enter the order of the matrix \n");
scanf("%d%d",&m,&n);
printf("Enter the co-effients of the matrix \n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a [ i ] [ j ] );
if(a [ i ] [ j ] ==0)
{
++count;
}
}
}
printf("\n Matrix is :-\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
if(count > ((m*n)/2))
{
printf("The given matrix is sparse matrix \n");
}
else
{
printf("The given matrix is not a sparse matrix \n");
}
printf("There are %d number of zeros",count);
getch();
}
OUTPUT
Enter the order of the matrix
3
3
Enter the co-effients of the matrix
1
2
0
3
6
7
0
2
7
Matrix is :-
1 2 0
3 6 7
0 2 7
The given matrix is not a sparse matrix
There are 2 number of zeros
Experiment 12: Program to implement bubble
sorting
Algorithm
1. Input array
2. BubbleSort()
2.1 for i=0 till i<size, i++
for j=0 till j<size-1, j++
if AR[j]>AR[j+1]
tmp=AR[j]
AR[j]=AR[j+1]
AR[j+1]=tmp;
print array
Program
#include<stdio.h>
#include<conio.h>
void BubbleSort(int[],int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n How many elements do you want to add ");
scanf("%d",&N);
printf("\n Enter Array elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
BubbleSort(AR,N);
printf("\n\n The sorted array is ");
for(i=0;i<N;i++)
{
printf("\t%d",AR[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n Enter no of elements ");
scanf("%d",&N);
printf("\n Enter Array Elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
printf("\n Enter element to be searched ");
scanf("%d",&ITEM);
index=Lsearch(AR,N,ITEM);
if(index==-1)
printf("\n Element not found ");
else
printf("\n\n Element found at Position: %d ",index+1);
getch();
}
Program
#include <stdio.h>
#include <conio.h>
#define size 100
int partition(int a[], int beg, int end);
void quick_sort(int a[], int beg, int end);
void main()
{
int arr[size], i, n;
printf("\n Enter number of elements ");
scanf("%d", &n);
printf("\n Enter the elements of the array ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
printf("\n The sorted array is: ");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}