0% found this document useful (0 votes)
1K views11 pages

Write A Program To Store The Elements in 1-D Array and Perform The Operations Like Searching, Sorting and Reversing The Elements. (Menu Driven)

The document contains code for three practical programs involving arrays and matrices: 1. A menu-driven program to store elements in a 1D array and perform operations like searching, sorting, and reversing the elements. 2. A menu-driven program to read two arrays from the user, merge them, and display the sorted merged array. 3. A menu-driven program to perform matrix addition, multiplication, and transpose operations.

Uploaded by

Balavant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views11 pages

Write A Program To Store The Elements in 1-D Array and Perform The Operations Like Searching, Sorting and Reversing The Elements. (Menu Driven)

The document contains code for three practical programs involving arrays and matrices: 1. A menu-driven program to store elements in a 1D array and perform operations like searching, sorting, and reversing the elements. 2. A menu-driven program to read two arrays from the user, merge them, and display the sorted merged array. 3. A menu-driven program to perform matrix addition, multiplication, and transpose operations.

Uploaded by

Balavant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical 1

Write a program to store the elements in 1-D array and perform the operations like
searching, sorting and reversing the elements. [Menu Driven]

#include<stdio.h>
int a[20];
intn,i,j,key,temp;
/*Function Prototype*/
void search();
void reverse();
void sort();
void display();
int main()
{
int choice;
cout<<”Enter the size of the array elements:"<<endl;
cin>>n;
cout<<”Enter the elements for the array"<<” “;
for(i=0;i<n; i++)
{cin>>a[i];}

do{cout<<"--------Menu-----------"<<endl;
cout<<"1.Search”<<endl;
cout<< "2.Sort”<<endl;
cout<<"3.Reverse”<<endl;
cout<<"4. Display”<<endl;

cout<<"-----------------------";
cout<<"Enter your choice: ”<<endl;
cin>>choice;
switch(choice)
{
case 1: search();
break;
case 2:
sort();
break;
case 3:
reverse();
break;
case 4:
display();
break;
default:
cout<<"Invalid choice:"<<” “;
break;
}
}while(choice!=4);
return 0;
}

void search() //searching an array element


{cout<<"Enter the element to be searched:"<<endl;
cin>>key;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
cout<<The element is present at position "<<i+1;
break;
}
}
if(i==n)
{
cout<<"The search is unsuccessful";
}
}//end of search()

void sort() //sorting the array elements


{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"After sorting the array elements are:"<<endl;
display();
}//end of sort

void reverse() //reversing the array


{
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
cout<<“Reverse array”<<endl;

for(i=0;i<n;i++)
cout<<a[i];
}

void display()
{for(i=0;i<n;i++)
{ cout<<a[i];
} }

Practical 2
Read the two arrays from the user and merge them and display the elements in sorted
order.[Menu Driven]

#include<stdio.h>

int a[20],b[20],c[40];
int n;
/*Function Prototype*/
void create(intarr[]);
void merge(int m[], int n[]);
void sort(int temp[]);
void display(intarr[], int n);
int length(int temp[]);
int main()
{
int i;
int choice, opt,val;

do{
cout<<"--------Menu-----------"<<endl;
cout<<"1.Create 1st Array ”<<endl;
cout<<"2.Create 2nd Array ”<<endl;
cout<< "3.Sort 1st array”<<endl;
cout<< "4.Sort 2nd array”<<endl;
cout<<"5. Merge”<<endl;
cout<<"6. Display Sorted Array”<<endl;

cout<<"-----------------------";
cout<<"Enter your choice: ”<<endl;
cin>>choice;
switch(choice)
{
case 1: create(a);
cout<<”the first array is”;
display(a,n)
break;
case 2: create(b);
cout<<”the second array is”;
display(b)
break;

case3:sort(a);
cout<<”the sorted first array is:”;
display(a);
break;

case 4: sort(b);
cout<<”the sorted second array is:”;
display(b);
break;
case5 :
merge(a,b);
break;

case 6
cout<<”the merged sorted second array is:”;
display(arr);
break;

default:
cout<<"Invalid choice:"<<” “;
break;
}
}while(choice!=4);
return 0;
}

int length(int temp[])


{int i=0;
while(temp[i]!=’\0’)
{ i++}
return i;
}

void create (int temp[])


{
int i, n;
cout<<Enter the number of elements”<<endl;
cin>>n;
for (i=0; i<n;i++)
{ cin>>temp[i];
} }

void display(int temp[] ,int n)


{
int i;
n=length(temp);
for(i=0;i<n;i++)
{
cout<<temp[i]<<endl;
}
}
void sort(int temp[])
{
inti,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(temp[j]> temp [j+1])
{
t= temp [j];
temp[j]= temp [j+1];
temp[j+1]=t;
}
}

}
}

void merge(int m[], int n[])


{
int n1,n2;
int i=0,j=0,k=0;
n1=length(a);
n2=length(b);
while(i<n1 && j<n2)
{
if(m[i]<n[j])
{
c[k]=m[i];
i++;
k++;
}
else
{
c[k]=n[j];
k++;
J++;
}
}
while(i<n1)
{
c[k]=m[i];
i++;
k++;
}
while(j<n2)
{
c[k]=n[j];
j++;
k++;
}}

Practical 2
Read the two arrays from the user and merge them and display the elements in sorted
order.[Menu Driven]

#include <iostream.h>
int *merge(int a[ ], int m, int b[ ], int n )
{
inti,j;
int *c=new int[m+n];
for ( i=0; i<m; i++)
c[i]=a[i];
for ( int i=m,j=0; i<m+n; i++,j++)
c[i]=b[j];
return c;
}
void display(int a[], int size)
{
for(int i=0;i<size;i++)
cout<<a[i]<<endl;
}
void sort(int *a, int size)
{
int temp;
for(int i=0;i<size-1;i++)
{
for(int j=i+1; j<size; j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}}}
void main()
{
intm,n;
charch;
do
{
cout<<”\nEnter size of first array”;
cin>>m;
int *x=new int[m];
cout<<”enter ”<<m<<”numbers”<<endl;
for(int i=0;i<m;i++)
cin>>x[i];
cout<<”\nEnter size of second array”;
cin>>n;
int *y=new int[n];
cout<<”enter ”<<n<<”numbers”<<endl;
for(int i=0;i<n;i++)
cin>>y[i];
cout<<”first array\n”;
display(x,m);
cout<<”second array\n”;
display(y,n);
int *z=merge(x,m,y,n);
cout<<”merged array\n”;
display(z,m+n);
sort(z,m+n);
display(z,m+n);
cout<<”menu options –Do you want to continue(Y/N)”<<endl;
cin>>ch;
}while(ch==’y’||ch==’Y’);}

Practical 3: Write a program to perform the Matrix addition, Multiplication and


Transpose Operation. [Menu Driven]
#include<iostream.h>
/*Function Prototype*/
void addition();
void multiplication();
void transpose();
void main()
{
int choice;
do{
cout<<"\n\n--------Menu-----------\n";
cout<<"1.Addition\n";
cout<<"2.Multiplication\n";
cout<<"3.Transpose\n";
cout<<"-----------------------";
cout<<"\nEnter your choice:\t";
cin>>choice;
switch(choice)
{case 1 : addition ();
break;
case 2:
multiplication();
break;
case 3:
transpose();
break;
default:
cout<<"\n Invalid choice:\n";
break;
}
}while(choice!=4);}
void addition()
{int r, c, a[10][10], b[10][10], sum[10][10], i, j;
cout<< "Enter number of rows (between 1 and 10): ";
cin>> r;
cout<< "Enter number of columns (between 1 and 10): ";
cin>> c;
cout<<endl<< "Enter elements of 1st matrix: " <<endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout<< "Enter element a" << i + 1 << j + 1 << " : ";
cin>> a[i][j];
}
// Storing elements of second matrix entered by user.
cout<<endl<< "Enter elements of 2nd matrix: " <<endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout<< "Enter element b" << i + 1 << j + 1 << " : ";
cin>> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout<<endl<< "Sum of two matrix is: " <<endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout<< sum[i][j] << " ";
if(j == c - 1)
cout<<endl;
}

}
void multiplication
{int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout<< "Enter rows and columns for first matrix: ";
cin>> r1 >> c1;
cout<< "Enter rows and columns for second matrix: ";
cin>> r2 >> c2;

// If column of first matrix in not equal to row of second matrix,


// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout<< "Error! column of first matrix not equal to row of second.";
cout<< "Enter rows and columns for first matrix: ";
cin>> r1 >> c1;
cout<< "Enter rows and columns for second matrix: ";
cin>> r2 >> c2;
}
// Storing elements of first matrix.
cout<<endl<< "Enter elements of matrix 1:" <<endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{ cout<< "Enter element a" << i + 1 << j + 1 << " : ";
cin>> a[i][j];
}
// Storing elements of second matrix.
cout<<endl<< "Enter elements of matrix 2:" <<endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout<< "Enter element b" << i + 1 << j + 1 << " : ";
cin>> b[i][j];
}
// Initializing elements of matrix mult to 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{ mult[i][j] += a[i][k] * b[k][j];
}
// Displaying the multiplication of two matrix.
cout<<endl<< "Output Matrix: " <<endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{cout<< " " <<mult[i][j];
if(j == c2-1)
cout<<endl;
}}
void transpose()
{ int a[10][10], trans[10][10], r, c, i, j;
cout<< "Enter rows and columns of matrix: ";
cin>> r >> c;
cout<<endl<< "Enter elements of matrix: " <<endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout<< "Enter elements a" << i + 1 << j + 1 << ": ";
cin>> a[i][j]; }
cout<<endl<< "Entered Matrix: " <<endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout<< " " << a[i][j];
if(j == c - 1)
cout<<endl<<endl; }
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ trans[j][i]=a[i][j]; }
cout<<endl<< "Transpose of Matrix: " <<endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{ cout<< " " << trans[i][j];
if(j == r - 1)
cout<<endl<<endl;
}}

You might also like