0% found this document useful (0 votes)
32 views

Lecture 10

Uploaded by

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

Lecture 10

Uploaded by

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

Lecture 11:

10: Programming Fundamentals:2012

Lecture 11

Structure

University Institute of Information Technology, PMAS-AAUR 1


Lecture 10: Programming Fundamentals:2012

Structure Concept
• Although arrays greatly improved our ability to
store data, there is one major drawback to their
use ... each element (each box) in an array must be
of the same data type.
• It is often desirable to group data of different types
and work with that grouped data as one entity.
• We now have the power to accomplish this
grouping with a new data type called a structure.

University Institute of Information Technology, PMAS-AAUR 2


Lecture 10: Programming Fundamentals:2012

Structure Concept
• A structure is a collection of variable types
grouped together.
• You can refer to a structure as a single variable, and
to its parts as members of that variable by using
the dot (.) operator.
• The power of structures lies in the fact that once
defined, the structure name becomes a user-
defined data type and may be used the same way
as other built-in data types, such as int, double,
char.
University Institute of Information Technology, PMAS-AAUR 3
Lecture 10: Programming Fundamentals:2012

How to declare and create a Structure


• The structure is declared by using the keyword
struct followed by structure name, also called a tag.
• Then the structure members (variables) are
defined with their type and variable names inside
the open and close braces "{"and "}“ and must be
closed with semicolon.

• The above structure declaration is also called a


Structure Specifier.

University Institute of Information Technology, PMAS-AAUR 4


Lecture 10: Programming Fundamentals:2012

Example
• Three variables:
– custnum of type int,
– salary of type int,
– commission of type float
–Are structure members and the structure name is Customer. This
structure is declared as follows:

University Institute of Information Technology, PMAS-AAUR 5


Lecture 10: Programming Fundamentals:2012

How to Access Structure Members


• Similar to variable declaration.
– For variable declaration, data type is defined followed by
variable name.
– For structure variable declaration, the data type is the name
of the structure followed by the structure variable name.

• In the above example, structure variable cust1


is defined as:

University Institute of Information Technology, PMAS-AAUR 6


Lecture 10: Programming Fundamentals:2012

• A programmer wants to assign 2000 for the


structure member salary in the above example
of structure Customer with structure variable
cust1 this is written as:

University Institute of Information Technology, PMAS-AAUR 7


Lecture 10: Programming Fundamentals:2012

Example
• Write a program that declares a structure to store Roll No., Marks, average and grade of student. The program should
define a structure variable, inputs the values and then displays values.

main()
{
struct Student cout<<"You enter the following data "<<endl;
{ cout<<"Roll No. "<<scr.rno<<endl;
int rno;
cout<<"Marks "<<scr.marks<<endl;
int marks;
cout<<"Average "<<scr.avg<<endl;
float avg;
cout<<"Grade "<<scr.grade<<endl;
char grade;
}
};
Student scr;
cout<<"Enter Roll No. ";
cin>>scr.rno;
cout<<"Enter Marks ";
cin>>scr.marks;
cout<<"Enter Average ";
cin>>scr.avg;
cout<<"Enter Grade ";
cin>>scr.grade;

University Institute of Information Technology, PMAS-AAUR 8


Lecture 10: Programming Fundamentals:2012

Program to define a structure with 5


members. The first member be a student
name and other be marks obtained in
subjects. Assign values to a members during
their declaration. Calculate total marks and
then print the numbers and the total marks of
students.

University Institute of Information Technology, PMAS-AAUR 9


Lecture 10: Programming Fundamentals:2012

struct result
{
Char name[15];
int s1, s2, s3, s4;
};
result student = {“Ali”, 70, 40, 90, 78};
int total = student. s1 + student.s2 + student.s3 + student.s4;

cout<<“Name of Student : “ << student.name<<endl;


cout<<“Total Marks :“<<total<<endl;

University Institute of Information Technology, PMAS-AAUR 10


Lecture 10: Programming Fundamentals:2012

Write a program that declare a structure to


store day, month and year of birth date. It
input three values and displays date of birth in
dd/mm/yy format.

University Institute of Information Technology, PMAS-AAUR 11


Lecture 10: Programming Fundamentals:2012

struct birth
{
int day;
Int month;
float year;
};
birth b;
cout<<“ Enter day of birth “;
cin>>b.day;
cout<<“ Enter month of birth “;
cin>>b.month;
cout<<“ Enter year of birth “;
cin>>b.year;
cout<<“your date of birth is”<<b.day<<“/”<<b.month<<“/”<<b.year;
University Institute of Information Technology, PMAS-AAUR 12
Lecture 10: Programming Fundamentals:2012

Write a program that declare a structure to


store Book ID, price and pages of a book. It
then display record of most costly book.

University Institute of Information Technology, PMAS-AAUR 13


Lecture 10: Programming Fundamentals:2012

struct book
{
int id;
Int pages;
float price;
};
book b1, b2;
cout<<“ Enter id, pages and price of book 1“;
cin>>b1.id>>b1.pages>>b1.price;
cout<<“ Enter id, pages and price of book 2“;
cin>>b2.id>>b2.pages>>b2.price;
cout<<“ The most costly book is “;
if(b1.price>b2.price)
{
cout<<“First Book”<<b1.id<<b1.pages<<b1.price<< “ has high cost”;
}
Else
{
cout<<“Second Book”<<b2.id<<b2.pages<<b2.price<< “ has high cost”;
}
University Institute of Information Technology, PMAS-AAUR 14
Lecture 10: Programming Fundamentals:2012

Array type Member Structure


• The member of structure may be of different
types.
• These can also be simple variables or array
variables.
struct record
{
char name [15];
int sub[4];
};
University Institute of Information Technology, PMAS-AAUR 15
Lecture 10: Programming Fundamentals:2012

struct record
{
char name [15];
int Roll_No;
int sub[5];
};

Ahmed

0 1 2 3 4

Roll No.

University Institute of Information Technology, PMAS-AAUR 16


Lecture 10: Programming Fundamentals:2012

Write a program that declare a structure to


store 3 Books ID, price and pages. It then
display record of most costly book.

University Institute of Information Technology, PMAS-AAUR 17


Lecture 10: Programming Fundamentals:2012

struct book max = b[i].price;


{
int id;
m = 0;
Int pages; for(i = 0; i<3; i++)
float price; if(b[i].price>max)
};
book b[3];
{
int i, max, m max = b[i].price;
for(i = 0; i<3; i++) m = i;
{
}
cout<<“ Enter book ID“; cout<<“Costly book is ”;
cin>>b[i].id; cout <<b[m].id<<endl;
cout<<“ Enter pages of book “;
cin>>b[i].pages; cout<<b[m].pages<<endl;
cout<<“ Enter the price of book“; cout<<<<b[m].price”;
cin>>b[i].price; }
}

University Institute of Information Technology, PMAS-AAUR 18


Lecture 10: Programming Fundamentals:2012

struct result total = student.sub[0] + student.sub[1]


{ +
char name[15]; student.sub[2] + student.sub[3] ;
int sub[4];
}; cout<<“Name of Student“
<<student.name<<endl;
result student;
int i, total;
for(i=0; i<=3; i++)
Cout<< “Enter student name”; {
cout<<"Marks
Cin>> student.name;
"<<student.sub[i]<<endl;
for(i=0; i<=3; i++)
}
{
cout<<“Total Marks "<<total<<endl;
cout<< “Enter the marks of subject “<<I + 1<< “ = “;

cin>> student.sub[i]; }
}
University Institute of Information Technology, PMAS-AAUR 19
Lecture 10: Programming Fundamentals:2012

Example

Write a program that declare a structure to


store roll no. and marks of five subjects. It
then display Roll No., marks and average
marks.

University Institute of Information Technology, PMAS-AAUR 20


Lecture 10: Programming Fundamentals:2012

Struct Test for( i = 0; i<5; i++)


{ {
int rno; cout<< “Enter Marks “;
int marks[5]; cin>> r.marks[i];
total = total + r.marks[i];
};
}
Test r;
avg = total/5;
int i; total = 0; cout<< “Roll No. “<<r.rno<<endl;
float avg = 0; cout<<“Total Marks “<<total<<endl;
cout<< “Roll Number “; cout<<“Average “<<avg;
cin>> r.rno[i]; }
University Institute of Information Technology, PMAS-AAUR 21
Lecture 10: Programming Fundamentals:2012

NESTED STRUCTURE

University Institute of Information Technology, PMAS-AAUR 22


Lecture 10: Programming Fundamentals:2012

• A Structure with in another Structure is called


Nested Structure.
• A nested structure is created when the
member of a structure is itself a structure.

University Institute of Information Technology, PMAS-AAUR 23


Lecture 10: Programming Fundamentals:2012

Example
struct A
{
int n;
float b;
};
struct B
{
char c;
A x;
};
University Institute of Information Technology, PMAS-AAUR 24
Lecture 10: Programming Fundamentals:2012

Example
struct result
{ cout<<"Roll No.
int marks;
char grades;
"<<rec.rno<<endl;
};
struct record
cout<<"Marks
{
int rno;
"<<rec.r.marks<<endl;
};
result r;
cout<<"Grade
record rec; "<<rec.r.grade<<endl;
cout<<"Enter Roll No. ";
cin>>rec.rno;
cout<<"Enter Marks ";
cin>>rec.marks;
cout<<"Enter grade ";
cin>>rec.grade;

University Institute of Information Technology, PMAS-AAUR 25


Lecture 10: Programming Fundamentals:2012

Write a program using nested structure that


display the data of a person in following format.
Phonebook

Name City Phone Birthday

Day Month Years

University Institute of Information Technology, PMAS-AAUR 26


Lecture 10: Programming Fundamentals:2012

struct date cout<<“Enter Name “;


{ cin>>abc.name;
int day; cou<<“Enter City “;
int month; cin>>abc.city;
int year; cout<<“Enter phone No. “;
}; cin>>abc.tel;
struct phonebook cout<<“Enter Date of Birth in following format (DD/MM/YY) “;
{ cin>>abc.birthday>>abc.month>>abc.year;
char name[20];
char city[15];
int tel;
cout<<“ The Entry made is “;
date birthday;
cout<<abc.name << “----”<<abc.city<<“----”<<abc.tel;
};
cout<<“Birthday is”<<abc.birthday.day << “----”<<
phonebook abc;
abc.birthday.month<< “----”<< abc.birthday.year

University Institute of Information Technology, PMAS-AAUR 27


Lecture 10: Programming Fundamentals:2012

UNION & ENUMERATION

University Institute of Information Technology, PMAS-AAUR 28


Lecture 10: Programming Fundamentals:2012

Classification of Data Types


• Two types of Data Types:
• Built-in data types
– Fundamental data types (int, char, double,
float, void, pointer)
– Derived data types (array, string, structure)

• Programmer/User-defined data types


– Structure
– Union
– Enumeration

University Institute of Information Technology, PMAS-AAUR 29


Lecture 10: Programming Fundamentals:2012

Basic Data Types


C++ Data Types

User-defined Type Built-in Type Derived Type


structure array
union function
class pointer
enumeration reference

Integral Type Void Floating Type

int char float


University Institute of Information Technology, PMAS-AAUR
double
30
Lecture 10: Programming Fundamentals:2012

Enumerated Data Type:


Enumerated data type provides a way for attaching
names to numbers.
enum keyword automatically enumerates a list of
words by assigning them values 0, 1, 2, and so on.
For Example:
• enum shape {circle, square, triangle};
• enum colour {red, blue, green, yellow};
• enum position {off, on};

University Institute of Information Technology, PMAS-AAUR 31


Lecture 10: Programming Fundamentals:2012

Syntax
· The syntax for declaring an enumeration data type is:

– value1, value2, … are identifiers called enumerators


– value1 < value2 < value3 <...
OR

enum typeName{obj1, obj2, ...};

University Institute of Information Technology, PMAS-AAUR 32


Lecture 10: Programming Fundamentals:2012

Example
enum year {january, feburary, march, april,
may, june, july, august, september, october,
november, december};
year y;
y = march;
cout<<“ The value of y is “<<y;

Output:
The value of y is 2

University Institute of Information Technology, PMAS-AAUR 33


Lecture 10: Programming Fundamentals:2012

Example Continue
Example No. 2

Example No. 3

University Institute of Information Technology, PMAS-AAUR 34


Lecture 10: Programming Fundamentals:2012

Example Continue
• . Example No. 4

Example No. 5

University Institute of Information Technology, PMAS-AAUR 35

You might also like