You are on page 1of 8

Programming Fundamentals

Lab-10

For Students:

Roll No

Name

Class

Instructor

_________________________
Mr. Irfan Ullah

_____________________________________________________________________________1
14
Programming Fundamentals - Lab [COSC-1201]
10.1. OBJECTIVES

1. Structures
2. Declaring a Structure
3. Defining Structure Variable
4. Accessing members of structure
5. Initializing a Structure Variable
6. Array of Structure

10.2. Structure
Structure is a collection of variables of different data types under a single name. We can say a
structure as a user defined datatype.

10.3. Declaring a Structure


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.

10.4. Defining Structure Variable


Once you declare a structure person as above. You can define a structure variable as:

Person bill;

Here, a structure variable bill is defined which is of type structure Person.

When structure variable is defined, only then the required memory is allocated by the compiler.

10.5. Accessing member of a Structure


The members of structure variable are accessed using a dot (.) operator.

If we want to access age of structure variable bill and assign 50 to it. You can perform this task
by using following code:

bill.age = 50;

_____________________________________________________________________________1
15
Programming Fundamentals - Lab [COSC-1201]
10.6. Initializing a Structure Variable
A structure variable can be initialized as:

Person bill = {“Name”, 35, 12000.0}

10.7. Example Program


struct part
{
int modelNumber;
int partNumber;
float cost
};
int main( )
{
part part1, part2;
part1.modelNumber = 1111;
part1.partNumber = 111;
part1.cost = 111.11;
part2.modelNumber = 2222;
part2.partNumber = 222;
part2.cost = 222.22;
cout<<“\nModel of Part1 =”<<part1.modelNumber<<endl;
cout<<“\nPart of Part1 =”<<part1.partNumber<<endl;
cout<<“\nCost of Part1 =”<<part1.cost<<endl;
cout<<“\nModel of Part2 =”<<part1.modelNumber<<endl;
cout<<“\nPart of Part2 =”<<part1.partNumber<<endl;
cout<<“\nCost of Part2 =”<<part1.cost<<endl;
return0;
}

10.8. Array of Structures


An array of structure is a type of array in which each element contains a complete structure. It
can be used to store many records.

Syntax:

struct book
{
int bookid;
int pages;
float price;
_____________________________________________________________________________1
16
Programming Fundamentals - Lab [COSC-1201]
};

book b[3];
The above line of code declares a structure Book. Then defines an array of structures b[3]. This
structure now can store the record of three Books.

10.9. LAB TASKS


10.9.1. Program no 1
Write a program that declares a structure to store BookID, price and pages of a book. It defines
two structure variables, input values. And display the record of most costly book.

_____________________________________________________________________________1
17
Programming Fundamentals - Lab [COSC-1201]
10.9.2. Program no 2
Write a program that declares a structure to store roll no and marks of five subjects. It defines a
structure variable, inputs the values and displays roll no, total marks and average marks.

_____________________________________________________________________________1
18
Programming Fundamentals - Lab [COSC-1201]
10.9.3. Program no 3
Write a program that declares a structure to store id, pages and price of a book. It defines an
array of structures to store the records of five books. It inputs the record of five books and

_____________________________________________________________________________1
19
Programming Fundamentals - Lab [COSC-1201]
display the record of most costly and least costly book.

_____________________________________________________________________________1
20
Programming Fundamentals - Lab [COSC-1201]
_____________________________________________________________________________1
21
Programming Fundamentals - Lab [COSC-1201]

You might also like