0% found this document useful (0 votes)
56 views

Merging of Two Array

The document contains code snippets for various C++ programs including: 1) A function to merge two sorted arrays into a single sorted array. 2) Linear search and binary search algorithms to search for a target value in a sorted array. 3) Bubble sort, selection sort, and insertion sort algorithms to sort arrays. 4) A program to multiply two matrices using nested for loops.

Uploaded by

Vineet Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Merging of Two Array

The document contains code snippets for various C++ programs including: 1) A function to merge two sorted arrays into a single sorted array. 2) Linear search and binary search algorithms to search for a target value in a sorted array. 3) Bubble sort, selection sort, and insertion sort algorithms to sort arrays. 4) A program to multiply two matrices using nested for loops.

Uploaded by

Vineet Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Merging Of Two Array

#include<iostream>
using namespace std;

int merge(int a[],int b[],int m,int n){


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

if(i==m){
for(int x=j;x<n;x++){
c[k]=b[x];
k++;
}
}else{
for(int x=i;x<m;x++){
c[k]=a[x];
k++;
}
}

cout<<"merged array is: ";


for(int x=0;x<m+n;x++){
cout<<c[x]<<" ";
}
return 0;
}

int main(){
int i=0,j=0,k=0;
int m,n;
cout<<"enter the size of array 1:";
cin>>m;
cout<<"enter the size of aray 2:";
cin>>n;
int a[m],b[n];

cout<<"enter the array 1:";


for (int i = 0; i < m; i++){
cin>>a[i];
}
cout<<"enter the array 2:";
for (int i = 0; i < n; i++){
cin>>b[i];
}

int z=merge(a, b, m, n);


return 0;
}
Linear Search

#include<iostream>
using namespace std;
int main()
{
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
return 0;
}
Binary Search

#include<iostream>
using namespace std;
int main()
{
int a[10],n,item,loc,i;

cout<<"enter the size of the array ";


cin>>n;
int ub=n-1;
int lb=0;
int mid=(lb+ub)/2;
cout<<"enter the elements of the array ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter the element you want to search ";
cin>>item;

while(a[mid]!=item && lb<=ub)


{
if(item<a[mid])
{
ub=mid-1;
}
else{
lb=mid+1;
}
mid=int(lb+ub)/2;
}

if(a[mid]==item)
{
loc=mid;
cout<<"element found at "<<loc+1 <<" position";
}else{
cout<<"element not present";
}
}
Bubble Sort

#include<iostream>
using namespace std;

int bubble(int a[],int n){


int i,j,temp;
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<<"sorted array is: ";
for (i=0; i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}

int main()
{
int a[10],i,j,n,temp;
cout<<"enter the size of the array";
cin>>n;
cout<<"enter the elements of the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
int x= bubble(a,n);
return 0;
}
Selection Sort

#include<iostream>
using namespace std;

int selection(int a[],int n){


int i,j,temp,loc,min;

for(i=0;i<n-1;i++){
min=a[i];
loc=i;
for(j=i+1;j<n;j++){
if(a[j]<min){
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"sorted array is ";
for(i=0;i<n;i++){
cout<<a[i]<<"\t";
}
return 0;
}

int main()
{
int a[10],i,j,n,temp;
cout<<"enter the size of the array";
cin>>n;
cout<<"enter the elements of the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
int x=selection( a, n);
return 0;
}
Insertion Sort

#include<iostream>
using namespace std;

int main()
{
int i,j,n,temp,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;

while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}

a[j+1]=temp;
}

cout<<"\nSorted list is as follows\n";


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

return 0;
}
Multiplication Of Matrix

#include<iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],p[10][10],i,j,k,m,n,sum=0;
cout<<"enter the no. of rows of the first matrix ";
cin>>m;
cout<<"enter the no. of columns of the first matrix ";
cin>>n;
cout<<"enter the elements of the first mmatrix";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the no. of rows of the second matrix ";
cin>>m;
cout<<"enter the no. of columns of the second matrix ";
cin>>n;
cout<<"enter the elements of the second matrix";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
p[i][j]=0;
for(k=0;k<n;k++)
{
p[i][j]= p[i][j]+a[i][k]*b[k][j];
}
}
}

cout<<"multiplication of matrix is\n";


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
Factorial Using Recursion

#include<iostream>
using namespace std;

int fact(int n)
{
int b =1;
if(n<0){
cout<<"factorial can not be done of negative integers";
return 0;
}
if(n==0){
return 1;
}
b=n*fact(n-1);
return b;
}

int main()
{
int n;
cout<<"enter a number";
cin>>n;
int temp=fact(n);
cout<<"factorial of" <<n<< "is"<<temp;
return 0;
}
Stack- Push, Pop, Display

#include<iostream>
using namespace std;
int push(int num);
int max1=5;
int stack[5];
int TOP=-1;
int isempty(){
if(TOP==-1){
return 1;
}else{
return 0;
}
}
int isfull(){
if(TOP==max1){
cout<<"value of max is:"<<max1;
cout<<"the value of top is:"<<TOP;
return 1;
}
else{
return 0;
}
}

int push(int num){


if(isfull()){
cout<<"stack is full"<<endl;
return 0;
}
++TOP;
stack[TOP]=num;
cout<<num<<" has been inserted "<<endl;
return 0;
}

int pop(){
int temp;
if(isempty()){
cout<<"stack is empty"<<endl;
return 0;
}
temp=stack[TOP];
TOP--;
cout<<temp<<" is deleted"<<endl;
return 0;
}
int display(){
int i;
if(isempty()){
cout<<"stack is empty";
return 0;
}
for(i=TOP;i>-1;i--){
cout<<stack[i]<<" ";
}
cout<<endl;
return 0;
}
int main(){
int num;
char ch;
do{
int a;
cout<<"choose \n.1.push \n2.pop\n3.display";
cin>>a;
switch(a){
case 1:
cout<<"Enter an integer number: ";
cin>>num;
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
cout<<"an invalid choice!!!!!!";
}
cout<<"Do you want to continue(press y or Y) ?";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;

You might also like