Вы находитесь на странице: 1из 11

2008 Paper-1 Set No-1

1. Describe in detail the execution of while statement with example.


Ans:- It is another loop like do-while loop in C. The while loop allows execution of statements inside
block of loop only if condition in loop succeeds.
Basic syntax to use while loop is:

variable initialization;
while (condition to control loop) {
statement 1;
statement 2;
..
..
iteration of variable;
}
In the pseudo code above :

Variable initialization is the initialization of counter of loop before start of while loop

Condition is any logical condition that controls the number of times execution of loop statements

Iteration is the increment/decrement of counter


Basic C program covering usage of while loop in several cases:

#include <stdio.h>
int main () {
int i = 0;
int loop_count = 5;
printf("Case1:\n");
while (i<loop_count) {
printf("%d\n",i);
i++;
}
printf("Case2:\n");
i=20;
while (0) {
printf("%d\n",i);
i++;

}
printf("Case3:\n");
i=0;
while (i++<5) {
printf("%d\n",i);
}
printf("Case4:\n");
i=3;
while (i < 5 && i >=2) {
printf("%d\n",i);
i++;
}
return 0;
}

2. Given a number, write a program using while loop to reverse the digits of
the number ?
Ans:#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
long n,j;
clrscr();
printf(Enter the No :- );
scanf(%ld,&n);
printf(\n);
printf(Reverce of the number is.);
while(n>0)
{
j = n%10;
printf(%ld,j);
n /= 10;
}
getch();
}

/*
********
OUTPUT
********
Enter the No :- 23456
Reverce of the number is.65432
*/

3.Write a program to compute the sum of the digits of a given integer number.
Ans:#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
getch();
}

4.List out the similarities and differences between structures and Unions.?
Ans:-

Structure
1.The keyword struct is used to define a structure
2. When a variable is associated with a structure,
the compiler allocates the memory for each
member. The size of structure is greater than or
equal to the sum of sizes of its members. The
smaller members may end with unused slack
bytes.
3. Each member within a structure is assigned
unique storage area of location.
4. The address of each member will be in
ascending order This indicates that memory for

Union
1. The keyword union is used to define a union.
2. When a variable is associated with a union,
the compiler allocates the memory by
considering the sizeof the largest memory. So, size
of union is equal to the size of largest member.

3. Memory allocated is shared


by individual members of union.
4. The address is same for all the members of a
union. This indicates that every member begins at

each member will start at different offset values.


5 Altering the value of a member will not affect
other members of the structure.
6. Individual member can be accessed at a time

the same offset value.


5. Altering the value of any of the member will alter
other member values.
6. Only one member can be accessed at a time.

5. What is a pointer? How is a pointer initiated? Give an example.


.
Pointer :Different from other normal variables which can store values, pointers are special variables that
can hold the address of a variable. Since they store memory address of a variable, the pointers
are very commonly said to point to variables
For Example:-.
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display qs value using ptr variable */
printf(%d, *ptr);
return 0;
}

Initializing a Pointer :A pointer is initialized in the following way :

<pointer declaration(except semicolon)> = <address of a variable>


OR
<pointer declaration>
<name-of-pointer> = <address of a variable>

Note that the type of variable above should be same as the pointer type.(Though this is not a
strict rule but for beginners this should be kept in mind).

Example :char ch = 'c';


char *chptr = &ch; //initialize
OR

char ch = 'c';
char *chptr;
chptr = &ch //initialize

6. Let a be an array of integers. Present recursive algorithms to compute The sum


of elements of the array ?
Ans:Algorithm Array Sum
Input: nonnegative integer N, and array A[1],A[2],...,A[N]
Output: sum of the N integers in array A
Algorithm Body:
j:=1
sum:=0
while j<N
sum := sum + a[J]
j:=j+1
end while
end Algorithm Array Sum

7.

Write a C program to count the number of nodes in a linked list.?

Ans:#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*temp;
void insert_data(int value)
{
struct node *var;
temp=head;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;

if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
int count_node()
{
int i=0;
temp=head;
while(temp!=NULL)
{
i++;
temp=temp->next;
}
printf("\n\nnumber of nodes are %d ",i);
}
void display()
{
struct node *var;
var=head;
printf("\nlist of elments are \n");
while(var!=NULL)
{
printf("-> %d ",var->data);
var=var->next;

}
}
int main()
{
int i,value;
char ch='y';
head=NULL;
printf(" 1.) Insert node");
printf("\n 2.) display the list");
printf("\n 3.) count number of nodes");
printf("\n 4.) exit");
while(ch=='y')
{
printf("\nChoose to do operation :");
scanf("%d",&i);
switch(i)
{
case 1 :
{
printf("\nEnter the data to be inserted in node ");
scanf("%d",&value);
insert_data(value);
//display();
break;
}
case 2 :
{
display();
break;
}
case 3 :
{
count_node();
break;
}

case 4 :
{
exit(0);
break;
}
}
}
getch();
}

8. Explain the representation of graph using adjacency matrix. Give the


necessary Algorithm?

Ans:- The adjacency matrix of a graph G = {V, E} with n vertices is a n x n Boolean/matrix. In


c
x
x

this
matrix the entry in the ith row and jth column is 1 if there is an edge from the ith vertex to
the jth vertex in the graph G. If there is no such edge, then the entry will be zero. It is to be
noted that
(i) The adjacency matrix of a undirected graph is always symmetric, i.e., M [i,j] = M [j, i]
(ii) The adjacency matrix for a directed graph need not be symmetric.
(iii) The memory requirement of an adjacency matrix is n2 bits
For example for the graph in the following figure (a) is adjacency matrix is given in (b)
1

Figure. 4 (a)
1

0
1
1
0
0

1
0
1
0
0

3
0
1
0
1
1

4
1
0
1
0
1

Figure.4 (b) Adjacency Matrix

5
0
1
1
1
0

Let us answer the following questions:

(i) Suppose if we want to know how much time will take in finding number of edges a graph
with n vertices?

a
e

Since the space needed to represent a graph is n2 bits where n is a number of vertices. All
algorithm will require at least 0 (n2) time because n2 n entries of the matrix have to be
examined. Diagonal entries are zero.

s
m
B
e

(ii) Suppose the most of the entries in the adjacency matrix are zeros, i.e., when a graph is a
sparse... How much time is needed to the find m number of edges in a graph? It will take
much less time if say 0 (e + n), where e is the number of edges is a graph and e << n2/2.
But this can be achieved if a graph is represented through an adjacency list where only h
the edges will be represented

representation of graph algorithm:


A: BoolMatrix;
var P: BoolMatrix;
n: integer
);

int i, j, k;
begin
for i :=1 to n do
for j := 1 to n do
P[i, j] := A[i, j];
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
P[i, j] := P[i, j] or (P[i, k]
and P[k, j])
end;

/*Input, the adjacency matrix of a given graph*/


/*Output, the path matrix of the graph*/
/*Input, the size of the matrix (i.e., the number
of vertices)*/

/*Step 1: Copy adjacency matrix into path


matrix*/
/*Step 2: Allow vertex k as a pivot point*/
/*Step 3: Process rows*/
/*Step 4: Process columns*/
/*Step 5*/

9. Write and explain linear search procedure with a suitable example.


Ans:- A linear search is the most basic of search algorithm you can have. A linear
search sequentially moves through your collection (or data structure) looking for a
matching value.

Implementation
function findIndex(values, target) {
for(var i = 0; i < values.length; ++i){

if (values[i] == target) { return i; }


}
return -1;
}
findIndex([7, 3, 6, 1, 0], 6)

10. Present recursive algorithms to compute Maximum element of the array?


Ans :- We can modify the standard Binary Search algorithm for the given type of arrays.
i) If the mid element is greater than both of its adjacent elements, then mid is the maximum.
ii) If mid element is greater than its next element and smaller than the previous element then maximum
lies on left side of mid. Example array: {3, 50, 10, 9, 7, 6}
iii) If mid element is smaller than its next element and greater than the previous element then maximum
lies on right side of mid. Example array: {2, 4, 6, 8, 10, 3, 1}
#include <stdio.h>
int findMaximum(int arr[], int low, int high)
{
/* Base Case: Only one element is present in arr[low..high]*/
if (low == high)
return arr[low];
/* If there are two elements and first is greater then
the first element is maximum */
if ((high == low + 1) && arr[low] >= arr[high])
return arr[low];
/* If there are two elements and second is greater then
the second element is maximum */
if ((high == low + 1) && arr[low] < arr[high])
return arr[high];
int mid = (low + high)/2;

/*low + (high - low)/2;*/

/* If we reach a point where arr[mid] is greater than both of


its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
is the maximum element*/
if ( arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1])
return arr[mid];
/* If arr[mid] is greater than the next element and smaller than the
previous
element then maximum lies on left side of mid */
if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
return findMaximum(arr, low, mid-1);
else // when arr[mid] is greater than arr[mid-1] and smaller than
arr[mid+1]
return findMaximum(arr, mid + 1, high);

}
/* Driver program to check above functions */
int main()
{
int arr[] = {1, 3, 50, 10, 9, 7, 6};
int n = sizeof(arr)/sizeof(arr[0]);
printf("The maximum element is %d", findMaximum(arr, 0, n-1));
getchar();
return 0;
}
Time Complexity: O(Logn)
This method works only for distinct numbers. For example, it will not work for an array like {0, 1, 1, 2, 2, 2,
2, 2, 3, 4, 4, 5, 3, 3, 2, 2, 1, 1}.

11. Compare and Compare and contrast singly linked list with an array.?
Ans:-

The difference between arrays and linked lists are:

- Arrays are linear data structures. Linked lists are linear and non-linear data
structures.
- Linked lists are linear for accessing, and non-linear for storing in memory
- Array has homogenous values. And each element is independent of each other
positions. Each node in the linked list is connected with its previous node which is a
pointer to the node.
- Array elements can be modified easily by identifying the index value. It is a
complex process for modifying the node in a linked list.
- Array elements can not be added, deleted once it is declared. The nodes in the
linked list can be added and deleted from the list.

Вам также может понравиться