You are on page 1of 12

Lab # 12 – Programming Fundamentals (CPE-121)

Lab Manual # 12

Title: C++ Nested Structures

CLO: CLO-1

Student Name: M.Usama Saghar Class Roll: 2019-CPE-27

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

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

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)
Nested Structure

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()
{

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)
int i;
Employee E;

cout<<"Enter Employee Id : "<<endl;


cin>>E.Id;
cout<<"Enter Employee Name : "<<endl;
cin>>E.Name;
cout<<"Enter Employee Salary : "<<endl;
cin>>E.Salary;
cout<<"Enter Employee House No : "<<endl;
cin>>E.Add.HouseNo;

cout<<"Enter Employee City : "<<endl;


cin>>E.Add.City;
cout<<"Enter Employee House No : "<<endl;
cin>>E.Add.PinCode;

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;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

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:

// C Program to Store Information


// of Students Using Structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Create the student structure


struct Student {
char* name;
int roll_number;
char* grade;
double total_marks;
};

// Driver code
int main()
{
int i = 0, n = 5;

// Create the student's structure variable


// with n Student's records
struct Student student[n];

// Get the students data


student[0].roll_number = 27;
student[0].name = "saghar";
student[0].grade = "A";
student[0].total_marks = 78.50;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)
student[1].roll_number = 5;
student[1].name = "Usama";
student[1].grade = "B";
student[1].total_marks = 56.84;

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;

// Print the Students information


printf("Student Records:\n\n");
for (i = 0; i < n; i++) {
printf("\tName = %s\n", student[i].name);
printf("\tRoll Number = %d\n", student[i].roll_number);
printf("\tAge = %d\n", student[i].grade);
printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
}

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

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;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

Paste your output here

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

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)
variable of GradeRec in StudentRec structure.The program should output each student’s
information followed by the percentage and the relevant grade. It should also find and print the
lowest test score and the name of the student having the lowest test score.

Your Code:

// This program demonstrates the use of a record (C++ struct)


#include <iostream>

struct StudentRec

string lastName;
string firstName;
int age;

};

void main(void)

{
StudentRec theStudent;

cout << "Enter first name: ";

cin >> theStudent.firstName;


cout << "Enter last name: ";
cin >> theStudent.lastName;
cout << "Enter age: ";

cin >> theStudent.age;

cout << "\n\nHello " << theStudent.firstName << ' '

<< 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;
};

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

struct StudentRec

{
string lastName;
string firstName;
int age;
GradeRec courseGrade;

};

void main(void)

{
StudentRec student;

cout << "Enter first name: ";

cin >> student.firstName;


cout << "Enter last name: ";
cin >> student.lastName;
cout << "Enter age: ";
cin >> student.age;
cout << "Enter overall percent: ";
cin >> student.courseGrade.percent;
if(student.courseGrade.percent >= 90)
{
student.courseGrade.grade = 'A';
}
else if(student.courseGrade.percent >= 75)
{
student.courseGrade.grade = 'B';
}
else
{
student.courseGrade.grade = 'F';
}

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;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 12 – Programming Fundamentals (CPE-121)

Comments:

In this lab we study about structures in detail.


Structure is a collection of variables of different data types under a single name. It is similar to a
class in that, both holds a collecion of data of different data types. For example: You want to
store some information about a person: his/her name, citizenship number and salary.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like