12 @home

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Sri Krishna College of Engineering and Technology

Name :Dinesh Karthikeyan G M Email :[email protected]


Roll no:23ee026 Phone :9942227000
Branch :SKCET Department :EEE-A
Batch :2023_27 Degree :BE-EEE

2023_27_I_PS using CPP_IRC

PSCPP_Friend_PH

Attempt : 1
Total Mark : 50
Marks Obtained : 50

Section 1 : Coding

1. Create two classes A and B with x and y as their private attributes.


Both classes have a public member function
setdata(int) - Assign the values to x and y
Declare a friend function in both the classes - void min(int,int) - prints the
minimum of x and y.
Create a driver class main and access the methods.

Answer
// You are using GCC
#include<iostream>
using namespace std;
class B;
class A
{
private:
int x;
public:
void setdata(int t)
{
x=t;
}
friend void min(A,B);
};
class B {
private:
int y;
public:
void setdata(int h )
{
y=h;
}

friend void min(A,B);


};
void min(A c,B d) {
if(c.x < d.y)
{
cout<<c.x;
}
else
{
cout<<d.y;
}}
int main() {
A c;
B d;
int x,y;
cin>>x>>y;
c.setdata(x);
d.setdata(y);
min(c,d);

Status : Correct Marks : 10/10

2. Problem statement :
Student structure
Create a structure student with the following members
Roll Number
Five subject marks
Average
Grade
Given the five subject marks, Calculate the average and grade.
GRADE CALCULATION:
1)if avg>70 the grade will be 1
2)if avg 50 to 70 the grade will be 2
3)if avg is below 50 the grade will be 3 (Note: rn- Roll Number, s-Subjects,
avg- Average)

Answer
// You are using GCC
#include<iostream>
using namespace std;
struct student
{
int rn;
int s1,s2,s3,s4,s5;
float avg;
}c;
int main()
{
int n;
cin>>n;
for (int i=0;i<n;i++)
{
cin>>c.rn;
cin>>c.s1>>c.s2>>c.s3>>c.s4>>c.s5;
int grade;
c.avg=((c.s1+c.s2+c.s3+c.s4+c.s5)/5);
cout<<c.rn<<" "<<c.s1<<" "<<c.s2<<" "<<c.s3<<" "<<c.s4<<" "<<c.s5<<" "<<c.avg<<"
";
if(c.avg>70)
{
cout<<"1";
}
else if(c.avg<70&&c.avg>50)
{
cout<<"2";
}
else
{
cout<<"3";
}
}
}

Status : Correct Marks : 10/10

3. Problem Statements:
Write a program to find the difference between two time periods using
structures.

Answer
// You are using GCC
#include<iostream>
using namespace std;
struct Diff
{
int t1,m1,s1;
int t2,m2,s2;
}x;
int main()
{
cin>>x.t1>>x.m1>>x.s1;
cin>>x.t2>>x.m2>>x.s2;
if(x.t1>24||x.t2>24||x.m1>60||x.m2>60||x.s1>60||x.s2>60)
{
cout<<"-1";
}
else
{
int tot1=((x.t1*3600)+(x.m1*60)+x.s1);
int tot2=((x.t2*3600)+(x.m2*60)+x.s2);
int di=tot1-tot2;
int h=di/3600;
int m=(di%3600)/60;
int s=(di%60);

cout<<"Difference: "<<x.t1<<":"<<x.m1<<":"<<x.s1<<"-"<<x.t2<<":"<<x.m2<<":"<<x.s2
<<"="<<h<<":"<<m<<":"<<s;
}
}

Status : Correct Marks : 10/10

4. Problem Statement:
In an online grocery shop, customers want to purchase multiple items.
Create a structure to store the Item code, Brand name, Item Name,
Quantity, Price of the product. Generate the Bill number, Display the
purchased product, name, amount and quantity, and the total bill amount.
a. Write a function MESSAGE() to alert the customer with the product
name if the rate of a product is more than Rs.1000.
b. Write a function VOUCHER() to generate the voucher for Rs.200 if the bill
amount is greater than Rs.10000.

Answer
// You are using GCC
#include<iostream>
#include<string>
using namespace std;
struct win
{
int ic,qu,pr;
string br,it;
}f;
void message(win items)
{
if(items.pr*items.qu>1000)
{
cout<<items.it<<" costs more than 1000"<<endl;
}
}
void voucher(float tot)
{
if(tot>10000)
{
cout<<"You have won a voucher of Rs.200";
}
else
{
cout<<"No voucher";
}
}
int main()
{
int n;
cin>>n;
float tot=0;
win item[n];
for(int i=0;i<n;i++)
{
cin>>item[i].ic>>item[i].br>>item[i].it>>item[i].qu>>item[i].pr;

tot+=item[i].qu*item[i].pr;
message(item[i]);
}
cout<<tot<<endl;
voucher(tot);
return 0;
}

Status : Correct Marks : 10/10

5. Problem Statement:
Write a program to add two distances (in inch-feet) system using
structures.

Answer
// You are using GCC
#include<iostream>
using namespace std;
struct feet
{
int f1;
float i1;
int f2;
float i2;
}t;
int main()
{
cin>>t.f1>>t.i1>>t.f2>>t.i2;
int a=t.f1+t.f2;
float b=t.i1+t.i2;
if(b>=12)
{
b-=12;
a++;
cout<<"Sum: "<<a<<" feet, "<<b<<"inches.";
}
else
{
cout<<"Sum: "<<a<<" feet, "<<(float)b<<" inches.";
}
}

Status : Correct Marks : 10/10

You might also like