Professional Documents
Culture Documents
CSC Assignment 123
CSC Assignment 123
i. Arrays: Array items are typically stored in a sequence of computer memory locations,
which can be write the items in order, separated by commas and enclosed by square
brackets. Thus, a=[1 , 4 , 17 , 3 , 90 ,79 , 4 , 6 , 81]. This array a has 9 items, and hence we
say that its size is 9. for our array a , its positions are0 , 1 ,2 , . .. , 7 , 8. The element in the
8th position is 81, and we use the notation a [8] to denote this element.
Consider LA be a linear array in the memory of the computer. As we know that the
memory of the computer is simply a sequence of addressed location as given in
ii. Lists: A linked list, or one-way list, is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers. That is, each node is divided into
two parts: the first part contains the information of the element, and the second part,
called the link field or next pointer field, contains the address of the next node in the list
A list can involve virtually anything, for example, a list of integers [3,2,4,2,5], a
shopping list [apples, butter, bread, cheese], When considering lists, we can speak
20/08/05/123
iii. Recursion: We previously saw how iteration based on for-loops was a natural way to
process collections of items stored in arrays. When items are stored as linked-lists, there
is no index for each item, and recursion provides the natural way to process them. The
idea is to formulate procedures which involve at least one step that invokes (or calls)
the procedure itself.
iv. Stacks: Stacks are, on an abstract level, equivalent to linked lists. They are the ideal
data structure to model a First-In-Last-Out (FILO), or Last-In-First-Out (LIFO),
strategy in search.
Graphical Representation
Their relation to linked lists means that their graphical representation can be the same,
but one has to be careful about the order of the items. For instance, the stack created by
inserting the numbers [3,1,4,2,5] in that order would be represented as:
2
20/08/05/12
2. In a non-linear structure, each element may have one or more predecessors and successors,
and there may be multiple paths to reach a specific element. Because of their complex
organization, it is not possible to traverse all the elements in a single run. Examples of non-
linear structures are trees and graphs.
i. Trees: a tree can be defined as either the empty tree, or a node with a list of successor
trees. Nodes are usually, though not always, labelled with a data item (such as a number
or search key). We will refer to the label of a node as its value.
8
3 1
1 6 9 1
7 1 1 1
Types of Threes
i. Quad-trees: is a particular type of tree in which each leaf-node is labelled by
a value and each non-leaf node has exactly four children. It is used most
often to partition a two dimensional by recursively dividing it into four
quadrants.
ii. Binary trees: nary trees are the most common type of tree used in computer
science. A binary tree is a tree in which every node has at most two children,
and can be defined “inductively” by the following rules: Definition.
ii. Graphs: Graphs Often it is useful to represent information in a more general graphical
form than considered so far, such as the following representation of the distances
between towns. A graph G may be defined as a finite set V of vertices and a set E of
edges (pair of connected vertices). The notation used is as follows: Graph G = (V,E) Let
we consider graph of
Different types of data (e.g., integers, floating-point numbers, characters) are stored and
processed in memory using specific data types. These data types define the size and format of the
data, ensuring proper representation and manipulation. Common data types include:
1. Integers: Used for whole numbers and typically come in various sizes (e.g., int32, int64).
2. Floating-Point: Used for real numbers with decimal points, like 3.14 or 2.71828.
3. Characters: Used for representing text and symbols.
Conclusion
Data representation in memory is a fundamental aspect of computer science and plays a vital role
in how computers store and process information. It involves encoding data into binary form,
organizing memory units, specifying data types, and considering endianness. A solid
understanding of these concepts is essential for programmers and computer scientists working
with low-level programming, data structures, and computer architecture.
3
20/08/05/123
1. Static allocation • In this allocation scheme, the compilation data is bound to a fixed
location in the memory and it does not change when the program executes. As the
memory requirement and storage locations are known in advance, runtime support
package for memory allocation and de-allocation is not required. In a static storage-
allocation strategy, it is necessary to be able to decide at compile time exactly where each
data object will reside at run time. In order to make such a decision, at least two criteria
must be met:
i. The size of each object must be known at compile time.
ii. Only one occurrence of each object is allowable at a given moment during program
execution.
Because of the first criterion, variable-length strings are disallowed, since their length
cannot be established at compile time. Similarly dynamic arrays are disallowed, since
their bounds are not known at compile time and hence the size of the data object is
unknown. Because of the second criterion, nested procedures are not possible in a static
storage-allocation scheme. This is the case because it is not known at compile time which
or how many nested procedures, and hence their local variables, will be active at
execution time.
2. Dynamic Allocation: The allocation can be varied during the execution and It makes the
use of recursive function. In a dynamic storage-allocation strategy, the data area
requirements for a program are not known entirely at compilation time. In particular,
the two criteria that were given in the previous section as necessary for static storage
4
20/08/05/12
allocation do not apply for a dynamic storage-allocation scheme. The size and number of
each object need not be known at compile time; however, they must be known at run time
when a block is entered. Similarly more than one occurrence of a data object is allowed,
provided that each new occurrence is initiated at run time when a block is entered. Key
concepts related to dynamic memory allocation include:
i. Heap Memory: In many programming languages, dynamic memory is allocated
from a region of memory called the "heap." The heap is a pool of memory that
can be used for storing data structures of varying sizes during program execution.
ii. Allocation and Deallocation: During program execution, memory can be allocated
using functions like `malloc`, `calloc’. It is essential to release memory when it is
no longer needed, typically done with functions like `free` or `delete`. Failure to
deallocate memory can lead to memory leaks.
iii. Fragmentation: Fragmentation can occur in the heap when memory is allocated
and deallocated in an inefficient manner. Two main types of fragmentation exist:
external fragmentation, which involves free memory blocks that are not
contiguous, and internal fragmentation, where memory within allocated blocks is unused.
The following three-address statements are associated with the run-time allocation and de
allocation of activation records:
1. Call, 2. Return, 3. Halt, 4. Action, a placeholder for other
statements.
5
20/08/05/123
group item is followed by its sub items, and the level of the sub items is 1 more than
the level of the group item. Furthermore, an item is a group if and only if it is
immediately followed by an item with a greater level number.
Some of the identifiers in a record structure may also refer to arrays of elements. In
fact, the first line of the above structure is replaced by
1 Newborn(20)
This will indicate a file of 20 records, and the usual subscript notation will be used to
distinguish between different records in the file. That is, we will write to
Newborn[1], Newborn[2], Newborn[3],. . .
to denote different records in the file.
Strings
A string is a sequence of characters. In Programming, strings start and end with single or double
quotes (they are equivalent but they have to match).
>>> s = "foo"
>>> print s
foo
>>> s = 'Foo'
>>> print s
Foo
>>> s = "foo'
SyntaxError: EOL while scanning string literal
Each string is stored in the computer’s memory as a list (array) of characters.
>>> myString = "GATTACA"
Accessing single characters
>>> myString = "GATTACA"
>>> myString[0]
'G'
Conclusion: Records and strings are indispensable data structures in computer science and
programming. Records allow for the organization of structured data, while strings facilitate the
manipulation and representation of textual data. Understanding how to work with these data
6
20/08/05/12
structures is fundamental to building software applications that handle and process diverse types
of information.