You are on page 1of 16

Name:V.

Bharani Akash
Reg No:21BCS0002
Slot:L31+32
Course code : csc2002
1. Write a function called “Element_Search” to search an element in
The given integer array and return whether the element exist or not.
Overload the same function by accepting double values and strings.

Code:
#include<iostream>
#include<string.h>
Using namespace std;

Int Element_Search(int i[],int n,int x);


Double Element_Search(double d[],int n,double x);
Char Element_Search(char str[],int n,char x);

Int main()
{
Int i1[20],i,j,n;
Double d1[20],d;
Char str1[20],str;
Bool result;
Cout<<”\nEnter the number of elements : “;
Cin>>n;
Cout<<”\nEnter the integer : “;
For(j=0;j<n;j++)
{
Cin>>i1[j];
}
Cout<<”\nEnter the element to search: “;
Cin>>i;
Result=Element_Search(i1,n,i);
If(result==1)
Cout<<”\nelement is Found \n”;
Else
Cout<<”\nelement is not Found \n”;
Cout<<”\n\nEnter the number of elements: “;
Cin>>n;
Cout<<”\nEnter the double array: “;
For(j=0;j<n;j++)
{
Cin>>d1[j];
}
Cout<<”\nEnter the element to search: “;
Cin>>d;
Result=Element_Search(d1,n,d);
If(result==1)
Cout<<”\nelement is Found \n”;
Else
Cout<<”\nelement is not Found \n”;
Cout<<”\n\nEnter the string: “;
Cin>>str1;
N=strlen(str1);
Cout<<”Enter the element to search: “;
Cin>>str;
Result=Element_Search(str1,n,str);
If(result==1)
Cout<<”\nelement is Found \n”;
Else
Cout<<”\nelement is not Found \n”;
}
Int Element_Search(int i[],int n,int x)
{
For(int j=0;j<n;j++)
{
If(i[j]==x)
Return 1;
}
Return 0;
}
Double Element_Search(double d[],int n,double x)
{
For(int i=0;i<n;i++)
{
If(d[i]==x)
Return 1;
}
Return 0;
}
Char Element_Search(char str[],int n,char x)
{
For(int i;i<n;i++)
{
If(str[i]==x)
Return x;
}
Return 0;
}
2. Consider the system of measuring the distance by feet and inches.
Develop an OOP to create two Distance objects and compare them to
Produce the results between the Distance object as “Larger” or “Equal”
Or “Smaller” with the difference on their distance.

Code:
#include<iostream>
Using namespace std;
Class Distance{
Private:
Int feet;
Int inches;
Public:
Void get();
Void display();
Void compare(class Distance dd);

};

Void Distance::get()
{
Cout << “\n\nEnter the feet and inches: “;
Cin >> feet >> inches;
}

Void Distance::display()
{
Cout <<”\nDistance:”<<feet<<” feets “ <<inches<<” inches”;
}
Int main()
{

Class Distance d1,d2;


D1.get();
D2.get();
D1.compare(d2);

}
Void Distance::compare(class Distance dd)
{
Int flag;
Float temp,temp1;
Temp = feet + inches/12;
Temp1 = dd.feet + dd.inches/12;
If(temp > temp1)

{
Feet = feet – dd.feet;
Inches = inches – dd.inches;
If(inches<0)
{
Inches=12+inches;
Feet-=1;
}
Cout << “ \nDistance 1 is greater by “<<feet<<” feet “<<inches<<”
inches “;
}
Else if( temp1 > temp)
{
Feet = dd.feet – feet;
Inches = dd.inches – inches;
If(inches<0)
{
Inches=12+inches;
Feet-=1;
}
Cout << “\nDistance 2 is greater by “<<feet<<” feet
“<<inches<<” inches “;;

}
Else
{
Cout << “\n The two distance are equal”;
}

}
4. an OOP to get the purchase date of a machine and find the Age of
machine currently in months.

Code:
#include<iostream>
Using namespace std;
Class Machine{
Private:
Int bought_year;
Int present_year;

Public:
Void get();
Void display();
Void cal_age();

};

Void Machine::get()
{
Cout << “Enter the year in which the machine is bought: “;
Cin >> bought_year;
Cout << “Enter the current year : “;
Cin >> present_year;

}
Void Machine::display()
{
Cout << “\nThe machine was bought in the year:
“<<bought_year;
Cout << “\nPresent Year: “<<present_year;
}
Void Machine::cal_age()
{
Int final_year;
Final_year = (present_year – bought_year)*12;
Cout << “\nThe age of the machine in months : “<<final_year<<”
Months”;

}
Int main()
{
Class Machine d1;
D1.get();
D1.display();
D1.cal_age();
}

You might also like