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

"Algorithms + Data Structures Programs!": - Niklaus Wirth

- Niklaus Wirth stated that "Algorithms + Data Structures = Programs!". This quote captures that algorithms and data structures are the fundamental building blocks of computer programs. - The document then provides examples of using different data structures like structures, arrays within structures, nested structures, and self-referential structures to organize data in C/C++ programs. It also discusses passing structures to functions and between data types. - Finally, the document presents examples of potential interview questions related to structures that assess a candidate's knowledge of using structures in programming.

Uploaded by

TB
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
389 views

"Algorithms + Data Structures Programs!": - Niklaus Wirth

- Niklaus Wirth stated that "Algorithms + Data Structures = Programs!". This quote captures that algorithms and data structures are the fundamental building blocks of computer programs. - The document then provides examples of using different data structures like structures, arrays within structures, nested structures, and self-referential structures to organize data in C/C++ programs. It also discusses passing structures to functions and between data types. - Finally, the document presents examples of potential interview questions related to structures that assess a candidate's knowledge of using structures in programming.

Uploaded by

TB
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

“Algorithms + Data Structures = Programs!

- Niklaus Wirth
STRUCTURES
 user defined data type.
Collection of variables refrenced under one
name.
 in ‘C++’ has backward compatibility with ‘ C’.
Declared with the help of keyword ‘struct’.
All members are public by default .
Structure Declaration.

struct date
struct tag
{
{
int day;
type variable-name;
int month
type variable-name;
int year;
type variable-name;
};
:
}structure variables;
The ‘typedef ‘ Keyword.
as in

Without typdef With typedef

struct date
typedef struct date
{ int day ; { int day ;
int month;
int month;
}; };

struct date D1; date D1;


The ‘typedef ‘ Keyword.
as in

typedef struct date


{ int day ;
int month; /* As Refrence provides alias
}dat; name to a variable.
typedef provides an alias
date D1; name for a data
dat D2; type. */
Refrencing Structure elements.

D1.year=1988;
D2.day= 2;
.
struct_name element_name

da month year
y
D1 D1.yea
r

D2

D2.day
Structure Assignments.

 One structure variable can be assigned another variable


of the same structure type.
 even if the data members are same (in no. and in type )
we cannot assign to different structure type.
 Only Assignment (=) is possible for two similar
structures.
 Other Operations like ( == ) OR ( ! = ) are not possible
in most versions of compliers.
Structure Assignments.

Eg.

typedef struct phoneno typedef struct pin_no


{ int no; { int no;
}; };
phoneno p1,p2; Pin_no n1,n2; Pin no
p1.no=6491444; n1.no=456010; cannot be
same as
phone no !
p2=p1; n2=n1;

p2=n2;

//error: type
Complex Structures
 Structure elements may be simple or complex.

 Simple elements are of fundamental type like


int,float etc .
 complex elements are like arrays or even
structures.
 such structures are called COMPLEX Structures.
 when element is a structure, then NESTED
Structure.
Structures and Arrays

 Array of structures.
eg. Struct emp e[10];
 Arrays within structure
eg. Struct student
{
char name[20];
int marks[5];
};
Nested Structures
Struct addr
 structure within a structure
{ int houseno;
 inner structure should be char locality[20];
defined before outer structure . char city[20];
Elements are refrenced };
outermost to innermost. Struct employee

 here {int name;

shrey.address.houseno=3; addr address;


};
Struct employee shrey;
Structures and functions.

Passing structure elements to Functions


eg. int s = print (bday.day,bday.month);

/* if one of the elements is an array , it will


automatically passed as by refrence as arrays cant be passed by
value! */
Structures and functions.
Passing whole structures
eg. Void prnsum(struct distance d1,struct distance d2) ;

 returning whole structures


eg. Distance prnsum ( distance d1, distance d2 )
{ distance d3;
d3.feet=d1.feet+d2.feet;
return d3; }
A Case Study !

A company has a no. of employees. A


person’s (probably HR manager) job is to
store information about all the new
employees. The information includes Name,
Age, Address and Basic Salary.

Whenever the manager wants to retrieve


any information he must punch in the
employee IDto, implement
Program which would give
the case Studycomplete

information of that particular employee.


Self Referential Structures..
as in

A Structure having a member element that refers to the structure


itself , is called a self-referential Structure.

 mainly used for creation of Linked Lists.


 has an additional pointer that points to the
structure.
 eg.
struct employee {
int empno;
char name[20];
employee *next; };
Linked Lists using self referential Structures

empn 1013 1053 1011


o
name rahul akshay Martin
salary 21,000 24,000 26,000
next

Representation as in memory.
The Concept of BIT FIELDS

 A bit field is a set of adjacent bits whose size varies from 1-16
bits in length.

General form of bit field definition :


struct tag name
{
data-type name1 : bit-length;
data-type name2 : bit length ;
…….
data type nameN : bit length;
};
 Data type is either int or unsigned int, bit length is the no. of
bits.

‘scanf’ of values is not possible !


The Concept of BIT FIELDS

15 14 13 12 …………………………… 2
1 0

struct personal
{ unsigned gender : 1 ;
Bit Bit Range
unsigned age : 7 ;
field length of
gender 1 values
0 or 1
unsigned mar_status : 1 ;
unsigned children : 3 ;
age 7 0 to 127
}emp; (27 – 1)
Martial 1 0 or 1
Emp.gender=1; status
Emp.age=32; children 3 0 to 7
(23 – 1)
Problems On Structures..
// as asked in various campus
placements //
This Question was asked at technical exam for IBM
Give the output of the following program .
Struct point a) 25:20
{int x,y;};
40:10
Void show(point p)
{cout<<p.x<<“:”<<p.y<<endl; } 35:10
Void main()
{ point U={20,10},V,W;
e) 25:20
V=U;
40:20
V.X+=20;
W=V; 35:10
U.Y+=10;
U.X+=5;
i) 25:10
W.X-=5;
show(U);show(V);show (W); 40:10
} 35:10
Solution
A) 25:20
40:10 X Y
Struct point
35:10
{int x,y;};
Void show(point p)
{cout<<p.x<<“:”<<p.y<<endl; }
U
Void main()
{ point U={20,10},V,W;
V=U;
V.X+=20;
V
W=V;
U.Y+=10;
U.X+=5;
W.X-=5;
show(U);show(V);show (W);
W
} ;
Aptitude test

Identify error(s) , if any in the following code snippet

struct { a) Struct tag missing

short day; b) more than 1 variable


declared.
short month;
c) terminated incorrectly
short year ;
d) NO error !
}
bdate,joindate;
Solution

Correct Ans is

D) NO error !

When Structure tag is missing , it is


possible to access the structure tag through
the variables created just after !

struct date {
short day;
short month;
short year ;
}bdate,joindate;
is also correct
Possible Questions for Technical interview.

 Use of Typedef in C / Use of Typedef in C++


 Difference between structure and arrays
 Difference between structure and union.
 Can a Structure contain a Pointer to itself?
How are Structure passing and returning implemented by the complier?

 What are bit fields? What is the use of bit fields in a


Structure declaration?

How would you use qsort() function to sort an array of


structures?

How can we read/write Structures from/to data files?


THANK YOU !

You might also like