Lab#12 2019-CPE-27 M.USAMA SAGHAR
Lab#12 2019-CPE-27 M.USAMA SAGHAR
Lab#12 2019-CPE-27 M.USAMA SAGHAR
Lab Manual # 12
CLO: CLO-1
Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results
No of Checks
SUB TOTAL
TOTAL SCORE
______________________
Course Instructor / Lab Engineer
When a structure contains another structure, it is called nested structure. For example,we have
two structures named Address and Employee. To make Address nested to Employee, we have
to define Address structure before and outside Employee structure and create an object of
Address structure inside Employee structure.
Syntax:
struct structure1
{
----------
----------
};
struct structure2
{
----------
----------
structure1obj;
};
Example:1Nested Structure
#include<iostream>
Using namespace std;
struct Address
{
charHouseNo[25];
char City[25];
charPinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
Address Add;
};
void main()
{
cout<<"Details of Employees";
cout<<"Employee Id : "<<E.Id<<endl;
cout<<"Employee Name : "<<E.Name<<endl;
cout<<"Employee Salary : "<<E.Salary<<endl;
cout<<"Employee House No : "<<E.Add.HouseNo<<endl;
cout<<"Employee City : "<<E.Add.City<<endl;
cout<<"Employee House No : "<<E.Add.PinCode<<endl;
P-1: Write a program that uses two structures Results and Record. The Result structure stores
marks and grade, Record structure stores roll number and a Result type. The program declares
a variable of type Record, inputs roll number, marks and grade. It finally displays these values
on the screen.
Your Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver code
int main()
{
int i = 0, n = 5;
student[2].roll_number = 2;
student[2].name = "Haalim";
student[2].grade = "A";
student[2].total_marks = 87.94;
student[3].roll_number = 4;
student[3].name = "Haan";
student[3].grade = "A";
student[3].total_marks = 89.78;
student[4].roll_number = 3;
student[4].name = "Solo";
student[4].grade = "B";
student[4].total_marks = 78.55;
return 0;
}
P-2 Write a program that uses three structures Dimension, Results and Rectangle. The
Dimension structure stores length and width, Result structure stores area and perimeter and
Rectangle stores two variables of Dimension and Results. The program declares a variable of
type Rectangle, inputs length and width, calculates area and perimeter and then display the
results
Your Code:
#include "iostream"
#include "math.h"
using namespace std;
struct Dimensions
{
float length;
float width;
};
struct Result
{
float area;
float perimeter;
};
struct Rectangle
{
Dimensions size ;
Result status;
float area;
};
int main()
{
Rectangle rectangle1;
cout << "\t\t\t enter length of rectangle " << "\n";
cin >> rectangle1.size.length;
cout<< "\t\t\t enter width of rectangle " << "\n";
cin >> rectangle1.size.width;
rectangle1.area = rectangle1.size.length*rectangle1.size.width;
cout << "\t\t\t area of rectangle is " << rectangle1.area <<"\n";
rectangle1.status.perimeter = (2 * rectangle1.size.length) + (2 *
rectangle1.size.width);
cout << "\t\t\t perimeter of retangle is " << rectangle1.status.perimeter << "\n";
system("pause");
return 0;
}
P-3 Write a program that uses two structures GradeRec and StudentRec. The GradeRec
structure stores percentage and grade,StudentRec stores information of student. Declare a
Your Code:
struct StudentRec
string lastName;
string firstName;
int age;
};
void main(void)
{
StudentRec theStudent;
<< theStudent.lastName << ". How are you?\n"; cout << "\nCongratulations on reaching
the age of "
<< theStudent.age << ".\n";
}
// This program demonstrates the use of a nested struct struct GradeRec
{
float percent; char grade;
};
struct StudentRec
{
string lastName;
string firstName;
int age;
GradeRec courseGrade;
};
void main(void)
{
StudentRec student;
cout << "\n\nHello " << student.firstName << ' ' << student.lastName << ". How are you?\n";
cout << "\nCongratulations on reaching the age of " << student.age << ".\n";
cout << "Your overall percent score is "
<< student.courseGrade.percent << " for a grade of "
<< student.courseGrade.grade;
Comments: