Lab Manual # 11: Title: C++ Structures Clo: Clo-1
Lab Manual # 11: Title: C++ Structures Clo: Clo-1
Lab Manual # 11: Title: C++ Structures Clo: Clo-1
Lab Manual # 11
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
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.
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;
};
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver code
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[4].roll_number = 3;
student[4].name = "Usama";
student[4].age = 13;
student[4].total_marks = 78.55;
return 0;
}
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:
struct members
{
char title[50];
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;
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()
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;
// Calculate Average
averageMarks = total / subjects;
return 0;
}
Comments:
In this lab we studied about how to define a structure in C++ programming
how to access members of a structure?