0% found this document useful (0 votes)
11 views18 pages

Lab Manual #2 Abdullah Emam Structure

This document is a lab manual for a C++ programming course focusing on structures. It covers how to define and use structures, access their members, and includes several programming exercises related to student information, book records, and distance calculations. The manual also provides examples and outlines lab performance and submission criteria.

Uploaded by

Abdullah Emam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

Lab Manual #2 Abdullah Emam Structure

This document is a lab manual for a C++ programming course focusing on structures. It covers how to define and use structures, access their members, and includes several programming exercises related to student information, book records, and distance calculations. The manual also provides examples and outlines lab performance and submission criteria.

Uploaded by

Abdullah Emam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lab # 2 – Data Structure

Lab Manual # 2

Title: C++ Structures

Student Name: ……………………………………………………………….…………


Student Group: …………………………………………………………………………

Lab Instructor : Abdullah Emam


Lab Performed In : Hall E Lab

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 Information Technology Program


Lab # 2 – Data Structure
Structure is the collection of variables of different types under a single name for better visualization of
problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can
hold data of one or more types.

How to define a structure in C++ programming?

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 {

char name [50];

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.

How to define a structure variable?

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.

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
How to access members of a structure?

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;

Example: C++ Structure

Program to assign data to members of a structure variable and display it.

#include<iostream>

usingnamespacestd;

struct person{

char name[50];

int age;

float salary;

};

int main(){

person p1;

cout<<"Enter Full name: ";

cin.get(p1.name,50);

cout<<"Enter age: ";

cin>> p1.age;

cout<<"Enter salary: ";

cin>> p1.salary;

cout<<"\nDisplaying Information."<<endl;

cout<<"Name: "<< p1.name <<endl;

cout<<"Age: "<< p1.age <<endl;

Version 1.0 Information Technology Program


Lab # 2 – Data Structure

cout<<"Salary: "<< p1.salary;

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.

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
P-1: Write a C++ Program to Store Information of Students Using Structure. At least 4 students.

Your Code:

#include <iostream>
#include <string>

using namespace std;

// Define the Student structure


struct Student {
string name;
int roll_number;
int age;
double total_marks;
};

// Main function
int main() {
int i = 0, n = 5;

// Create an array of students


Student student[n];

// Assign student data


student[0].roll_number = 1;
student[0].name = "Haalim";
student[0].age = 12;
student[0].total_marks = 78.50;

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;

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
student[4].roll_number = 3;
student[4].name = "Usama";
student[4].age = 13;
student[4].total_marks = 78.55;

// Print student information


cout << "Student Records:\n\n";
for (i = 0; i < n; i++) {
cout << "\tName = " << student[i].name << endl;
cout << "\tRoll Number = " << student[i].roll_number << endl;
cout << "\tAge = " << student[i].age << endl;
cout << "\tTotal Marks = " << student[i].total_marks << endl << endl;
}

return 0;
}

#include <iostream>
#include <string>

using namespace std;

// Define a structure for Student


struct Student {
string name;
int roll_number;
int age;
double total_marks;

// Function to display student information


void display() {
cout << "\tName = " << name << endl;
cout << "\tRoll Number = " << roll_number << endl;
cout << "\tAge = " << age << endl;
cout << "\tTotal Marks = " << total_marks << endl << endl;
}
};
// Main function
int main() {
// Number of students
const int n = 5;

// Creating an array of students and initializing data


Student students[n] = {

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
{"Haalim", 1, 12, 78.50},
{"Gabrial", 5, 10, 56.84},
{"Haan", 2, 11, 87.94},
{"Saghar", 4, 12, 89.78},
{"Usama", 3, 13, 78.55}
};

// Printing student records


cout << "Student Records:\n\n";
for (int i = 0; i < n; i++) {
students[i].display();
}
return 0;
}

Paste your output here

Version 1.0 Information Technology Program


Lab # 2 – Data Structure

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

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
char name[50];
int id;
char subject[100];
};

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;

cout << "\nDisplaying Information," << endl;


cout << "Name: " << s.name << endl;
cout << "id: " << s.id << endl;
cout << "Subject name: " << s.subject << endl;
return 0;
}

Paste your output here

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
P-3: Write a C++ Program to Add Two Distances (in inch-feet) System Using Structures If inch is
greater than 12, changing it to feet.

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;

cout << "\nEnter information for 2nd distance" << endl;


cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;

sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;

// changing to feet if inch is greater than 12


if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}

cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
Paste your output here

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;

// Input number of subjects

cout << "Enter number of subjects\n";


cin >> subjects;

// Take marks of subjects as input


cout << "Enter marks of subjects\n";

for(i = 0; i < subjects; i++){


cin >> marks;
total += marks;
}

// Calculate Average

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
averageMarks = total / subjects;

// Each subject is of 100 Marks


percentage = (total/(subjects * 100)) * 100;

cout << "Total Marks = " << total;


cout << "\nAverage Marks = " << averageMarks;
cout << "\nPercentage = " << percentage;

return 0;
}

Paste your output here

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”.

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
5: Create a structure to store an employee's ID, name, department, and salary.
• Store information for multiple employees.
• Write functions to: o Find and print the names of employees in a specific
department.
o Calculate and print the average salary of all employees.
o Display the details of an employee given their ID.

Version 1.0 Information Technology Program


Lab # 2 – Data Structure

Version 1.0 Information Technology Program


Lab # 2 – Data Structure
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 Information Technology Program


Lab # 2 – Data Structure
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 Information Technology Program


Lab # 2 – Data Structure
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:

Paste your output here

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:

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 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:

Paste your output here

Version 1.0 Information Technology Program


Lab # 2 – Data Structure

P-4 Library Management System


Problem Statement:
Create a structure to store a book’s ID, title, author, and price. Implement functions to:
1️-Add a new book to the library
2️-Search for a book by title
3️-Display all books in the library
4- Menu-driven program for user-friendly navigation to select a certain function and Keeps
running until the user chooses to exit

p- 5 Student Report Card System


Problem Statement:
Create a structure to store a student’s ID, name, and marks in 3️ subjects. Implement functions
to:
1️-Add student records
2️-Calculate and display total marks & percentage
3️-Find the topper (highest total marks)
4- Menu-driven program for user-friendly navigation to select a certain function and Keeps
running until the user chooses to exit

p- 6 Hospital Patient Record System


Problem Statement:
Create a structure to store a patient’s ID, name, age, and disease. Implement functions to:
1️-Add a new patient record
2️-Search for a patient by ID
3️-Display all patient records
4 - Menu-driven program for user-friendly navigation to select a certain function and Keeps
running until the user chooses to exit

Version 1.0 Information Technology Program

You might also like