Sie sind auf Seite 1von 18

CHAPTER 3

RECORD
Or
STRUCTURE

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Definition of a record
It is a collection of non-homogenous
(different types of) related data
items.
Each item of a record is called a
field or attribute.
Related data items of a person may
constitute a record.
MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Items of a Structure
The following items may constitute
a structure:
Items
Type
Persons ID
Numeric
Name
Character
Telephone no.
Numeric
Phone bill
Numeric
MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Record in C/C++
Record in C/C++ is called structure.
A structure can be defined as follows:
struct
{
keyword
int id;
char name[35];
int phone;
int bill;
An array
} person;
structure variable
Here, name is an array of 35 characters.
An array may be a member (field) of a structure (record).

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Record in C/C++
Record in C/C++ is called structure.
A structure can be defined as follows:
struct
{
keyword
int rollno;
char name[35];
int marks;
An array
}stud;
structure variable
Here, name is an array of 35 characters.
An array may be a member (field) of a structure (record).

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Memory requirement for a record


struct
{
int rollno;
char name[35];
int marks;
}stud;
Name of field

Type of field

rollno

int

name

char

marks

int
Total

MD. RAFIQUL ISLAM

Required space
in byte
2
1 X 35= 35
2
= 39 bytes

DATA STRUCTURE FUNDAMENTALS

Record in C/C++
We can assign value to the members of the structure
(stud) as follows:
stud.rollno = 5011;
stud.name[ ] = H.M. Mehedi Hasan;
stud.marks = 50;
In other way we can use scanf() to store (assign) data to members
of a structure as follows.
scanf (%d, &stud.rollno);
gets (name);
scanf(%d, &stud.marks);

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

Array of structures
Example:
struct
{
int rollno;
char name[40];
int marks;
}stud [30];
Here, stud is an array of structures where
members are roll_no, name[ ] and marks, here
Name[ ] is an array.

Question:

Calculate the space (memory) required for the above code.

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

RECORD
If there is an array of structures, we can assign the
members of the structure (stud) as follows:
stud [2].rollno = 5011;
stud [2].name[ ] = H.M. Mehedi Hasan;
stud [2].marks = 50;
In other way we can use scanf() and gets() to store data:
scans (%d, &stud[2].rollno);
gets(stud[2].name);
scanf(%d, stud[2].marks);

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

RECORD
If we want to store data to a record (structure) for
more than one person or student, then we need array
of structures.
But disadvantage is that, for these types of array a
huge amount of memory space may be misused or
wasted.

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

10

Algorithm for record operation


.Problem 3.1: Define a record (structure) with three fields for a student and

store data for 30 students.


Solution:
struct
{
int roll_no;
char name[40];
int marks;
} stud[30];
for (i = 0; i < 30; ++i)
{
store data in stud[i].roll_no;
store data in stud[i].name;
store data in stud[i].marks;
}
//For storing data we can use scanf( ) for int type data and gets() for char
array.

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

11

Code for storing data using C/C++


struct
{
int rollno;
char name[40];
int marks;
}stud[30];
for(i=0; i<30; ++i)
{
scanf(%d,&stud[i].rollno);
fflush(stdin);
gets(stud[i].name);
scanf (%d,&stud[i].marks);
}
MD. RAFIQUL ISLAM

loop for array, stud.

DATA STRUCTURE FUNDAMENTALS

12

Code for printing the data


for(i=0; i<30; ++i)
{
cout>>stud[i].rollno;
cout>>stud[i].name;
cout>>stud[i].marks;
}

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

13

Pointer to structure
struct stud
roll
marks
{
int roll:
int marks;
}
sptr
struct stud *sptr;
//sptr is a pointer to stud structure.
sptr=new(stud);
//Allocation memory space for the structure.
cin>>sptr->roll;
//for input
cin>>sptr->marks;
cout<<sptr->roll;
cout<<sptr->marks;
MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

14

Difference between an array and a record


An array is a finite set of same type of data
elements.In other words, it is a collection of
homogeneous data items (elements).
Whereas a record is a collection of non-homogenous
(different types of) related data items.
The data items of an array are of same type.
But in a record, data elements are usually of
different types.
MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

15

Problem on structure
Problem 3.1:

There are 40 students in a class. Each student


record contains student ID, Name, Marks of four
class tests of a course. Write a program to print
the ID, name and average marks of best three
tests for each student.

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

16

Sample questions of this chapter


1. Define record (structure) and give example.
2. Write code to declare a record (structure) with id, name
and marks of a student, write code to store (enter) data and
print(display) the data.
3. What is array of structures (records)? Write is usefulness
and explain with example.
4. What is the difference between an array and a record
(structure).
5. What pointer to the record and structure? Give example.
6. There are 40 students in a section, declare a structure with
id, name and marks of a student, write code to enter (store)
data for the students of the section and print (display) their
data.
MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

17

This is the end


of
Record.

MD. RAFIQUL ISLAM

DATA STRUCTURE FUNDAMENTALS

18

Das könnte Ihnen auch gefallen