Strcuture Practice
Strcuture Practice
Code:
#include <iostream>
#include<string.h>
using namespace std ;
struct movies_record{
char name[100];
char year[10];
char director[100];
char budget[100];
float rating ;
void Display(){
cout<<"Movie name: "<<name<<endl;
cout<<"Year of release: "<<year<<endl;
cout<<"Name of Director: "<<director<<endl;
cout<<"Budget: "<<budget<<endl;
cout<<"Rating out of 4: "<<rating;
}
};
int main() {
movies_record movie[10];
int n ;
cout<<"Enter total number of movies: ";
cin>>n;
cin.ignore();
for (int i=0 ; i<n ; i++){
cout<<"Movie name: ";
cin.getline(movie[i].name , 100);
cout<<"Year of release: ";
cin.getline(movie[i].year , 100);
cout<<"Director name: ";
cin.getline(movie[i].director , 100);
cout<<"Budget: ";
cin.getline(movie[i].budget , 100);
cout<<"Rating: ";
cin>>movie[i].rating ;
cin.ignore();
}
for (int i=0 ; i<n ; i++){
movie[i].Display();
cout<<endl;
}
return 0;
}
OUTPUT:
Task:4(1)
Code:
#include<iostream>
using namespace std;
struct Point {
float x;
float y;
Point (){
x=0;
y = 0;
}
Point (float a , float b){
x=a;
y = b;
}
void Display(){
cout<<"("<<x<<","<<y<<")";
}
};
int main ()
{
Point P_1(4,9),P_2;
P_1.Display();
cout<<endl;
P_2.Display();
}
OUTPUT:
Task:4(2)
Code:
#include<iostream>
using namespace std;
//int Check(Point P);
struct Point {
float x;
float y;
Point (){
x=0;
y = 0;
}
Point (float a , float b){
x=a;
y = b;
}
void Display(){
cout<<"("<<x<<","<<y<<")";
}
};
int Check(Point P);
int main ()
{
Point P_1(5,2) ;
P_1.Display();
cout<<endl;
cout<<"Point is in "<<Check( P_1)<<" Quadrant";
}
int Check(Point P){
if (P.x>=0 && P.y>=0){
return 1;
}
else if (P.x<0 && P.y>=0){
return 2;
}
else if (P.x<0 && P.y<0){
return 3;
}
else if (P.x>=0 && P.y<0) {
return 4;
}
}
OUTPUT:
Task:4(3)
Code:
#include<iostream>
#include<math.h>
using namespace std;
struct Point {
float x;
float y;
Point (){
x=0;
y = 0;
}
Point (float a , float b){
x=a;
y = b;
}
void Display(){
cout<<"("<<x<<","<<y<<")";
}
};
float Distance(Point P_1 , Point P_2);
int main ()
{
Point P_1(5,2) , P_2(3,5) ;
P_1.Display();
cout<<endl;
P_2.Display();
cout<<endl<<"Distance between two points is: ";
cout<<Distance( P_1 , P_2);
}
float Distance(Point P_1 , Point P_2){
float x , y ;
x = (P_2.x - P_1.x)*(P_2.x - P_1.x);
y = (P_2.y - P_1.y)*(P_2.y - P_1.y);
return sqrt(x+y);
}
OUTPUT:
Task:4(5)
Code:
#include<iostream>
#include<math.h>
using namespace std;
struct Point {
float x;
float y;
Point (){
x=0;
y = 0;
}
Point (float a , float b){
x=a;
y = b;
}
void Display(){
cout<<"("<<x<<","<<y<<")";
}
Point operator + (Point P){
Point P_3;
P_3.x = x + P.x ;
P_3.y = y + P.y ;
return P_3;
}
};
int main ()
{
Point P_1(4,3) , P_2(5,6) , P ;
P_1.Display();
cout<<endl;
P_2.Display();
P = P_1 + P_2;
cout<<endl<<"New Point obtained from Head to Tail rule is: "<<endl;
P.Display();
}
OUTPUT:
Task:5(1)
Code:
#include<iostream>
#include<math.h>
using namespace std;
struct Mass{
float kg ;
float g;
float mg;
Mass(){
kg=0;
g=0;
mg=0;
}
Mass(float a , float b , float c){
kg = a ;
g = b;
mg = c ;
}
void Display(){
cout<<kg<<"Kg "<<g<<"g "<<mg<<"mg";
}
};
int main () {
Mass m_1(8,7,6) , m_2;
m_1.Display();
cout<<endl;
m_2.Display();
}
OUTPUT:
Task:5(2-a)
Code:
#include<iostream>
#include<math.h>
using namespace std;
struct Mass{
float kg ;
float g;
float mg;
Mass(){
kg=0;
g=0;
mg=0;
}
Mass(float a , float b , float c){
kg = a ;
g = b;
mg = c ;
}
void Display(){
cout<<kg<<"Kg "<<g<<"g "<<mg<<"mg";
}
Mass operator + (Mass M ){
Mass m ;
m.kg = kg + M.kg;
m.g = g + M.g;
m.mg = mg + M.mg;
m.normalize();
return m;
}
void normalize (){
g = g + static_cast<int>(mg)/1000;
mg = static_cast<int>(mg) % 1000;
kg = kg + static_cast<int>(g)/1000;
g = static_cast<int>(g) % 1000;
}
};
int main () {
Mass m_1(8,500,600) , m_2(6,540 ,600), m_3;
m_1.Display();
cout<<endl;
m_2.Display();
m_3 = m_1 + m_2 ;
cout<<endl<<"Resultant Mass is: ";
m_3.Display();
}
OUTPUT:
Task:5(2-b)
Code:
#include<iostream>
#include<math.h>
using namespace std;
struct Mass{
float kg ;
float g;
float mg;
Mass(){
kg=0;
g=0;
mg=0;
}
Mass(float a , float b , float c){
kg = a ;
g = b;
mg = c ;
}
void Display(){
Task:5(2-c)