Nothing Special   »   [go: up one dir, main page]

Experiment 03 Introduction To Structures and Nested Structures Objectives

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

EXPERIMENT 03

Introduction to Structures and Nested Structures


Objectives:
 To revise the concepts of structures and pointers in structures.
 To revise the concepts of nested structures.
Equipment /Tool:
PC, Microsoft Visual Studio 2013
Background:

Structures:

Programming languages such as C, C++ and C# support structures. Structures are defined with the
keyword ‘struct’.

Structures can be used to map different real world entities in a computer program e.g., student,
employee etc. They are also used to create custom data types. Arrays have a limitation that all the
elements of an array should be of same data type so structures overcome this limitation. Structures
serve as a building block to implement advanced data structures like linked lists, stacks, queues
and trees etc.
Structure Definition:

A simple structure program is shown below

2
Structures in RAM:

3
4
5
Nested Structures:

In C++, both structures and classes can have member variables and member functions. Both can
be used to map real world entities in the computer program. Both can inherit other classes and
structures. However, a slight difference between structures and classes is that by default every
member in structure is public. What does that mean? On the contrary, every member in class is
by default private. What does that mean? Structures in C++ are different than structures in C?
How?

Like other primitive data types, the memory for the structure can be allocated at compile time.
Any example? Likewise, we can also dynamically allocate the memory for the structure at run
time as shown in the example

6
Structures can be passed to the function just like other primitive types. C++ supports passing
structures to a function by value as well as by reference
Procedure:
Perform all the tasks and provide your code and result in the space below the questions.
Lab Tasks

Q1) Write a program in which a ‘Student’ entity is map having the following attributes:
 Name
 EnrollmentNumber
 CGPA
 DegreeProgram
In main, declare one student entity. Take the values of its members from user and also print them
Answer:
Solution

#include <iostream>
using namespace std;
#include< string.h>
struct student
{
string name, enrollment, degreeprogram;
float CGPA;

};
void main()
{
student s;
cout << "enter information" << endl;
cout << "enter name" << endl;
cin >> s.name;
cout << "enter enrollment" << endl;
cin >> s.enrollment;
cout << "enter degree program" << endl;
cin >> s.degreeprogram;
cout << "enter CGPA" << endl;
cin >> s.CGPA;

cout << "\ndisplay information " << endl;


cout << "name" << s.name << endl;
cout << "enrollment" << s.enrollment << endl;
cout << "degree program" << s.degreeprogram << endl;
cout << "cgpa" << s.CGPA << endl;
system("pause");

7
Q2) Define a structure ‘StudentDetails’ having following attributes
 Name
 EnrollmentNumber
Define another structure ‘StudentResult’ having following attributes
 StudentDetails
 SubjectName
 Marks
In main, declare an object of ‘StudentResult’. Initialize all of its members by taking input from
user. Also, print the entered values as well on console.
Answer:
Solution

#include <iostream>
using namespace std;
#include< string.h>
struct studentDetail
{
string name, enrollment;

};
struct studentresult
{
string studentdetail;
string subjectname;
int marks;
};
void main()
{
studentDetail s;

cout << "enter student Detail " << endl;


cout << "enter name" << endl;
cin >> s.name;
cout << "enter enrollment" << endl;
cin >> s.enrollment;
studentresult r;

cout << "student result" << endl;


cout << "student detail" << endl;
cin >> r.studentdetail;
cout << "subject name" << endl;
cin >> r.subjectname;
cout << "marks" << endl;
cin >> r.marks;

8
cout << "\ndisplay information " << endl;
cout << "name" << s.name << endl;
cout << "enrollment" << s.enrollment << endl;
cout << "student detail" << r.studentdetail << endl;
cout << "subject name" << r.subjectname << endl;
cout << "marks" << r.marks << endl;
system("pause");
}

Q3) Make a structure ‘Shape’ that should have following functions:


 float area(float radius);
 float area(float height, float width);
Make an instance of the structure in main and use both functions to compute the area of circle,
rectangle and square respectively. Take the required parameters from user or you can also
randomly assign them.
Answer:
Solution

include<iostream>
#include<string>
#include<conio.h>
using namespace std;
struct SHAPE{
float area(float radius)
{ float a = 0;
a = 3.14*radius*radius;
return a;
};
float area(float height, float width)
{ float b = 0;
b = height*width;
return b;
};
};
void main()
{
SHAPE S1; float ra, he, wi;

9
cout << "Enter the Radius of The Circle:" << endl;
cin >> ra;
cout << endl;
cout << "Enter The Height of The Rectangle/Sqaure:" << endl;
cin >> he;
cout << endl;
cout << "Enter The Width of The Rectangle/Square:" << endl;
cin >> wi;
cout << endl;
S1.area(ra);
cout << "Area of The Circle is:" << S1.area(ra);
cout << endl;
if (he == wi)
{
S1.area(he, wi);
cout << "Area of the Square is:" << S1.area(he, wi);
cout << endl;
}
else { S1.area(he, wi); cout << "Area of The Rectangle is:" << S1.area(he, wi);
};
_getch();
}

Q4) Write a program in which a ‘Student’ entity is map having the following attributes:
 Name
 EnrollmentNumber
 CGPA
 DegreeProgram
Reserve the memory for 2 students at compile time and make a menu which should allow user to
add the student data one by one.
Answer:
Solution
#include <iostream>
using namespace std;
#include< string.h>
struct student
{
string name, enrollment, degreeprogram;
float CGPA;

};
void main()
{
student s;
cout << "enter information" << endl;
cout << "enter name" << endl;
cin >> s.name;

10
cout << "enter enrollment" << endl;
cin >> s.enrollment;
cout << "enter degree program" << endl;
cin >> s.degreeprogram;
cout << "enter CGPA" << endl;
cin >> s.CGPA;

cout << "\ndisplay information " << endl;


cout << "name" << s.name << endl;
cout << "enrollment" << s.enrollment << endl;
cout << "degree program" << s.degreeprogram << endl;
cout << "cgpa" << s.CGPA << endl;
system("pause");
cout << "do you want to continue"<<" yes "<<" no" << endl;
student r;
cout << "enter information" << endl;
cout << "enter name" << endl;
cin >> r.name;
cout << "enter enrollment" << endl;
cin >> r.enrollment;
cout << "enter degree program" << endl;
cin >> r.degreeprogram;
cout << "enter CGPA" << endl;
cin >> r.CGPA;

cout << "\ndisplay information " << endl;


cout << "name" << r.name << endl;
cout << "enrollment" << r.enrollment << endl;
cout << "degree program" << r.degreeprogram << endl;
cout << "cgpa" << r.CGPA << endl;
system("pause");
cout << "do you want to continue" << " yes or no" << endl;
student p;
cout << "enter information" << endl;
cout << "enter name" << endl;
cin >> p.name;
cout << "enter enrollment" << endl;
cin >> p.enrollment;
cout << "enter degree program" << endl;
cin >> p.degreeprogram;
cout << "enter CGPA" << endl;
cin >> p.CGPA;

cout << "\ndisplay information " << endl;


cout << "name" << p.name << endl;
cout << "enrollment" << p.enrollment << endl;
cout << "degree program" << p.degreeprogram << endl;
cout << "cgpa" << p.CGPA << endl;
system("pause");

11
Q5) Modify Question 4 by dynamically reserving the memory for students as per user desire.
Afterwards, make a menu which should allow user to add the student data one by one.
Also, the menu should allow user to search for any particular student record through the ‘Name’
attribute. Make two separate functions for adding the student data and for searching the student
record.
Answer:
Solution

#include<iostream>
#include<string>
#include<conio.h>
using namespace std;

struct Students{
string Name;
string Enrollment;
float CGPA;
string DegreeProgram;
};
void main()
{
int sum;
cout << " please enter the number of students for data entery....." << endl;
cin >> sum;
Students * ptr; ptr = new Students[sum]; Students *ddr = &ptr[0];
int n, m, i = 0, t = 0, q = t;
for (n = i; n == i; n++){

cout << " please enter The Name of Student....." << endl;
cin >> (*(ddr + n)).Name;
cout << endl;
cout << " please enter The Enrollment......" << endl;
cin >> (*(ddr + n)).Enrollment;
cout << endl;
cout << "please enter The CGPA......" << endl;
cin >> (*(ddr + n)).CGPA;
cout << endl;
cout << " please enter The Degree Program......" << endl;
cin >> (*(ddr + n)).DegreeProgram; cout << endl;
}
{i++; }
while (q == t){
cout << " for new entry press one else press zero......." << endl;
cin >> m;
if (m == 1)
{
if (n < sum){

for (n = i; n == i; n++){

cout << " please nter The Name of Student....." << endl;
cin >> (*(ddr + n)).Name;
cout << endl;
cout << " please enter The Enrollment....." << endl;
cin >> (*(ddr + n)).Enrollment;
cout << endl;
cout << " please enter The CGPA....." << endl;
cin >> (*(ddr + n)).CGPA;

12
cout << endl;
cout << " please enter The Degree Program....." << endl;
cin >> (*(ddr + n)).DegreeProgram; cout << endl;

}i++;
}
else cout << " memory almost full......" << endl; cout << endl;
}
else if (m == 0){
string Name;
cout << " please enter The Name of Student to check the Data........" <<
endl; cout << endl;
cin >> Name;
cout << endl;
for (int j = 0; j <sum; j++){

if (Name == (*(ddr + j)).Name) {


cout << " please ehe Name of Student....." << endl;
cout << (*(ddr + j)).Name;
cout << endl;
cout << " The Enrollment ....." << endl;
cout << (*(ddr + j)).Enrollment;
cout << endl;
cout << " The CGPA...." << endl;
cout << (*(ddr + j)).CGPA;
cout << endl;
cout << " The Degree Program..." << endl;
cout << (*(ddr + j)).DegreeProgram;
cout << endl;
}

}
}
cout << " for main menu press one .............. for exit pree zero... " << endl;
cin >> t; cout << endl;
}
_getch();
}

Additional Tasks:
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
13
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………
……………………………………………………………

14

You might also like