Skillrack PPS 5

CSE 1002 PPS5

Total time : 600 mins

Challenges : 5

Question 1(College application)

A student may apply for an arts college or an engineering college after his school. Admission to arts college or engineering college, is based on the marks obtained in the six subjects : English, Second language, Maths, Physics, Chemistry and Computer Science.  Both the applications have the following details : application number, name, age, marks in all six subjects and cut-off for the eligibility. For Arts college, cut-off is the   average of  marks in all the subjects and whereas for engineering colleges cut-off is the average of marks in maths, physics, chemistry plus the marks scored in the entrance exam.  Given all the required details, Design an OOP model to compute the cut-off marks and implement it using C++.

Note: Syntax to print ‘x’ decimal places of variable ‘a’

include

use

cout<<fixed<<setprecision(x)<<a;

Input Format

Type of application (0 for arts student and 1 for engineering student)

Application Number

Name of student

Age

Marks scored in English

Marks scored in Second Language

Marks scored in Maths

Marks scored in Physics

Marks scored in Chemistry

Marks scored in Computer Science

Marks scored in entrance exam (only for engineering student)

Output Format

Name of student

Cut-off marks

Solution

#include

#include

using namespace std;

class college_Appln

{

protected:

int appln_No;

char name[30];

int age;

float marks[6];

public:

void get();

void print();

};

class arts_Appln:public college_Appln

{

float entrance;

float cutoff;

public:

void calc_Cutoff();

void print();

};

class engg_Appln:public college_Appln

{

float cutoff;

float entrance;

public:

void get();

void print();

void calc_Cutoff();

};

void college_Appln :: get()
{
    cin>>appln_No>>name>>age;
    for(int i=0;i < 6;i++)   
    cin>>marks[i];
}
void arts_Appln :: calc_Cutoff()
{
    cutoff=0;
    for(int i=0;i < 6;i++)
    cutoff+=marks[i];
    cutoff/=6;
}
void arts_Appln :: print()
{
    cout<<name<<"\n"<<fixed<<setprecision(2)<<cutoff;  
}   
void engg_Appln :: get()   
{  
    cin>>appln_No>>name>>age;
    for(int i=0;i < 6;i++)   
    cin>>marks[i];
    cin>>entrance;
}
void engg_Appln :: calc_Cutoff()
{
    cutoff=entrance+(marks[2]+marks[3]+marks[4])/3;
}
void engg_Appln :: print()
{
    cout<<name<<"\n"<<fixed<<setprecision(2)<<cutoff;
}

int main()

{

int ch;

cin>>ch;

if(ch==0)

{

arts_Appln a;

a.get();

a.calc_Cutoff();

a.print();

}

else

{

engg_Appln e;

e.get();

e.calc_Cutoff();

e.print();

}

}

Diagram

appln

Question 2(Cost of pizza)

Pizza is a delicious circular food item that is a favorite for many people. Given the radius of the pizza, ingredients required for the preparation of the pizza, per square cm (cm2) area of the pizza and cost of its ingredients per 100 grams, design an OOP model and write a C++ program to calculate the cost of the pizza. Add Rs 50 for veg pizza and Rs 100 for chicken pizza. Use 3.14 for pi. Your program should  get all the ingredients available in the kitchen with their cost per 100 grams, as an input.  Assume that all the ingredients required for the preparation of the pizza is available in the kitchen.

Note: Syntax to print ‘x’ decimal places of variable ‘a’

include

use

cout<<fixed<<setprecision(x)<<a;

Input Format

Give 0 for veg pizza and 1 for chicken pizza

Radius of pizza

Number of ingredients required for the preparation of the pizza

Name of ingredient1

Quantity of ingredient1 required (in grams)

Name of ingredient2

Quantity of ingredient2 required

….

Name of ingredient-n

Quantity of ingredient-n required

Number of ingredients available in the kitchen.

Name of ingredient-1 available in the kitchen

Cost of 100 gm of ingredient1 available in kitchen

Name of ingredient-2 in kitchen

Cost of 100 gm of ingredient2 in kitchen

….

Name of ingredient-n in kitchen

Cost of 100 gm of ingredient-n in kitchen

Output Format

Cost of pizza

Solution

#include

using namespace std;

#include

#include

class circle

{

protected:

float radius;

public:

void get_C();

void print_C();

float area();

};

struct ingre_Cost

{

char name[30];

float price;

};

class kitchen

{

protected:

int num1;

//ingredients in the kitchen and their cost

ingre_Cost ing_Cost[20];

public:

void get_K();

void print_K();

//Given name of ingredients

//return cost of 100gm of it

float get_Cost(char*);

};

struct ingre_Qty

{

char name[30];

float qty;

};

class cookeditem

{

protected:

//number of ingredients

int num;

// names of ingredients and their quantity in

// Pizza

ingre_Qty ing_Qty[20];

public:

void get_CI();

void print_CI();

};

//Create a class pizza that inherits circle and cookeditem

//Create another class veg_Pizza inherited that inherits pizza

//Create another class chik_Pizza inherited that inherits pizza

void circle :: get_C()
{
cin>>radius;
}
float circle :: area()
{
return(3.14*radius*radius);
}
void kitchen :: get_K()
{
cin>>num1;
for(int i=0;i < num1;i++)  
cin>>ing_Cost[i].name>>ing_Cost[i].price;
}
float kitchen :: get_Cost(char *n)
{
for(int i=0;i < num1;i++)  
if(strcmp(n,ing_Cost[i].name)==0)  
return(ing_Cost[i].price);  
}  
void cookeditem :: get_CI()  
{  
cin>>num;
for(int i=0;i < num;i++)  
cin>>ing_Qty[i].name>>ing_Qty[i].qty;
}
class pizza : public circle,cookeditem
{
protected :
float p_cost;
void get()
{
get_C();
get_CI();
}
void cost(kitchen k)
{
p_cost=0;
for(int i=0;i < num;i++)
p_cost+=ing_Qty[i].qty*k.get_Cost(ing_Qty[i].name)*0.01*area();
}
};
class veg_Pizza : public pizza
{
public :
void get_P()
{
get();
}
void compute_Cost(kitchen k)
{
cost(k);
}
void print_P()
{
cout<<fixed<<setprecision(2)<<p_cost+50;
}
};
class chik_Pizza : public pizza
{
public :
void get_P()
{
get();
}
void compute_Cost(kitchen k)
{
cost(k);
}
void print_P()
{
cout<<fixed<<setprecision(2)<<p_cost+100;
}
};

int main()

{

int ch;

cin>>ch;

if (ch==0)

{

//Create an oject for veg pizza

veg_Pizza p;

//get radius of circle(pizza)

// get ingredients and quantity required for 1 square centimeter

p.get_P();

//get items in kitchen and their cost

kitchen k;

k.get_K();

//compute cost

p.compute_Cost(k);

p.print_P();

}

else

{

if (ch==1)

{

chik_Pizza c;

c.get_P();

kitchen k1;

k1.get_K();

c.compute_Cost(k1);

c.print_P();

}

}

}

Diagram

pizza

Question 3(Customer Discount)

A retail store is maintaining their customer details such as customer id and name, mobile number and address. They have decided to introduce a preferred customer plan for the existing customer. Any customer is given the status of preferred customer if they have made purchases more than 10 times. Preferred customers earn discounts based on their cumulative purchase amount as follows:

Cumulative Purchase Amount
Discount (%)
>= Rs. 5000
1
>= Rs 10,000
2
>= Rs 15000
3
>= Rs 20000
4
Design an OOP model and implement it using C++ program. Refer the declaration of classes and application program to do the design. The program must get the details, compute bill amount and print details both for ordinary and preferred customers.

For example, if a preferred customer makes three purchases of amounts 3000, 7000, 8000 then the amount to be paid for each bill is 3000, 6860 and 7760 and the total amount to be paid is 17620

Input Format

Type of customer (0 for ordinary customer and 1 for preferred customer)

Name

Mobile number

Address

Customer id

Number of bills

Total Cost of products in bill1

….

Total Cost of products in bill-n

Output Format

Total amount to be paid by the customer for ‘n’ bills

Solution

#include < iostream >
using namespace std;
class details
{
    protected :
    char name[20],mob[20],add[20],id[10];
    int num,bills[20];
    void get_details()
    {
        cin>>name>>mob>>add>>id>>num;
        for(int i=0;i < num;i++)   
        cin>>bills[i];
    }
};
class customer : public details
{
    public :
    void get()
    {
        get_details();
    }
    float calc_Total()
    {
        float total=0;
        for(int i=0;i < num;i++)
        total+=bills[i];
        return(total);
    }
};
class preferred_Customer : public details
{
    public :
    void get()
    {
        get_details();
    }
    float calc_Total()
    {
        float total=0,spent=0;
        for(int i=0;i < num;i++)   
        {  
            spent+=bills[i];   
            if(spent > =20000)
            total+=bills[i]*0.96;
            else if(spent > =15000)
            total+=bills[i]*0.97;
            else if(spent > =10000)
            total+=bills[i]*0.98;
            else if(spent > =5000)
            total+=bills[i]*0.99;
            else
            total+=bills[i];
        }
        return(total);
    }
};

int main()

{

int ch;

//get choice of customer

cin>>ch;

if(ch==0)

{

customer c1;

//get the details of customer

c1.get();

//calculate total amount to be paid

//develop a function in customer class that will calculate

// the total amount and return it

cout<<c1.calc_Total();

}

else

{

//preferred customer

preferred_Customer pc;

//get details

pc.get();

//function should calculate and return total amount

cout<<pc.calc_Total();

}

}

Diagram

customer

Question 4(Polygon in 2D space)

Design a class polygon to represent a polygon in a two dimensional space. Provide member functions to get the details of the polygon and compute area. Area of a polygon with ‘n’ vertices is given by the following formula:

Here the vertices are numbered from 1 to n either clockwise or anticlockwise. The sign of the value changes when numbering is done in the other way. So absolute value is considered.

Derive two classes: triangle and quadrilateral, from the class polygon. Overload the operator [] to get the ‘i-th’ point of the polygon if ‘i’ is less than or equal to the number of vertices ‘n’ and raise an user defined exception outofrange when ‘i’ is greater than ‘n’. Catch the exception in main and print ‘outofrange’.

Input Format

Next six entries are Vertices of a triangle

value of X- coordinate of point1

value of Y- coordinate of point1

….

value of X- coordinate of point6

value of Y- coordinate of point6

Next eight entries are Vertices of a triangle

value of X- coordinate of point1

value of Y- coordinate of point1

….

value of X- coordinate of point8

value of Y- coordinate of point8

index of the vertex  of the polygon, whose coordinate has to be printed.

Output Format

Area of triangle

Area of quadrilateral

Value of X- coordinate and Y- coordinate of point-n separated by tab or print Out of range if index is not in range

Solution

#include

using namespace std;

#include

#include

#include

//Declaration of classes

struct point

{

double x;

double y;

point();

void get();

friend ostream& operator<<(ostream&,point);

};

class outofrange:public exception

{

public:

void what();

};

class polygon

{

protected:

int num_Of_Ver;

point* vertices;

public:

//initialize num_Of_Ver to ‘n’

//allocate memory to store points of ‘n’ vertices

polygon(int n);

~polygon();

//get function cannot be defined here instead define in the derived class

void get();

//we return reference of the point so that assignment can be made

point& operator[](int idx);

double area();

};

class triangle : public polygon

{

public:

triangle():polygon(3){}

};

class quadrilateral : public polygon

{

public:

quadrilateral():polygon(4){}

};

point :: point(){}
void point :: get()
{
    cin>>x>>y;
}
ostream& operator << (ostream& output,point poi)
{
    output<<poi.x<<"\t"<<poi.y;
    return(output);
}
void outofrange :: what()
{
    cout<<"Out of range";
}
polygon :: polygon(int n)
{
    num_Of_Ver=n;
    vertices=new point[n];
}
void polygon :: get()
{
    for(int i=0;i < num_Of_Ver;i++)
    vertices[i].get();
}
point& polygon :: operator [](int idx)
{
    if(idx < num_Of_Ver)
    return(vertices[idx]);
    else
    {
        outofrange o;
        throw(o);
    }
}
double polygon :: area()
{
    double a=0;
    for(int i=0;i < num_Of_Ver-1;i++)
    a+=vertices[i].x*vertices[i+1].y-vertices[i+1].x*vertices[i].y;
    a+=vertices[num_Of_Ver-1].x*vertices[0].y-vertices[num_Of_Ver-1].y*vertices[0].x;
    if(a/2<0)
    return(-a/2);
    return(a/2);
}
polygon :: ~polygon(){}

int main()

{

triangle t;

int index;

t.get();

cout<<fixed<<setprecision(2)<<t.area()<<endl;

quadrilateral r;

r.get();

cout<<fixed<<setprecision(2)<<r.area()<<endl; cin>>index;

try

{

cout<<r[index];

}catch(outofrange o)

{

o.what();

}

}

Diagram

polygon

Question 5(Black coin in board game)

Colored coin game is a 8X8 board game which has many colored coins. Each coin has a weight and power. Power of a coin is defined by the moves that it can make. In a move,  a black coin can move one step vertically upwards.  A red coin can move one step either horizontally, vertically or diagonally. Given the current position of a black coin and list of movements made by it, print all possible next positions of the coin. If the total number of moves made by a black coin is greater than 6, then that coin should be treated as a red coin and the subsequent moves will be as that of the red coin. At any point of time, the coin cannot move outside the board. The rows and columns of the board are numbered as 1 to 8. Print the horizontal movement of coin in an increasing order of columns and print the vertical movement of coin in increasing order of rows. To print, the diagonal movement of the coin refer the sample board config given below. If the current position of your coin is 4,4 then print P1, P2 … P8 in order..

Input Format

Weight of black coin

Current row position of coin

Current column position of coin

Number of moves made by black coin

Output Format

Weight of black coin

List of possible next positions

One position in a line with row and column separated by a comma

Solution

void coin :: get()
{
	cin>>weight>>curr_Y>>curr_X;
}
void coin :: print()
{
	cout<<weight<<"\n"; 
}  
int coin :: get_Curr_X() 
{ 
	return(curr_X); 
}  
int coin :: get_Curr_Y() 
{ 
	return(curr_Y); 
}  
int coin :: get_Weight() 
{ 
	return(weight); 
} 
void coin :: move(){} 
class black_Coin : public coin 
{ 
	public : 
	int moves; 
	void get() 
	{ 
		coin :: get(); 
		cin>>moves;
	}
	bool gained_Power()
	{
		if(moves > =5)
		return(true);
	        return(false);
	}
	void move()
	{
	cout<<curr_Y<<","<<curr_X+1;
	}
};
class red_Coin : public coin
{
	public :
	void set_Curr_Pos_Wt(black_Coin b)
	{
		weight=b.get_Weight();
		curr_X=b.get_Curr_X();
		curr_Y=b.get_Curr_Y();
	}
	void move()
	{
		cout<<curr_Y-1<<","<<curr_X<<"\n";
		cout<<curr_Y+1<<","<<curr_X<<"\n";
		cout<<curr_Y<<","<<curr_X-1<<"\n";
		cout<<curr_Y<<","<<curr_X+1<<"\n";
		cout<<curr_Y-1<<","<<curr_X-1<<"\n";
		cout<<curr_Y+1<<","<<curr_X-1<<"\n";
		cout<<curr_Y-1<<","<<curr_X+1<<"\n";
		cout<<curr_Y+1<<","<<curr_X+1<<"\n";
	}
};

Diagram

coin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s