Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Data Structure Journal

1. Searching:-

I]Linear search:-

// C++ Program to Implement Linear Search

#include<iostream>

using namespace std;

int main()

int arr[10], i, num, index;

cout<<"Enter 10 Numbers: ";

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

cin>>arr[i];

cout<<"\nEnter a Number to Search: "; // to search in linear pair

cin>>num;

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

if(arr[i]==num)

index = i;

break;

cout<<"\nFound at Index No."<<index;

cout<<endl;

return 0;

OUTPUT 1:-

1
Data Structure Journal

OUTPUT 2:-

2
Data Structure Journal

2. SEARCHING
II]Binary search:-
// Binary Search in C++
#include <iostream.h>
#include<stdio.h>
int binarySearch(int array[], int x, int low, int high)
{
// Repeat until the pointers low and high meet each other
while(low<=high)
{
int mid=low+(high-low)/2;
if(array[mid]== x)
return mid;
if(array[mid]<x)
low = mid+1;
else
high = mid – 1;
}
return-1
}
int main(void)
{
int array[]={3,4,5,6,7,8,9};
int x=4;
int n = sizeof(array)/sizeof(array[0]);
int result=binarySearch(array, x, 0, n-1);
if(result == -1)
printf(“Not found”);
else
printf(“Element is found at index %d”, result);
}
return 0;
}
Output 1:-
For x=4

OUTPUT2:-
For x=5

3
Data Structure Journal

4
Data Structure Journal

2.SORTING:-
IV] Merge Sort:-

//perform to perform merge sorting of the given array


#include&lt;stdio.h&gt;
void disp( ); //header file
// formula to sort the array
void mergesort(int,int,int);
//formula to divide array
Void mergediv(int ,int)
int a[50],n;
void main( )
{
int i;
clrscr( );
printf(&quot;\nEnter the n value:&quot;);
scanf(&quot;%d&quot;,&amp;n);
//for array in the program
printf(&quot;\nEnter elements for an array:&quot;);
for(i=0;i&lt;n;i++)
scanf(&quot;%d&quot;,&amp;a[i]);
//to display the enter array before sorting
printf(&quot;\nBefore Sorting the elements are:&quot;);
disp( );
//formula for sorting
msortdiv(0,n-1);
//to display elements after sorting
printf(&quot;\nAfter Sorting the elements are:&quot;);
disp( );
getch( );
}
void disp( )

{
int i;
for(i=0;i&lt;n;i++)
printf(&quot;%d &quot;,a[i]);
//in order to perform
merge sorting

void mergesort(int low,int mid,int high)


{
int t[50],i,j,k;
i=low;
j=mid+1;
k=low;
while((i&lt;=mid) &amp;&amp; (j&lt;=high))
{

5
Data Structure Journal

//using conditional statement in irder to check if (low&lt;high)


if(a[i]&gt;=a[j])
t[k++]=a[j++];
else
t[k++]=a[i++];
}
while(i&lt;=mid)
t[k++]=a[i++];
while(j&lt;=high)
t[k++]=a[j++];
for(i=low;i&lt;=high;i++)
a[i]=t[i];
}
void msortdiv(int low,int high)
{
int mid;
if(low!=high)
{
mid=((low+high)/2);
msortdiv(low,mid);
msortdiv(mid+1,high);

mergesort(low,mid,high);
}
}
OUTPUT 1:-

OUTPUT 2:-

6
Data Structure Journal

3. TWO DIMENSIONAL ARRAYS:-


A.i] Display principal diagonal(and reverse diagonal elements.
//program to display principal diagonal elements
#include&lt;stdio.h&gt; //header file
#include&lt;conio.h&gt;
void main()
{
int array[10][10],i,j,m,n; //defining variables
printf(“enter no of rows”); // elements in row
scanf(“%d”,&amp;m);
printf(“\n enter the no.of cols”); //elements in columns
scanf(“%d”,&amp;n);
printf(“/n enter values in matrix \n”);
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
printf(“\n enter a[%d] [%d] value ::”,i,j);
scanf(“%d”,&amp;array1[i][j]); //in order to introduce array in the program
}

}
//check condition to print diagonals,matrix must be square matrix if(m==n)
printf(“\n the diagonal elements of matrix are ::\n\n”); //enter the values of diagonals
{
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
if(i==j)
printf(“\t %d”,array1[i]][j]); //print elements
else
printf(“\t”); //print space
}
printf(“\n \n”); //after each row new line
}
}
{
printf(“\n matrix is not a square matrix”);
}
getch();
}

7
Data Structure Journal

OUTPUT 1:-

OUTPUT 2:-

8
Data Structure Journal

3.a
III]Checking for unit matrix.

// using C++ to check the whether the matrix is a unit matrix

#include<iostream.h>

int main()

int I,j,r,c,flag=1;

cout<<”\nPlease Enter Matrix rows and columns :” // no of rows andcolumns

cin>>i>>j;

int unitMatrix[i][j];

cout<<”\nPlease Enter the unit Matrix items\n”; //putting elements in the matrix

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

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

if(unitMatrix[r][c]!=1&&unitMatrix[c][r]!=0)

flag=0;

break;

if(flag == 1)

cout<<”The Matrix that you entered is an Unit Matrix”;

else

cout<<”\nThe Matrix that you entered is Not an Unit Matrix”;

9
Data Structure Journal

return 0;

OUTPUT1:-

Output 2:-

10
Data Structure Journal

3.a

IV] Row-wise/column-wise matrix.

//program to preform addition of sparse matrix row-wise and column wise

#include&lt;stdio.h&gt; //header file

#include&lt;conio.h&gt;

void main()

int a[3][3],i,j,sum; //defining the variables

for(i=0;i&lt;3;i++)

for(j=0;j&lt;3;j++)

printf(“\n enter value for 2d array”); //to introduce array in the program

scanf(“%d”,&amp;a[i][j]);

printf(“\n 2d array elements are”); //representing array in matrix form

for(i=0;i&lt;3;i++);

printf(“/n”); //after each row new line

for (j=0;j,&lt;3;j++);

printf(“%d \t”,a[i][j]); // for space

for(i=0;i&lt;3;i++)

sum=0;

for(j=0;j&lt;3;j++)

sum=sum+a[i][j]; //formula to print sum of the row

printf(“\ n sum of row %d is %d”,i+1,sum);

11
Data Structure Journal

for(i=0;i&lt;3;i++)

sum=0;

for[j=0;j&lt;3;j++)

sum=sum+a[j][i]; //formula to print sum of the column

printf(“\n sum of column %d is %d”,i+1,sum);

getch();

OUTPUT 1:-

12
Data Structure Journal

OUTPUT 2:-

13
Data Structure Journal

4. Two dimensional arrays-memory representation

I] Row-order representation

// row order representation

#include<iostream.h>

#include<conio.h>

void main()

int mat[3][3];

int i,j;

clrscr();

cout<<"Enter Elements of Matrix\n"; //entering matrix element

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

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

cin>>mat[i][j];

cout<<"\nRow order representation : \n";

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

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

cout<<mat[i][j];

cout<<" row-[" <<i+ 1<<"]\n";

getch();

14
Data Structure Journal

Output 1:-

Output 2:-

15
Data Structure Journal

II]Column order representation:-

// C++ program to represent column order

#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3]; // introducing variables
int i,j;
clrscr();
cout<<"Enter Elements of Matrix\n"; //putting elements in the matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[j][i];
}
}
cout<<"\column major array: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat[j][i];
cout<<" column-[" <<i+ 1<<"]\n";
}
}
getch();
}
OUTPUT 1:-

16
Data Structure Journal

OUTPUT2:-

17
Data Structure Journal

5.SPARSE MATRIX:-

1]Diagonal

#include<stdio.h>

#include<conio.h>

int a[3][3],i,j,k=0;c=0;

printf(“enter the matrix elements:”);

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

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

scanf(“%d”,&a[i][j]);

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

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

if(a[i][j]!=0)

c++;

int b[i][j];

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

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

18
Data Structure Journal

if (a[i][j]!=0)

{ b[0][k]=i;

b[1][k]=j;

b[2][k]=a[i][j];

k++;

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

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

printf(“%2d”,b[i][j]);

printf(“\n”);

getch();

19
Data Structure Journal

III] Triplet representation.

//Program to convert a sparse matrix to triplet representation

#include&lt;stdio.h&gt; //header file

#include&lt;conio.h&gt;

void main()

Clrscr(); //for clear screen for each time after execution

int a[100][100],r,c,i,j; //variables for rows and columns

printf(“Enter the no.of rows and columns of matrix:”);

scanf(“%d %d”,&amp;r,&amp;c);

printf(“\n Enter a sparse matrix: ”); //sparse matrix of class 3X3

for(i=0;i&lt;r;i++)

for(j=0;j&lt;c;j++)

scanf(“%d”,&amp;a[i][j]);

printf(“/n The triplet representation of the sparse matrix”); //generating the result matrix

for(i=0;i&lt;r;i++)

for(j=0;j&lt;c;j++)

if(a[i][j]!0=0) // finding the total non zero values in sparse matrix

printf(“\n %d %d %d ”,i+1,j+1,a[i][j]);

printf(“\n”);

getch();

20
Data Structure Journal

//to hold the output screen

Output 1:-

Output 2:-

21
Data Structure Journal

22

You might also like