8.0 Session Eight: Data Structures 8.1 Session Objectives

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

8.

0 Session Eight : Data structures


8.1 Session Objectives
By the end of this session, you should be able to:
 Explain the term Data structure
 Explain the concept of static and dynamic data structure.

 Understand data structure operations.

8.2 DATA STRUCTURES


Def: DATA STRUCTURE:represent a grouping of related data items in the main memory.

There are two types of data structures :-


(i) Static data structures: Their size is fixed in advance in the main memory.
e.g records,files , arrays

(ii) Dynamic data structures: Their size is changing all time in the main memory i.e not
fixed
Examples:
i. Stack: is a linear list in which insertion and deletions can take place only
at one end called top( LIFO system)

ii. Queue: : is a linear list in which deletions can take place at one end of
the list (front) and insertion can only take only at the other end of the
list(rear).

iii. Trees: Data containing hierarchical relationship between various


elements.

8.3 DATA STRUCTURE OPERATIONS


When data is grouped together its easier to manipulate. The operations undertaken are:
(i) Traversing: Accessing each record exactly once so that certain
items in the record may be processed.

(ii) Searching: Finding the location of the record with a given key
value called Primary key.

(iii) Insertion: Adding new record to a structure.

(iv) Deletion: Removing a record from a structure.

(v) Sorting : Re-arranging records in sequence.

(vi) Merging: combining records from two files.


8.4 ARRAYS

An array is a data structure in which a collection of data items of the same data type are stored.
Characteristic of Arrays
 Data items of the same data type are stored.
 Items are referenced by index
 Multi-dimensional array has several rows and columns

There exist the following types of arrays:-


(i) 1-Dimension array(Has X co-ordinates)
(ii) 2- Dimension array(Has X Y co-ordinates)
(iii) 3- Dimension array(Has XYZ co-ordinates)
(iv) Nth- Dimension array(Has nth co-ordinates)

ARRAY DECLARATION
1-Dimension array :
Int score[6]; / * Array called score having 6 elements of integer type */

2- Dimension array:
Int score[6][4]; / * Array called Table having 6 rows and 4 columns of integer type */

3- Dimension array:

int table[5][5][20]; /* array can hold 500 integer-type elements */

8.5 1-DIMENSION ARRAYS

Below is an array called score havig 6 elements of int data type .


Elements of the array are referenced using the array name followed by subscripts
i.e score[0], score[1],score[2] …score[n-1]

score
16 12 6 4 7 20

ARRAY DECLARATION

Int score[6]; * Array called score having 6 elements of integer type */

8.6 READING elements in an array


Use a for loop
e.g
For(i=0; i<5; ++i)
{
Printf(“Enter a number”);
Scanf(“%i”,&score[i]);
}

8.7 OUTPUTTING elements in an array


Use a for loop
e.g
For(i=0; i<6; ++i)
{
Printf(“%i”,score[i]);
}

Develop a C program to read six integer values into an Array called Score then display the
numbers and the sum.

#include<stdio.h>
Void main(void)
{
Int score[6],sum, i ;

i=0;
Sum=0;
For(i=0; i<5; ++i)
{
Printf(“Enter a number”); Reading six elements in an array called score and
Scanf(“%i”,&score[i]); calculating their sum.
Sum=sum+score[i];
}
For(i=0; i<5; ++i)
{ Printing the six numbers.
Printf(“%i”,score[i]);
}

Printf(“sum is %i” , sum);


}

8.8 Session Summary


DATA STRUCTURE:represent a grouping of related data items in the main memory
8.9 Student Activity
 Explain the term Data structure
 State data structure operations

8.9.1 Further Readings/References


i. Kernighan, B. W., & Ritchie, D. M. (2014). The C programming language (2nd ed.).
Upper Saddle River, NJ: Prentice-Hall PTR. ISBN: 0131103628.
ii. Perry, G. R., & Miller, D. (2013). Absolute beginner's guide to C Programming (3rd
ed.). Indianapolis, Indiana: Que Publishing. ISBN: 0789751984.
iii. Graham, D. L. (2016). C programming language: a step-by-step beginner's guide
to learn C programming in 7 days. NY, USA: CreateSpace Independent Publishing
Platform. ISBN: 1534679707.

You might also like