Skillrack PPS4

CSE 1002 PPS4
11-Mar-2017 00:00
Total time : 600 mins
Challenges : 5

Question 1

The area is the two-dimensional amount of space that an object occupies. Area is measured along the surface of an object and has dimensions of length squared; for example, square feet of material, or centimetres squared.
The area of a rectangle is equal to the height h times the base b; A = h * b
The equation for the area of a trapezoid is one half the sum of the top t and bottom b times the height h; A = h * [ t + b ] / 2
The area of a circle is A = pi * r2, where pi = 3.14 and r = radius.
Develop a program in C++ using function overloading for computing the area of a rectangle, a trapezoid and a circle by a common function name ComputeArea() with different signature. Assume pi = 3.14. Print only two decimal places for all areas.
Note:
To print only two decimal places of a variable ‘a’, do the following:
#include
cout<<fixed<<setprecision(2)<<a;
Input Format:
Read the base and height of a rectangle.
Read the top, bottom and height of a trapezoid.
Read the radius of a circle.
Output Format:
Display the area of a rectangle, trapezoid and circle each in one line
Boundary Conditions:
You can give any valid integer or float values for inputs.

Solution

#include < iostream >
#include < iomanip >
using namespace std;
float area(int l,int b)
{
    return(l*b);
}
float area(int t,int b,int h)
{
    return(h*(t+b)/2);
}
float area(int r)
{
    return(3.14*r*r);
}
int main()
{
float len,bre,top,bottom,height,radius;
double a;
cin>>len>>bre;
a = area(len,bre);
cout<<fixed<<setprecision(2)<<a<<endl; cin>>top>>bottom>>height;
a = area(top,bottom,height);
cout<<fixed<<setprecision(2)<<a<<endl; cin>>radius;
a = area(radius);
cout<<fixed<<setprecision(2)<<a<<endl;
}

Input

INPUT :
area function overloaded with various inputs

Output

OUTPUT :
cout<<setprecision(2)<<area(#inputs);

Processing

float area(int l,int b)
{
    return(l*b);
}
float area(int t,int b,int h)
{
    return(h*(t+b)/2);
}
float area(int r)
{
    return(3.14*r*r);
}

Pseudocode

BEGIN

Read inputs
float area(int l,int b);
float area(int t,int b,int h);
float area(int r);
Output

END

Question 2

Generate boarding pass for the passengers of a ship which starts from Chennai  to Andaman. The boarding pass must be generated automatically with a pass number that begins with “CA” and followed by a number that is automatically incremented from value ‘x’, details like passenger name, age, mobile number, address, date of journey and fare. There is a seasonal discount based on the age of the passengers. Write a non member function called discount which calculates the discount in the fare for the passenger with the following discounts. For the  age group `between 12 and 58, both inclusive’  there is 20% discount in the fare,  for the age group ‘above 58’, there is 40% discount and for the children (age under 12), 50% discount. Write a C++ program to generate pass for ‘n’ users.
Input Format:
Passenger name
Value of ‘x’
Age
Address
date_of_Journey
mobile number
Original Fare
Output Format:
passenger name
Boarding pass number
age
date_of_Journey
mobile number
Total fare after discount based on age
Boundary Conditions:
>=1

Solution

#include < iostream >
using namespace std;
float discount(float init_price,int age)
{
    if(age < 12)
    init_price-=(init_price*0.5);
    else if(age < = 58) 
     init_price-=(init_price*0.2); 
     else 
     init_price-=(init_price*0.4); 
     return(init_price); 
 } 
 class passenger 
 { 
     char name[20],add[30],date[20],number[20]; 
     int age,x; 
     float init_price,final_price; 
     public : 
         passenger(){} 
         void get() 
         { 
             cin>>name>>x>>age>>add>>date>>number>>init_price;
            final_price=discount(init_price,age);
        }
        ~passenger()
        {
            cout<<name<<"\nCA"<<x<<"\n"<<age<<"\n"<<date<<"\n"<<number<<"\n"<<final_price<<"\n";
        }
};
int main()
{
    passenger pas;
    pas.get();
    return(0);
}

Input

INPUT :
void get()
{
    cin>>name>>x>>age>>add>>date>>number>>init_price;
    final_price=discount(init_price,age);
}

Output

OUTPUT :
cout<<name<<"\nCA"<<x<<"\n"<<age<<"\n"<<date<<"\n"<<number<<"\n"<<final_price<<"\n";

Processing

float discount(float init_price,int age)
{
    if(age < 12)
    init_price-=(init_price*0.5);
    else if(age < = 58)
    init_price-=(init_price*0.2);
    else
    init_price-=(init_price*0.4);
    return(init_price);
}

Pseudocode

BEGIN

Read input
p.get();
float discount(float init_price,int age)
{
    if(age < 12)
    init_price-=(init_price*0.5);
    else if(age < = 58)
    init_price-=(init_price*0.2);
    else
    init_price-=(init_price*0.4);
    return(init_price);
}
cout<<name<<"\nCA"<<x<<"\n"<<age<<"\n"<<date<<"\n"<<number<<"\n"<<final_price<<"\n";

END

Question 3

Design a class vector to perform the operations like retrieving value for i-th component from a vector, add two vectors and subtract a vector from another if they are of same dimension. A vector of n-dimension is represented by an n-tuple (a sequence of n numbers). Addition of two vectors of same dimension,  is got by adding   the corresponding components of the two vectors. Similarly, subtraction of the two vectors, v1-v2,  is got by the subtracting the respective components of v2 from the corresponding components of v1. Overload subscript ([]) operator for retrieving the i-th element from a vector, ‘+’ and ‘-‘ for addition and subtraction, << and >> for I/O operations. If the vectors are of different dimension, throw an exception stating “Vectors of different dimension cannot be added”.
Input format:
Dimension of first vector, ‘n1’
First element of first vector
Second element of first vector

n1-th element of first vector
Dimension of second vector, ‘n2’
First element of second vector
Second element of second vector

n2-th element of first vector
value of i
value of j
Output Format
i-th element of vector1
j-th element of vector2
sum of vectors
vector1-vector2

Solution

#include
using namespace std;
class vector
{
int num;
int ele[20];
public:
friend istream& operator>>(istream&,vector&);
friend ostream& operator<<(ostream&,vector&);
vector operator+(vector&);
vector operator-(vector&);
int operator[](int);
};
int vector :: operator [] (int n)
{
    return(ele[n-1]);
}
istream& operator >> (istream& input,vector& vec)
{
    input>>vec.num;
    for(int i=0;i < vec.num;i++) 
    input>>vec.ele[i];
    return(input);
}
ostream& operator << (ostream& output,vector& vec)
{
    for(int i=0;i < vec.num;i++)
    output<<vec.ele[i]<<"\n";
    return(output);
}
vector vector :: operator + (vector& vec)
{
    vector v=vec;
    for(int i=0;i < num;i++)
    v.ele[i]+=ele[i];
    return(v);
}
vector vector :: operator - (vector& vec)
{
    vector v;
    for(int i=0;i < num;i++)
    v.ele[i]=ele[i]-vec.ele[i];
    return(v);
}
int main()
{
vector v1,v2,v3;
int i,j;
cin>>v1;
cin>>v2;
cin>>i;
cin>>j;
cout<<v1[i]<<endl;
cout<<v2[j]<<endl;
v3 = v1+v2;
cout<<v3;
v3 = v1 – v2;
cout<<v3;
}

Input

INPUT :
istream& operator >> (istream& input,vector& vec)
{
    input>>vec.num;
    for(int i=0;i < vec.num;i++) 
    input>>vec.ele[i];
    return(input);
}

Output

OUTPUT :
ostream& operator << (ostream& output,vector& vec)
{
    for(int i=0;i < vec.num;i++)
    output<<vec.ele[i]<<"\n";
    return(output);
}

Processing

vector vector :: operator + (vector& vec)
{
    vector v=vec;
    for(int i=0;i < num;i++)
    v.ele[i]+=ele[i];
    return(v);
}
vector vector :: operator - (vector& vec)
{
    vector v;
    for(int i=0;i < num;i++)
    v.ele[i]=ele[i]-vec.ele[i];
    return(v);
}

Pseudocode

BEGIN

Read input
vector vector :: operator + (vector& vec)
vector vector :: operator - (vector& vec)
Output

END

Question 4

Design a OOP model to represent a point in a two dimensional space and overload the operators >>,<<, > and ==. Given ‘n’ points, design an algorithm and write a C++ code to sort them in descending order. While sorting, a point is said to be greater than the other based on their x-coordinate value. If value of x-coordinate is same for both the points then make a decision based on their value of y-coordinate.
Input Format
Number of points ‘n’
value of X- coordinate of point1
value of Y- coordinate of point1
….
value of X- coordinate of point-n
value of Y- coordinate of point-n
Output Format
Print ‘n’ points in sorted order
value of X- coordinate and Y- coordinate of point1 separated by tab
value of X- coordinate and Y- coordinate of point2 separated by tab
….
value of X- coordinate and Y- coordinate of point-n separated by tab

Solution

#include
using namespace std;
class point
{
int x,y;
public:
friend istream& operator>>(istream&,point&);
friend ostream& operator<<(ostream&,point&); bool operator == (point&); bool operator > (point&);
};
void sort_Points(point*,int);
istream& operator >> (istream& input,point& poi)
{
    input>>poi.x>>poi.y;
    return(input);
}
ostream& operator << (ostream& output,point& poi)
{
    output<<poi.x<<"\t"<<poi.y<<"\n"; 
    return(output);  
} 
bool point :: operator == (point& poi)  
{  
    if(poi.x==x && poi.y > y)
    {
        int temp=poi.y;
        poi.y=y;
        y=temp;
        return(true);
    }
    return(false);
}
bool point :: operator > (point& poi)
{
    if(poi.x > x)
    {
        point temp=poi;
        poi.x=x;
        poi.y=y;
        x=temp.x;
        y=temp.y;
        return(true);
    }
    return(false);
}
void sort_Points(point* poi,int n)
{
    for(int i=0;i < n;i++)
    for(int j=0;j < n;j++)  
    {  
        if(poi[j] > poi[i])
        continue;
        if(poi[j]==poi[i]);
    }
}
main()
{
point p[20];
int i,n;
cin>>n;
for(i=0;i<n;i++) cin>>p[i];
sort_Points(p,n);
for(i=0;i<n;i++)
cout<<p[i];
}

Input

INTPUT :

istream& operator >> (istream& input,point& poi)
{
    input>>poi.x>>poi.y;
    return(input);
}

Output

OUTPUT :

ostream& operator << (ostream& output,point& poi)
{
    output<<poi.x<<"\t"<<poi.y<<"\n";
    return(output);
}

Processing

bool point :: operator == (point& poi)
{
    if(poi.x==x && poi.y > y)
    {
        int temp=poi.y;
        poi.y=y;
        y=temp;
        return(true);
    }
    return(false);
}
bool point :: operator > (point& poi)
{
    if(poi.x > x)
    {
        point temp=poi;
        poi.x=x;
        poi.y=y;
        x=temp.x;
        y=temp.y;
        return(true);
    }
    return(false);
}

Pseudocode

BEGIN

Read input
void sort_Points(point* poi,int n)
{
    for(int i=0;i < n;i++)
    for(int j=0;j < n;j++)  
    {  
        if(poi[j] > poi[i])
        continue;
        if(poi[j]==poi[i]);
    }
}
Output

END

Question 5

Design a class stringpos to perform the operations like retrieving the letter  in the i-th position (i-th symbol) of  a string, rotation of a string,  oddfirst  of a string, evenfirst of a  string, spin of two strings,  oddevenswap of two strings, evenoddswap of two strings.  Here the terms : rotation, oddfirst, evenfirst, are the operations performed on one string. The terms : spin, oddevenswap, evenoddswap are the operations performed on two strings.  With the string S and integer n
Oddevenswap(S1,S2) produces a string such that the  letters occurring in the odd positions  of S1 are  replaced by the letters occurring in the corresponding even positions of S2 and the letters occurring in the even position of S1 are left unchanged. In the above example, after a certain stage,  If there are no even-position letters in S2 to replace the odd-position letters in S1, those odd-position letters are left unreplaced.  For Example, oddevenswap(abcd, efgh)= fbhd, oddevenswap(ab,efgh)=fb, oddevenswap(abcd,fg)= gbcd.  Here also, oddevenswap(S1, S2) need not be same as that of oddevenswap(S2,S1).
Similarly, evenoddwap(S1,S2) produces a string such that the  letters occurring in the even  positions  of S1 are  replaced by the letters occurring in the corresponding odd  positions of S2 and the letters occurring in the odd  position of S1 are left unchanged.  For Example, evenoddswap(abcd, efgh)= aecg, evenoddswap(ab,efgh)=ae, evenoddswap(abcd,fg)= afcd  Here also, evenoddswap(S1, S2) need not be same as that of evenoddswap(S2,S1).
Overload shift left operator for rotation, prefix ++ for oddfirst, postfix ++ for evenfirst, ‘*’ for spin,’+’ operator for Oddevenswap, ‘-‘ operator for Evenoddswap, << and >> for I/O operations.
Input format:
First line contains the first string, S1
Next line contains the second string, S2
Rotation index n
Output Format
rotation(S1,n)
oddfirst(S2)
evenfirst(S1)
Spin(S1,S2)
Oddevenswap(S1,S2)
Evenoddswap(S1,S2)

Solution

#include
#include
using namespace std;
class stringops
{
char str[50];
public:
friend istream& operator>>(istream&,stringops&);
friend ostream& operator<<(ostream&,stringops&); //Return a new string with letters rotated stringops operator>>(int);
//Return a new string with odd letters first
stringops operator++();
//Return a new string with even letters first
stringops operator++(int);
//Return a new string after Oddevenswap
stringops operator + (stringops&);
//Return a new string after Evenoddswap
stringops operator – (stringops&);
//Return a new string after spin
stringops operator * (stringops&);
};
istream& operator >> (istream& input,stringops& string)
{
    input>>string.str;
    return(input);
}
ostream& operator << (ostream& output,stringops& string)
{
    output<<string.str;  
    return(output);  
}  
stringops stringops :: operator >> (int num)
{
    num--;
    stringops new_string;
    for(int i=0;i < strlen(str)+1;i++)  
    {  
        if(num > -1)
        new_string.str[i]=str[num--];
        else
        new_string.str[i]=str[i];
    }
    return(new_string);
}
stringops stringops :: operator ++ ()
{
    stringops new_string;
    int len=0;
    for(int i=0;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    for(int i=1;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    new_string.str[len]='\0';
    return(new_string);
}
stringops stringops :: operator ++(int)
{
    stringops new_string;
    int len=0;
    for(int i=1;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    for(int i=0;i < strlen(str);i+=2)
    new_string.str[len++]=str[i];
    new_string.str[len]='\0';
    return(new_string);
}
stringops stringops :: operator + (stringops& string)
{
    stringops new_string;
    for(int i=0;i < strlen(str);i++)
    {
        if((i+1)%2==1 and i+1 < strlen(string.str))
        new_string.str[i]=string.str[i+1];
        else
        new_string.str[i]=str[i];
    }
    new_string.str[strlen(str)]='\0';
    return(new_string);
}
stringops stringops :: operator - (stringops& string)
{
    stringops new_string;
    for(int i=0;i < strlen(str);i++)
    {
        if((i+1)%2==0 && i-1 < strlen(string.str))
        new_string.str[i]=string.str[i-1];
        else
        new_string.str[i]=str[i];
    }
    new_string.str[strlen(new_string.str)]='\0';
    return(new_string);
}
stringops stringops :: operator * (stringops& string)
{
    stringops new_string;
    int s1=0,s2=0,s3=0;
    while(s1 < strlen(str) && s2 < strlen(string.str))
    {
        new_string.str[s3++]=str[s1++];
        new_string.str[s3++]=string.str[s2++];
    }
    while(s1 < strlen(str))
    new_string.str[s3++]=str[s1++];
    while(s2 < strlen(string.str))
    new_string.str[s3++]=string.str[s2++];
    new_string.str[s3]='\0';
    return(new_string);
}
int main()
{
stringops s1,s2,s3;
int n;
cin>>s1>>s2;
cin>>n;
//rotate
s3= s1>>n;
cout<<s3<<endl;
//oddfirst
s3 = ++s2;
cout<<s3<<endl;
//evenfirst
s3 = s1++;
cout<<s3<<endl;
//spin
s3 = s1*s2;
cout<<s3<<endl;
//Oddeven swap
s3 = s1+s2;
cout<<s3<<endl;
//Evenodd swap
s3 = s1-s2;
cout<<s3<<endl;
}

Input

INPUT :

istream& operator >> (istream& input,stringops& string)
{
    input>>string.str;
    return(input);
}

Output

OUTPUT :

ostream& operator << (ostream& output,stringops& string)
{
    output<<string.str;
    return(output);
}

Processing

stringops stringops :: operator >> (int num)
stringops stringops :: operator ++ ()
stringops stringops :: operator ++(int)
stringops stringops :: operator + (stringops& string)
stringops stringops :: operator - (stringops& string)
stringops stringops :: operator * (stringops& string)

Pseudocode

BEGIN

Read input
stringops stringops :: operator >> (int num)
stringops stringops :: operator ++ ()
stringops stringops :: operator ++(int)
stringops stringops :: operator + (stringops& string)
stringops stringops :: operator - (stringops& string)
stringops stringops :: operator * (stringops& string)
Output

END
Share on Google Plus

About Unknown

This blog is exclusively for VITIAN'S TO fulfill their needs. Also you can tell our team your need and we will assist you as soon as possible. For any quaries send your emails to "everything4vitians@gmail.com". Thank you
    Blogger Comment

0 comments:

Post a Comment