EXERCISE 1 (Done)
EXERCISE 1 (Done)
- Array
- Stack
- Data structure is a way to store and organize data so that it can be used efficiently.
3. Declare a structure named Staff with structure members Name and Age. [2 marks]
Struct Staff
{
Int Name;
Int Age;
}
4. Explain linear and non-linear data structure by giving the examples. [4 marks]
Data structure where data elements are arranged sequentially or linearly where each element is
attached to its previous and next adjacent is called a linear data structure. In linear data structure,
single level is involved. Therefore, we can traverse all the elements in single run only. Linear data
structures are easy to implement because computer memory is arranged in a linear way. For example,
Stack data structure is a linear data structure. The data structure follows the rule of LIFO (Last in-First
Out) where the data last added element is removed first. Push operation is used for adding an element
of data on a stack and pop operation is used for deleting data from the stack.
Data structures where data elements are not arranged sequentially or linearly are called non-linear
data structures. In a non-linear data structure, single level is not involved. Therefore, we can’t
traverse all the elements in single run only. Non-linear data structures are not easy to implement in
comparison to linear data structure. For example, A tree data structure consists of various nodes linked
together. The structure of a tree is hierarchical that forms a relationship like that of the parent and a
child. The structure of the tree is formed in a way that there is one connection for every parent-child
node relationship. Only one path should exist between the root to a node in the tree.
5. Explain primitive and non-primitive data structure by giving the examples. [4 marks]
A primitive data type is one that fits the base architecture of the underlying computer
such as int, float, and pointer, and all of the variations, thereof such as char short long
unsigned float double and etc, are a primitive data type.
Primitive data are only single values, they have not special capabilities.
A non-primitive data type is something else such as an array structure or class is known
as the non-primitive data type.
The data type that is derived from primary data types is known as a non-primitive data
type.
The non-primitive data types are used to store the group of values.
Examples of the non-primitive data types are Array, structure, union, link list, stacks,
queue etc…
6. Declare a structure named Car which holds the information based on the table below.
[6
marks]
Variable Data type Size
Platenumber String 10
Ownername Char 50
CC Double -
Brand Char 10
Price Float -
Answer:
Struct Car
{
String platenumber [10];
Char Ownername [10];
Double CC;
Char Brand [10];
Float price;
}
15