Sie sind auf Seite 1von 41

LAB 1: Find max and min element and number positive or negative in array

This program shall determine the Maximum and Minimum Number entered by the user and also tell the positive or
negative numbers

Roll No: 0025-ENGG-2014


Date: 25-05-2016

CODE:
#include "stdafx.h"

#include<iostream>
#include<conio.h>

using namespace std;


int large(int arr[],int lower,int upper)
{
int max;

if(lower==upper)
{
return arr[lower];
}
max=large(arr,lower+1,upper);
if(arr[lower>max])
return arr[lower];
else
return max;
}
int main()
{
int n;
cout<<"enter size of array"<<endl;
cin>>n;
int arr[100];
for(int i=0;i<n;i++)

{
cout<<"enter "<<i+1<<"element"<<endl;
cin>>arr[i];
}
cout<<"array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
cout<<"min element"<<endl;
cout<<large(arr,0,n-1);
getch();
return 0;}
OUTPUT:

LAB 2: Number System


This program will do the conversions of bases i.e. Hexadecimal to Octadecimal and , Octa to Hexa, Binary to Decimal ,
Decimal to Binary
Roll No: 0025-ENGG-2014
Date: 25-05-2016
CODE:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<" For converting "<<endl;
cout<<" Decimal
to binary
press A "<<endl;
cout<<" Decimal
to octal
press B "<<endl;
cout<<" Decimal
to hexadecimal press C "<<endl;
char conversion;
cin>>conversion;
switch(conversion)
{
case 'A':
{
cout<<"conversion of decimal to binary"<<endl;
int dec,rem,i=1,sum=0;
cout<<"enter value in decimal"<<endl;
cin>>dec;
do
{
rem=dec%2;

sum=sum+(i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
cout<<"value in Binary is "<<endl;
cout<<sum<<endl;
break;
}
case 'B':
{
cout<<"conversion of decimal to octal"<<endl;
int octal,to=0;
int i=1;
int total[50];
cout<<"enter value in decimal"<<endl;
cin>>octal;
while(octal>0)
{
to=octal%8;
octal/=8;
total[i++]=to;

}
for(int j=i-1;j>0;j--)
{
cout<<total[j];
}
break;

}
case 'C':
{
int dec,rem,x;
char hex[100];
int i=1,j,y;
cout<<"enter decimal"<<endl;
cin>>dec;
x=dec;
while(x!=0)
{
y=x%16;

if(y<10)
{
y=y+48;
}
else
{
y=y+55;
}
hex[i++]=y;
x=x/16;
}
cout<<"hex"<<endl;
for(j=i-1;j>0;j--)
{
cout<<hex[j];
}}
}
getch();
return 0;
}

OUTPUT:

LAB 3: Sorting and Searching


This program will have the duo combination of Sorting and Searching of Elements in an array
Roll No: 0025-ENGG-2014
Date: 25-05-2016

CODE:
// program.cpp : main project file.

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;

int insertion(int b[5])


{
for(int j=0;j<5+1;j++)

{
cout<<b[j]<<endl;
}
return 0;
}
int deletion(int c[5])
{
for(int l=0;l<4;l++)
{
cout<<c[l]<<endl;
}
return 0;
}
int sort(int array[5])
{
for(int q=0; q<5; q++)
{

cout<<array[q];
cout<<endl;
}
return 0;
}
int search()
{
return 0;
}

int main()
{
cout<<"
cout<<endl;
cout<<endl;

int q=0;

The Grand Program

"

<<endl;

cout<<"

for insertion press 1

"<<endl;

cout<<"

for deletion press 2

"<<endl;

cout<<"

for sorting press 3

"<<endl;

cout<<"

for searching press 4

cout<<endl;

cin>>q;
if(1==q)
{
int a[50];
int n,x,i,index=0;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter array values"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];

"<<endl;

}
cout<<"enter value to be inserted"<<endl;
cin>>x;
int u;
cout<<"enter index value "<<endl;
cin>>u;
for(i=0;i<n;i++)
{
index=i+u;
break;
}
for(i=n+1;i>index;--i)
a[i]=a[i-1];
a[index]=x;
insertion(a);
}
else if (2==q)

{
int a[50];
int n=5;
int i=0;
int k;
cout<<"enter index"<<endl;
cin>>k;
cout<<"Enter values of array"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}

cout<<"array is "<<endl;
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;

while(k<=n)
{
a[k]=a[k+1];
k++;
}
n=n-1;
cout<<"after deletion array is"<<endl;
deletion(a);

}
else if(3==q)
{
int arr[5];
int mini,temp;

cout<<"Enter 5 numbers: "<<endl;


for(int i=0; i<5; i++)
{
cin>>arr[i];
}

cout<<"array numbers: "<<endl;

for(int j=0; j<5; j++)


{
cout<<arr[j];
cout<<endl;
}

for(int r1=0;r1<4;r1++)
{

mini=r1;
for(int r2=r1+1; r2<5; r2++)
if(arr[r2]<arr[mini])

mini=r2;

if(mini !=r1)
{
temp=arr[r1];
arr[r1]=arr[mini];
arr[mini]=temp;
}
}
cout<<"sorted array "<<endl;
sort(arr);
}

else if(4==q)
{
int a[5];

cout<<"enter array values"<<endl;


for(int i=0;i<5;i++)
{
cin>>a[i];
}
int skey;
int found;
int i;
cout<<"enter the search key";
cin>>skey;
for(i=0; i<5; i++)
{

if(a[i]==skey)
{
found=1;
break;
}
}
if(found==1)

cout<<"element found at location="<<i+1;

else
cout<<"element not found"<<endl;
}
getch();
return 0;
}

OUTPUT:

LAB4: Merge sort


This program will perform the divide and conquer sort also known as the Merge Sort
Roll No: 0025-ENGG-2014
Date: 25-05-2016

CODE:
// merge.cpp : main project file
#include "stdafx.h"

#include <iostream>
#include<conio.h>
using namespace std;
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
int mid;

if (low < high)


{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{

if (a[i] < a[j])


{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;

i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[20], i, b[20];
cout<<"enter the elements\n";

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


{
cin>>a[i];
}
mergesort(a, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i]<<endl;
}
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array\n";

for (i = 0; i < 5; i++]


cout<<b[i]<<endl;
}
getch();
}

LAB 5:Min and max using recursion


This program will determine the Minimum and Maximum Number Entered in the Array of Larger Size with the help of
Recursion
Roll No: 0025-ENGG-2014
Date: 25-05-2016

CODE:
// recur.cpp : main project file.

#include "stdafx.h"

#include<iostream>
#include<conio.h>

using namespace std;

int large(int arr[],int lower,int upper)


{
int max;
if(lower==upper)
{
return arr[lower];
}
max=large(arr,lower+1,upper);
if(arr[lower>max])
return arr[lower];
else
return max;
}
int main()
{
int n;
cout<<"enter size of array"<<endl;

cin>>n;
int arr[100];
for(int i=0;i<n;i++)
{
cout<<"enter "<<i+1<<"element"<<endl;
cin>>arr[i];
}
cout<<"array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
cout<<"min element"<<endl;
cout<<large(arr,0,n-1);
getch();
return 0;
}

Das könnte Ihnen auch gefallen