[go: up one dir, main page]

0% found this document useful (0 votes)
5 views9 pages

PF CS1 lab 11

The document is a lab manual for Programming Fundamentals focused on C++ structures and C-Strings. It covers the declaration and usage of structures, provides sample code, and outlines tasks for students to complete related to structures and data types. Additionally, it includes evaluation criteria and further reading resources.

Uploaded by

64419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

PF CS1 lab 11

The document is a lab manual for Programming Fundamentals focused on C++ structures and C-Strings. It covers the declaration and usage of structures, provides sample code, and outlines tasks for students to complete related to structures and data types. Additionally, it includes evaluation criteria and further reading resources.

Uploaded by

64419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Faculty of Computing

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.

Creating our own Data Types


In C++ we can define our own data types and specify the operations that can be applied to them. The types
that we defined are referred to as user defined data types. These are structures and unions. We use
keywords struct to create them.
Structures
A structure is a collection of elements of various types. A data structure is a group of data elements
grouped together under one name. Each of the elements in a structure is called a data member. Data
member can have different types and different lengths Each member is accessed through the use of the
member selector operator. In C++, a structure is commonly declared according to the following syntax:

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>

using namespace std;

//Structure to Represent a Box

struct Book
{
char title[80];
char author[80];
char publisher[80];
int publishingYear;
};

//Prototype of function to calculate the volume of a box

int main()
{
Book oopBook1 = { "Beginning C++","Ivor Horton’s","Wrox",1998};

cout<<endl<<"Title of oopBook1 = "<<oopBook1.title<<endl


<<"Author of oopBook1 = "<<oopBook1.author<<endl
<<"Publisher of oopBook1 = "<<oopBook1.publisher<<endl
<<"Publishing Year of oopBook1 = "<<oopBook1.publishingYear<<endl;

Book oopBook2= {"OOP in C++",


"Robert Lafore",
"SAMS",
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).

Table 3: Evaluation of the Lab


Sr. No. Task No Description Marks
2 1 Task 1 2
3 2 Task 2 2
4 3 Task 3 2
5 4 Task 4 4

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.

9.1 Out comes


The outcomes of this lab were:
 Structures
 Class
 object
 new datatype
Lab 11 TASKS: Structures

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.

You might also like