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

Chapter One Introduction

The document discusses data structures and introduces arrays. It explains that arrays are a physically sequential, fixed size collection of homogeneous objects that allow for random access. Arrays are appropriate when the data consists of a fixed number of homogeneous objects. Examples in C show storing values in an array and finding the greatest value, as well as outputting all elements of an array.

Uploaded by

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

Chapter One Introduction

The document discusses data structures and introduces arrays. It explains that arrays are a physically sequential, fixed size collection of homogeneous objects that allow for random access. Arrays are appropriate when the data consists of a fixed number of homogeneous objects. Examples in C show storing values in an array and finding the greatest value, as well as outputting all elements of an array.

Uploaded by

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

Data Structures; an Introduction

The computer can be used for among many things:

™ Scientific Calculations

™ Information Management System for an enterprise

™ Real-time control system for an assembly line


Introduction

‰ In performing these operations, to use the computer


effectively one needs to:

a) Obtain the needed data and their relations

b) Store the data into the computer according to their


relations

c) Design algorithms to solve the problem


Introduction
‰ The tasks of programming and data processing require
efficient algorithms for accessing the data both in main
memory and on secondary storage devices.

‰ This efficiency is directly linked to the structure of the data


being processed. A data item that can be effectively linked
to other data items takes on meaning that transcends its
individual value.

‰ A data structure is a way of organizing data that considers


not only the items stored but also their relationship to each
other.
Introduction

‰ The way data is accessed is a function of how it is


organized, thus a general understanding of data structures
is essential to developing efficient algorithms.

‰ A clear notion of the relative advantages and


disadvantages of each technique is obviously crucial to
those designing systems.
Introduction
‰ The ability to make correct decisions regarding which data
structures to use typically involve the following general issues:
a) The efficiency of the program with respect to its run time. Does
it perform the optimal number of operations to achieve its goal?
b) The efficiency of a program with respect to its utilization of
main memory and secondary storage devices. Does it
consume such resources in a fashion that makes its use
impractical?
c) The developmental costs of a program or system. Could a
different approach to the problem significantly reduce the total
person-hours invested in it?
Introduction
‰ Thorough knowledge of a programming language is not a
sufficient base upon which to make these decisions.
‰ The study of data structures will expose you to a vast
collection of tried and proven methods used in designing
efficient programs.
‰ In many applications, different types of data structures
could be used to achieve the same end. In such
situations, the designer plays a game of tradeoffs.
‰ One data structure sacrifices memory compactness for
speed; another utilizes memory efficiently but results in a
slow run time.
‰ For each positive there is seemingly a corresponding
negative.
Algorithms for Data structures

‰ Once a data structure has been chosen for a particular


application, it is given life by the logical instructions that
manipulate the related data items stored in it i.e. the
algorithm.
Desirable traits of an algorithm are (among others):
i. completely free of ambiguity.
ii. It must be efficient. It should not unnecessarily use
memory locations nor should it require an excessive
number of logical operations.
iii. Should be concise and compact to facilitate verification
of their correctness.
Algorithms for Data structures
‰ Verification involves observing the performance of the
algorithm with a carefully selected set of test cases.

‰ These test cases should attempt to cover all of the


exceptional circumstances likely to be encountered by the
algorithm. These exceptional circumstances will depend
upon the data structure being manipulated.

‰ However, certain generic circumstances must be verified


for all algorithms manipulating data structures:
a) Does the algorithm work when the data structure is empty?
b) Does the algorithm work when the data structure is full?
c) Does the algorithm work for all the possibilities that can occur
between an empty structure and a full structure?
Algorithms for Data structures

‰ The need for conciseness in algorithms becomes obvious


when you consider the problem of verifying their
correctness. Hence, an algorithm should concern itself with
one specific problem.
‰ Each algorithm is a logical module designed to handle a
specific problem relative to a particular data structure.
‰ Such modules could be tied together by higher level
algorithms, which focus upon the connections between
modules rather than the underlying data structures.
Good Programs

Properties of good programs


They should:
a) run correctly
b) run efficiently
c) be easy to read and understand
d) be easy to debug
e) be easy to modify
Basic definitions
i. Data structures - In computer science, this is a way of storing data
in a computer so that it can be used efficiently. Often a carefully
chosen data structure will allow a more efficient algorithm to be
used.

ii. An abstract data type (ADT) - is a specification of a data structure


and the set of operations that can be performed on it. E.g a queue

iii. Abstraction - This is the separation between what a data structure


represents and what an algorithm accomplishes, from the
implementation details of how things are actually carried out. i.e.,
hiding the unnecessary details

iv. Data Abstraction - Hiding of the representational details

v. Data Types - a data type, or type is a classification of data. It


indicates a set of values that have the same general meaning or
intended purpose, e.g primitive
types(integers,strings),records,classes
Atomic/Simple Data Types

‰ These are data types that are defined without imposing


any structure on their values (have no internal structural
relationship)

‰ Examples of atomic types.

– Boolean,
– Integer
– characters
Structured Data Types
‰ A structured data type has a definition that imposes structure
upon its values.
‰ In many structured data types, there is an internal structural
relationship, or organization, that holds between the
components.
‰ For example, if we think of an array as a structured type, with
each position in the array being a component, then there is a
structural relationship of `followed by': we say that
component i is followed by component i+1.
‰ Many structured data types do have an internal structural
relationship, and these can be classified according to the
properties of this relationship.
Structured Data types: Linear Structure
‰ The most common organization for components is a
linear structure. A structure is linear if it has these 2
properties:

¾ Property P1: Each element is `followed by' at most one


other element.
¾ Property P2: No two elements are `followed by' the same
element.
‰ An array is an example of a linearly structured data type.
We generally write a linearly structured data type like
this: A->B->C->D (this is one value with 4 parts).

‰ Example 1 (violates P1): A points to B and C B<-A->C


‰ Example 2 (violates P2): A and B both point to C
A->C<-B
Structured Data types: Classes: Tree Structure

‰ If we drop the first constraint (P1)and keep the


second (P2) we get a tree structure or hierarchy:
no two elements are followed by the same
element. This is a very common structure too, and
extremely useful.

‰ Example 1 in the slide before is a tree, but


Example 2 is not.
‰ A is followed by B C D, B by E F, C by G.
Structured Data types: Classes: Graph Structure

Dropping both P1 and P2


‰ If we drop both constraints, we get a graph.
Structured Data types: Classes: Cyclic Structures:
• All the examples we've seen are acyclic. This means
that there is no sequence of arrows that leads back to
where it started.
• Linear structures are usually acyclic, but cyclic ones are
not uncommon.

• Graphs are often cyclic, although acyclic graphs are an


important topic of study.
• Example: Add an edge from G to D, and from E to A.
Structured Data Types : Arrays
‰ An Arrays is a physically sequential, fixed size
collection of homogeneous objects. In addition,
objects with the structure have the random access
property.
I. The array is physically sequential in that the data
objects are stored in consecutive memory locations.
II. The array has a fixed size in that its size can neither be
increased nor reduced though the number of items it
contains can vary
III. The array is homogeneous in that they are made up of
objects that are all the same type.
IV. The random access property means that the time it
takes to access one object in the structure does not
depend on what object in the structure had been
accessed previously.
Arrays

‰ An array type is appropriate for representing an


abstract data type when the following conditions are
satisfied:
1) The data objects in the abstract data type are
composed of homogeneous objects
2) The solution requires the representation of a fixed,
predetermined number of objects
Arrays
Examples: Storing values into an array and getting the greatest value
using C
0 1 2 3 4
int Numbers[5];

for (int i=0;i<5;i++)


{
printf("Enter Next Number:- "); 8 2 22 56 11
scanf("%d",&num);
printf("\n\n");
Numbers[i]=num;
}

int big=Numbers[0];
for (i=0;i<5;i++) Big=8 Big=8 Big=22 Big=56 Big=56
{
if(Numbers[i]>big)
big=Numbers[i];
}
Arrays

// Example: Outputting the elements of an array


#include <stdio.h>
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
printf(“%d”,result);
return 0;
}
Arrays
#include <stdio.h>
int Numbers[5]; //Get the biggest element
void main(void) printf("\n\n");
{
printf("We are dealing with arrays int big=Numbers[0];
\n");
printf("*** Assign values ***\n");
for (i=0;i<5;i++)
printf("\n\n"); int num=0;
{
for (int i=0;i<5;i++)
if(Numbers[i]>big)
{ printf("Enter Next Number:- ");
big=Numbers[i];
scanf("%d",&num);
}
printf("\n\n");
Numbers[i]=num;
} printf("The Bigest number is %d", big);
// Print the elements printf("\n\n");
printf("Printing elements \n"); }//end main
printf("\n\n");
for ( i=0;i<5;i++)
{
printf("%d",Numbers[i]);
if(i<4) printf(" : ");
}
Arrays
Multidimensional arrays

‰ Multidimensional arrays can be described as arrays of arrays. For


example, a two-dimensional array consists of a certain number of rows
and columns:
‰ A one-dimensional array is usually processed via a ‘for’ loop. Similarly,
a two-dimensional array may be processed with a nested for loop:
What does the piece of code below do?

for (int Row = 0; Row < NUMROWS; Row++)


{
for (int Col = 0; Col < NUMCOLS; Col++)
{
Array[Row][Col] = 0;
}
}
What would be the print out of the following initialization?

int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} };


int Array2[2][3] = { 1, 2, 3, 4, 5 };
int Array3[2][3] = { {1, 2} , {4 } };

for (int row = 0; row < 2; row++)


{
for (int col = 0; col < 3; col++)
{
printf(“%d”,Array1[row][col]);
}
printf(“\n\n”);
}
Arrays:Disadvantages
1) The size of the array is fixed —
Most often this size is specified at compile time with a simple
declaration such as in the example above. With a little extra
effort, the size of the array can be deferred until the array is
created at runtime, but after that it remains fixed.
2) Because of (1), the most convenient thing for programmers to
do is to allocate arrays which seem "large enough" (e.g. the 100
items). Although convenient, this strategy has two
disadvantages:
- most of the time there are just 20 or 30 elements in the array
and 70% the space in the array really is wasted.
- If the program ever needs process more than 100 scores, the
code breaks.
3) Inserting new elements at the front or somewhere at the middle
is potentially expensive because existing elements need to be
shifted over to make room.
Exercise 1

1) A matrix can be represented using a two dimensional


array. Create a C program that does the following

i. creates a three by three matrix.


ii. Allows a user to enter values into the elements of
the matrix
iii. Outputs the elements of the matrix
iv. Sums all the elements of the matrix and outputs
the result

You might also like