PF CS1 lab 11
PF CS1 lab 11
Programming Fundamentals
Lab Manual # 11
Lab Manual for Programming Fundamentals
Lab 11: Introduction to Structures
Objectives:
1. To learn how to declare and use C++ struct data type.
2. To learn about C- Strings (Character Array)
Introduction to Structures
What is an object?
Objects have states and behaviours. Example: A dog has states - color, name, breed as well as behaviors –
wagging the tail, barking, eating. An object is an instance of a class.
Software objects also have a state and a behavior. A software object's state is stored in fields and behavior
is shown via methods.
For example;
Int dollars=0;
Here we have defined a single instance of type int and given the name dollars.Variable dollars is an object
of type int.
struct TypeName
{
DataType MemberName;
DataType MemberName;
};
Sample Code
struct product {
int weight;
double price;
};
product apple;
product banana, melon;
This declares a structure type, called product, and defines it having two members: weight and price,
each of a different fundamental type. This declaration creates a new type ( product), which is then used to
declare three objects (variables) of this type: apple, banana, and melon. Note how once product is
declared, it is used just like any other type.
Example:
The member_list is where you describe the types of data that are to be associated with the object
that you are defining.
For example, the member list for Student is:
string name;
int id;
int mark[3]
This code just defines the format of the structure. In order to start using this particular structure you
need to declare an instance of it. This is similar to defining a variable of a predefined type such as
int.
Declaring a Structure Instance
To create an instance of a structure -you need to declare it, just as you would declare an instance
of a primitive data type. Following the example referred to in the previous section, the statement
Student stu;
Declares an instance called stu of the structure called Student. Note that the general syntax would
be:
datatype variable_name;
Think back to how you declare an instance of an integer .
i.e. int i; // datatype variable_name;
Sample Program
Struct person
{
Char name [20];
Int age,
Char degree [20]
};
Int `main ()
{
Person sara = {“sara khan”,20,“BSSE”};
Return 0;
}
Sample Program
//Using a BOOK Structure
#include <iostream>
struct Book
{
char title[80];
char author[80];
char publisher[80];
int publishingYear;
};
int main()
{
Book oopBook1 = { "Beginning C++","Ivor Horton’s","Wrox",1998};
oopBook2.publishingYear += 2;
cout<<endl
<<"Title of oopBook2 = "<<oopBook2.title<<endl
<<"Author of oopBook2 = "<<oopBook2.author<<endl
<<"Publisher of oopBook2 = "<<oopBook2.publisher<<endl
<<"Publishing Year of oopBook2 = "<<oopBook2.publishingYear<<endl;
return 0;
4 Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).
5.Further Reading
6.6 Books
5 The slides and reading material can be accessed from the folder of the class instructor available at
vle.
Task 1:
Declare a structure Product. It has following members
1. Code (int)
2. Description(string)
3. Packaging (char [L, S, M for small, medium, large])
4. Price (float)
5. Discount (float: 2 means 2% discount on the original price)
Create an array of 10 products and initialize it by hardcoding the values. Now print the details of
all those products in Large packaging whose net price (after applying discount) is between 200 –
1000
Task 2:
Declare a structure Contact that has a name, address, mobile number. Now create and
(hardcoded) initialize an array of 10 contact instances. Prompt the user to enter the starting
letters of the name to be searched. Your program must now print the details of only those
contacts whose name start with the letter specified by the user. E.g. if the user enters S
(independent of case) then your program will print all that contacts whose name start with sa
i.e. saima, sara, saad, sarmad etc. but if user entered sar then only sara and sarmad would be
printed
Task 3:
Write a function that reads the 20 values from the user and prints the greatest one.
Task 4:
Declare a two-dimensional array which can be used to store a yearly budget. Each row of the array
corresponds to a particular budgeted item like rent, electric, gas. Each column of the array
corresponds to a month, January, February, etc. Of course there are 12 columns corresponding to
the 12 months of the year. All the data to be placed in the array consists of real numbers.
Task 5:
Write a simple currency converter program using a structure object to represent a currency. For
your purposes, a currency object needs to tie together two things: a currency type and an amount
that will convert the currency to dollars by multiplication. Design a structure that a user can use to
represent currency objects, and write a program that allows the user to convert dollar into rupees.