Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PART C DSC

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

PART-C

Answer Any Three Questions


16. Explain the different types of loops in C with syntax and examples
LOOPING STATEMENTS

Loop constructs supports repeated execution of statements when a condition match.


If the loop Test Condition is true, then the loop is executed, the sequence of statements to be
executed is kept inside the curly braces { } is known as the Loop body. After every execution of the
loop body, condition is verified, and if it is found to be true the loop body is executed again. When
the condition check returns false, the loop body is not executed, and execution breaks out of the loop.

Types of Loop

There are 3 types of Loop in C language, namely:

1. while loop
2. for loop
3. do while loop
while loop

Repeats a statement or group of statements while a given condition is true.


It tests the condition before executing the loop body.

while(condition)

{
statements;
Example-- Example: Program to print first 10 natural numbers
#include<stdio.h> void
main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t",
x);x++;
}
}
Output: 1 2 3 4 5 6 7 8 9 10

do while loop

In some situations it is necessary to execute body of the loop before testing the condition.
Such situations can be handled with the help of do-while loop. do statement executes the body of the
loop first and at the end, the condition is checked using while statement. It means that the body of
the loop will be executed at least once, even though the starting condition inside while is initialized to
be false.
General syntax

do
{
.....
.....
}
while(condition)
Example: Program to print first 10 multiples of 5.
#include<stdio.h>
void main()
{
int a, i; a
= 5;
i = 1;
do
{
printf("%d\t", a*i); i++;
}
while(i <= 12);
}
Output

5 10 15 20 25 30 35 40 45 50 55 60

17. Write a C program using a pointer for searching the desired element from the array.
#include<stdio.h>
#include<stdlib.h>
int main() Output
{
int n,i,key, FOUND=0, a[30]; // array declaration
printf(“\n How many numbers:”);
scanf(“%d”,&n); // array size How many numbers: 6
printf(“\n Enter the array elements:\n”); for(i=0 ; Enter the array
i<n; i++) elements: 21
{ 33
scanf(“%d”, &a[i]);
} 46
printf(“\n Enter the key to be searched: ”);
scanf(“%d”,&key); 52

for(i=0 ; i<n; i++) // searching an element in an array 27


if(a[i] == key)
Enter the key to be searched:
{
73 NOT FOUND
printf(“\n Found at %d”,i);
FOUND=1;
if(FOUND = = 0)
printf(“\n NOT FOUND...”);
return 0;
}

18. Construct an expression tree for the expression (a + b * c) +((d * e + 1) * g). Give the outputs when you apply
preorder, inorder and postorder traversals

Recursive Traversals of Tree


Traversing means visiting each node at once. Tree traversal is a method for visiting all the nodes in the
tree exactly once.
There are three types of tree traversal techniques, namely
1. Inorder Traversal or symmetric order
2. Preorder Traversal or depth-first order
3. Postorder Traversal
Inorder Traversal
The inorder traversal of a binary tree is performed as
 Traverse the left subtree in inorder
 Visit the root
 Traverse the right subtree in inorder.
Recursive Routine for Inorder Traversal void Inorder (Tree T)

{
if (T!=NULL)
{
Inorder (T->left);
printf(“%d”,T->data); Inorder (T->right);

Preorder Traversal
The preorder traversal of a binary tree is performed as follows,
 Visit the root
 Traverse the left subtree in preorder
 Traverse the right subtree in preorder.

Recursive Routine for Preorder Traversal


void Preorder (Tree T)
{
if (T ! = NULL)
{
printf(“%d”,T->data); Preorder (T ->left); Preorder (T ->right);
}
Postorder Traversal
The postorder traversal of a binary tree is performed by the following steps.
 Traverse the left subtree in postorder.
 Traverse the right subtree in postorder.
 Visit the root.
Recursive Routine for Postorder Traversal
void Postorder (Tree T)
{
if (T ! = NULL)
{
Postorder (T ->Left); Postorder (T ->Right); printf(“%d”,T->data);
}
19. Illustrate the various representations of graphs with examples.

Graph is a data structure that consists of following two components:


1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.

The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di- graph). The pair of
form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.
Graphs are used to represent many real life applications: Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also used in social
networks like linkedIn, facebook. For example, in facebook, each person is represented with a vertex (or node).
Each node is a structure and contains information like person id, name, gender and locale.
Following is an example undirected graph with 5 vertices.

Following two are the most commonly used representations of graph.

1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph
representation is situation specific. It totally depends on the type of operations to be performed and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[]
[], a
slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is
always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an
edge from vertex i to vertex j with weight w.
The adjacency matrix for the above example graph is:

0 1 2 3 4

0 1 0 0 1
1 0 1 1 0 1
0 1 0 0
0 1 1 0 1
1 1 0 1 0

pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether
there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Adjacency List:

An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An
entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to
represent a weighted graph. The weights of edges can be stored
in nodes of linked lists. Following is adjacency list representation of the above graph.

20. Analyze the steps in the insertion sort algorithm and apply the algorithm for the numbers given below 12, 06, 14,

02, 01, 04, 03.

INSERTION SORT

1. It is a simple Sorting algorithm which sorts the array by shifting elements one by one.
Following are some of the important characteristics of Insertion Sort.
2. It has one of the simplest implementation
3. It is efficient for smaller data sets, but very inefficient for larger lists.
4. Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially
sorted list, hence it increases its efficiency.
5. It is better than Selection Sort and Bubble Sort algorithms.
6. Its space complexity is less, like Bubble Sorting, insertion sort also requires a single additional
memory space.
7. It is Stable, as it does not change the relative order of elements with equal keys
INSERTION SORT ROUTINE

void insert(int a[],int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=0;j>0&&a[j-1]>temp;j--)
a[j]=a[j-1];
a[j]=temp;

You might also like