Lab Manual #2 Abdullah Emam Structure
Lab Manual #2 Abdullah Emam Structure
Lab Manual # 2
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
The struct keyword defines a structure type followed by an identifier (name of the structure). Then inside
the curly braces, you can declare one or more members (declare variables inside curly braces) of that
structure. For example:
struct person {
int age;
float salary;
};
Here a structure person is defined which has three members: name, age and salary.
When a structure is created, no memory is allocated. The structure definition is only the blueprint for the
creating of variables. You can imagine it as a datatype. When you define an integer as below:
intA;
The int specifies that, variable A can hold integer element only. Similarly, structure definition only
specifies that, what property a structure variable holds when it is defined.
Once you declare a structure person as above. You can define a structure variable as:
personB;
Here, a structure variable B is defined which is of type structure person. When structure variable is defined, then
only the required memory is allocated by the compiler. Considering you have either 32-bit or 64-bit system, the
memory of float is 4 bytes, memory of int is 4 bytes and memory of char is 1 byte. Hence, 58 bytes of memory is
allocated for structure variable B.
The members of structure variable are accessed using dot operator. Suppose, you want to access age of
structure variable B and assign it 50 to it. You can perform this task by using following code below:
B.age = 50;
#include<iostream>
usingnamespacestd;
struct person{
char name[50];
int age;
float salary;
};
int main(){
person p1;
cin.get(p1.name,50);
cin>> p1.age;
cin>> p1.salary;
cout<<"\nDisplaying Information."<<endl;
return0;
} Here a structure person is declared which has three members. Inside main() function, a structure variable p1 is
defined. Then, the user is asked to enter information and data entered by user is displayed.
Your Code:
#include <iostream>
#include <string>
// Main function
int main() {
int i = 0, n = 5;
student[1].roll_number = 5;
student[1].name = "Gabrial";
student[1].age = 10;
student[1].total_marks = 56.84;
student[2].roll_number = 2;
student[2].name = "Haan";
student[2].age = 11;
student[2].total_marks = 87.94;
student[3].roll_number = 4;
student[3].name = "Saghar";
student[3].age = 12;
student[3].total_marks = 89.78;
return 0;
}
#include <iostream>
#include <string>
P-2: Write a program to define structure with four members. The first member is book Title, the
other be author name, the book id and the subject name. Assign values to the members during
their declaration and display them.
Your Code:
#include <iostream>
using namespace std;
struct members
{
char title[50];
int main()
{
members s;
cout << "Enter Book title," << endl;
cin>> s.title;
cout << "Enter Author name: ";
cin >> s.name;
cout << "Enter id: ";
cin >> s.id;
cout << "Enter the subject name: ";
cin >> s.subject;
Your Code:
#include <iostream>
using namespace std;
struct Distance{
int feet;
float inch;
}d1 , d2, sum;
int main()
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;
sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;
cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}
P-4: Write a program to define structure with five members . The first member be student name
and the other be marks obtained in subjects. Assign values to the members during their
declaration. Add the marks of the subjects to calculate total marks and then prints these numbers
and the total marks of the students.
Your Code:
#include <iostream>
using namespace std;
int main(){
int subjects, i;
float marks, total=0.0f, averageMarks, percentage;
// Calculate Average
return 0;
}
Comments:
In this lab we studied about how to define a structure in C++ programming
how to access members of a structure?
1. Array elements are accessed using the Subscript variable , Similarly Structure
members are accessed using dot [.] operator.
2. (.) is called as “Structure member Operator”.
3. Use this Operator in between “Structure name” & “member name”.
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;
Your Code:
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:
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 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: