Skip to main content

PROBLEM SOLVING AND PYTHON PROGRAMMING QUIZ

1) What is the first step in problem-solving? A) Writing code B) Debugging C) Understanding the problem D) Optimizing the solution Answer: C 2) Which of these is not a step in the problem-solving process? A) Algorithm development B) Problem analysis C) Random guessing D) Testing and debugging Answer: C 3) What is an algorithm? A) A high-level programming language B) A step-by-step procedure to solve a problem C) A flowchart D) A data structure Answer: B 4) Which of these is the simplest data structure for representing a sequence of elements? A) Dictionary B) List C) Set D) Tuple Answer: B 5) What does a flowchart represent? A) Errors in a program B) A graphical representation of an algorithm C) The final solution to a problem D) A set of Python modules Answer: B 6) What is pseudocode? A) Code written in Python B) Fake code written for fun C) An informal high-level description of an algorithm D) A tool for testing code Answer: C 7) Which of the following tools is NOT commonly used in pr...

Classification of data structures

CLASSIFICATION OF DATA STRUCTURES
Data structures are generally categorized into two classes: primitive and non-primitive data structures. 
Primitive and Non-primitive Data Structures
Primitive data structures are the fundamental data types which are supported by a programming language. Some basic data types are integer, real, character, and boolean. The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ are often used interchangeably. 
Non-primitive data structures are those data structures which are created using primitive data structures. Examples of such data structures include linked lists, stacks, trees, and graphs. 
Non-primitive data structures can further be classified into two categories: linear and non-lineardata structures. 
Linear and Non-linear Structures
If the elements of a data structure are stored in a linear or sequential order, then it is a linear data structure. Examples include arrays, linked lists, stacks, and queues. Linear data structures can be 
represented in memory in two different ways. One way is to have to a linear relationship between elements by means of sequential memory locations. The other way is to have a linear relationship 
between elements by means of links.
However, if the elements of a data structure are not stored in a sequential order, then it is a non-linear data structure. The relationship of adjacency is not maintained between elements of a 
non-linear data structure. Examples include trees and graphs. 
C supports a variety of data structures. We will now introduce all these data structures and they would be discussed in detail in subsequent chapters. 

Arrays
An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript).
In C, arrays are declared using the following syntax:
type name[size];
For example,
int marks[10];
The above statement declares an array marks that contains 10 elements. In C, the array index starts from zero. This means that the array marks will contain 10 elements in all. The first element will 
be stored in marks[0], second element in marks[1], so on and so forth. Therefore, the last element, that is the 10th element, will be stored in marks[9]. In the memory, the array will be stored 
Fig:  Memory representation of an array of 10 elements
Arrays are generally used when we want to store large amount of similar type of data. But they have the following limitations:
* Arrays are of fixed size.
* Data elements are stored in contiguous memory locations which may not be always available.
* Insertion and deletion of elements can be problematic because of shifting of elements from their positions.
However, these limitations can be solved by using linked lists.

Linked Lists
A linked list is a very flexible, dynamic data structure in which elements (called nodes) form a sequential list. In contrast to static arrays, a programmer need not worry about how many elements will be stored in the linked list. This feature enables the programmers to write robust programs 
which require less maintenance.
In a linked list, each node is allocated space as it is added to the list. Every node in the list points to the next node in the list. Therefore, in a linked list, every node contains the following two types of data:
* The value of the node or any other data that corresponds to that node
* A pointer or link to the next node in the list
The last node in the list contains a NULL pointer to indicate that it is the end or tail of the list. Since the memory for a node is dynamically allocated when it is added to the list, the total number of nodes that may be added to a list is limited only by the amount of memory available.
               Fig : Simple linked list

Stacks
A stack is a linear data structure in which insertion and deletion of elements are done at only one end, which is known as the top of the stack. Stack is called a last-in, first-out (LIFO) structure because the last element which is added to the stack is the first element which is deleted from the stack.
In the computer’s memory, stacks can be implemented using arrays or linked lists.The below figshows the array implementation of a stack. 
Fig : Array representation of a stack
Every stack has a variable top associated with it. top is used to store the address of the topmost element of the stack. It is this position from where the element will be added or deleted. There is another variable MAX, which is used to store the maximum number of elements that the stack can store.
If top = NULL, then it indicates that the stack is empty and if top = MAX–1, then the stack is full.
top = 4, so insertions and deletions will be done at this position. Here, the stack 
can store a maximum of 10 elements where the indices range from 0–9. In the above stack, five more elements can still be stored.
A stack supports three basic operations: push, pop, and peep. The push operation adds an element to the top of the stack. The pop operation removes the element from the top of the stack. And the peep operation returns the value of the topmost element of the stack (without deleting it).
However, before inserting an element in the stack, we must check for overflow conditions. An overflow occurs when we try to insert an element into a stack that is already full.
Similarly, before deleting an element from the stack, we must check for underflow conditions. An underflow condition occurs when we try to delete an element from a stack that is already empty.

Queues
A queue is a first-in, first-out (FIFO) data structure in which the element that is inserted first is the first one to be taken out. The elements in a queue are added at one end called the rear and removed from the other end called the front. Like stacks, queues can be implemented by using 
either arrays or linked lists.
Every queue has front and rear variables that point to the position from where deletions and insertions can be done, respectively. 
Fig : Array representation of a queue
Here, front = 0 and rear = 5. If we want to add one more value to the list, say, if we want to add another element with the value 45, then the rear would be incremented by 1 and the value would be stored at the position pointed by the rear. The queue, after the addition, would be as 
shown in below figure.
Fig : Queue after insertion of a new element
Here, front = 0 and rear = 6. Every time a new element is to be added, we will repeat the same procedure
Now, if we want to delete an element from the queue, then the value of front will be incremented. Deletions are done only from this end of the queue. The queue after the deletion will be as shown in below figure.
Fig: Queue after deletion of an element
However, before inserting an element in the queue, we must check for overflow conditions. An overflow occurs when we try to insert an element into a queue that is already full. A queue is full when rear = MAX–1, where MAX is the size of the queue, that is MAX specifies the maximum number of elements in the queue. Note that we have written MAX–1 because the index starts from 0.
Similarly, before deleting an element from the queue, we must check for underflow conditions. An underflow condition occurs when we try to delete an element from a queue that is already empty. If front = NULL and rear = NULL, then there is no element in the queue. 

Trees
A tree is a non-linear data structure which consists of a collection of nodes arranged in a hierarchical order. One of the nodes is designated as the root node, and the remaining nodes can be partitioned 
into disjoint sets such that each set is a sub-tree of the root.
The simplest form of a tree is a binary tree. A binary tree consists of a root node and left and right sub-trees, where both sub-trees are also binary trees. Each node contains a data element, a left pointer which points to the left sub-tree, and a right pointer which points to the right sub-tree. The root element is the topmost node which is pointed by a ‘root’ pointer. If root = NULL then the tree is empty.
Below figure shows a binary tree, 
                  Fig : Binary tree
where R is the root node and T1 and T2 are the left and right sub-trees of R. If T1 is non-empty, then T1 is said to be the left successor of R. Likewise, if T2is non-empty, then it is called the right successor of R.node 2 is the left child and node 3 is the right child of the root node 1. Note that the left sub-tree of the root node consists of the nodes 2, 4, 5, 8, and 9. Similarly, the right sub-tree of the root node consists of 
the nodes 3, 6, 7, 10, 11, and 12.

Graphs
A graph is a non-linear data structure which is a collection of vertices (also 
called nodes) and edges that connect these vertices. A graph is often viewed 
as a generalization of the tree structure, where instead of a purely parent-to-child relationship between tree nodes, any kind of complex relationships between the nodes can exist.
               Fig : Graph
In a tree structure, nodes can have any number of children but only one parent, a graph on the other hand relaxes all such kinds of restrictions. Figure shows a graph with five nodes.
A node in the graph may represent a city and the edges connecting the nodes can represent roads. 
A graph can also be used to represent a computer network where the nodes are workstations and the edges are the network connections. Graphs have so many applications in computer science and mathematics that several algorithms have been written to perform the standard graph operations, such as searching the graph and finding the shortest path between the nodes of a graph.
Note that unlike trees, graphs do not have any root node. Rather, every node 
in the graph can be connected with every another node in the graph. When two 
nodes are connected via an edge, the two nodes are known as neighbours. For 
example, in Fig. , node A has two neighbours: B and D.

Popular posts from this blog

Introduction to C Programs

INTRODUCTION The programming language ‘C’ was developed by Dennis Ritchie in the early 1970s at Bell Laboratories. Although C was first developed for writing system software, today it has become such a famous language that a various of software programs are written using this language. The main advantage of using C for programming is that it can be easily used on different types of computers. Many other programming languages such as C++ and Java are also based on C which means that you will be able to learn them easily in the future. Today, C is mostly used with the UNIX operating system. Structure of a C program A C program contains one or more functions, where a function is defined as a group of statements that perform a well-defined task.The program defines the structure of a C program. The statements in a function are written in a logical series to perform a particular task. The most important function is the main() function and is a part of every C program. Rather, the execution o...

Performance

Performance ( Optional ) * The I/O system is a main factor in overall system performance, and can place heavy loads on other main components of the system ( interrupt handling, process switching, bus contention, memory access and CPU load for device drivers just to name a few. ) * Interrupt handling can be relatively costly ( slow ), which causes programmed I/O to be faster than interrupt driven I/O when the time spent busy waiting is not excessive. * Network traffic can also loads a heavy load on the system. Consider for example the sequence of events that occur when a single character is typed in a telnet session, as shown in figure( And the fact that a similar group of events must happen in reverse to echo back the character that was typed. ) Sun uses in-kernel threads for the telnet daemon, improving the supportable number of simultaneous telnet sessions from the hundreds to the thousands.   fig: Intercomputer communications. * Rather systems use front-end processor...

Mathematics

MATHEMATICS           Mathematics is the science that deals with shapes, quantities and arrangements. Archmedes is known as the father of Mathematics (287BC-212BC). Mathematics seek and use patterns to formulates new conjuctures.They resove truth or false by using mathematical proof. Mathematics developed by counting, calculation, Measurements, Shapes and motion of physical objects.  Definition Mathematics has no general accepted definition. Until 18th century Aristotle defined mathematics as "the science of quantity". Many mathematicans take no interest in definition they simply say "Mathematics is what Mathematican do". Three leading definition of mathematics today are logicist, intutionist, and formalist. Logicist - In terms of Benjamin peirce, the definition of mathematics in terms of logic are "the science that draws necessary conclusion" and also said that " All mathematics is symbolic logic" by Mathematician Rusell. Intutionist - L.E.J.Bro...