You are on page 1of 2

LAB MANUAL # 8

Course: Computer Programming (CP-107)


Session: 21CP

Structures
This lab has been designed to enable students to practice how to create and use structures in
c++. In this lab students will create structures, initialize structures, and will access structure
members using dot operator. Clear concept about all these things has already been provided
in the class along with examples.
Objectives:
In this lab, you will practice:
• Creating structures.
• Creating structure instances.
• Initializing structures.
• Accessing structure members using dot operator.
• Using arrays within structures
• Using arrays of structures
• Using structure within structure

Lab Tasks:
Question # 1:
Write a program to define a structure with 5 members. The first member be student name
and the other four members be student marks obtained in four subjects. Input values from
the user. Add marks of the subjects and calculate the total marks and then print these
individual subject marks as well as total marks of the student.

Question # 2:
Write a program to input data for 10 students and each student has following data to be
stored.
• Name of Student
• Age of Student.
• Phone No.
• Father Name.
Also display the entered data
Question # 3:
A phone number, such as (051) 9047-599 can be thought of as having three parts: the area
code (051), the exchange (9047) and the number 599. Write a program that uses a structure
to store these three parts separately. Name the structure Phone. Create two instances of
type Phone. Initialize one and have the user input a number for the other one. The display
both numbers in following format:
• (051) 9047-599
LAB MANUAL # 8
Course: Computer Programming (CP-107)
Session: 21CP

Question # 4:
Create a structure and name it DOB that contains three members: the day, the month and
the year, all of type int. Let the user enter his date of birth and store it in variable of type DOB.
Calculate age of a person after taking input values.

Question # 5:
Create a structure called Time. Its three members, all type int, should be called hours, minutes
and seconds. Write a program that prompts the user to enter a time value in hours, minutes
and seconds. The values should be stored in variable of type Time. The program should finally
print out the total number of seconds represented by this time value.

Question # 6:
Create a structure Student that has four members; roll number, age, session and marks of
four subjects (take an array to record marks of 4 subjects). Create two instances of Student.
Get values from user and calculate average marks of both students.
Question # 7:
Create a four-function calculator for fractions. Create a structure Fraction having two
members, denominator, and enumerator. Ask the user to enter two fractions and an operator
( +,-,*,/). Program should calculate result of arithmetic operations using following formulas.

Operator Formula
Addition + 𝒂 𝒄 (𝒂 × 𝒅) + (𝒃 × 𝒄)
+ =
𝒃 𝒅 (𝒃 × 𝒅)
Subtraction - 𝒂 𝒄 (𝒂 × 𝒅) − (𝒃 × 𝒄)
− =
𝒃 𝒅 (𝒃 × 𝒅)
Multuplication * 𝒂 𝒄 (𝒂 × 𝒄)
× =
𝒃 𝒅 (𝒃 × 𝒅)
Division / 𝒂 𝒄 (𝒂 × 𝒅)
/ =
𝒃 𝒅 (𝒃 × 𝒄)

Question # 8:
Create a structure Book having two members; book name and boom id. Create another
structure Student having three members; name (string), roll number (int) and books(Book)
that he issued. Create three instance of type Student and print their values.

***************

You might also like