Beruflich Dokumente
Kultur Dokumente
Q1:- Why structuring of data is required .Take one example problem and
discuss various problems that can be faced if wrong choice has been made in
selecting data structure for the problem.
ANSWER:-
Aa data structure is a particular way of storing and organizing data in a computer so that it can
be used efficiently.
Different kinds of data structures are suited to different kinds of applications, and some are
highly specialized to specific tasks. For example, B-trees are particularly well-suited for
implementation of databases, while compiler implementations usually use hash tables to look up
identifiers.
data structure is a group of data elements grouped together under one name. These data
elements, known as members, can have different types and different lengths.
Importance of data structure:
a. Data structure is important since it describes the types of operations we can perform on
the data and how efficiently they can be carried out.
b. It also describes how dynamic we can be in dealing with our data.
c. We determine which data structures to use to store our data only after we have carefully
analyzed the problem.
EX:-
The stack is a data structure that is used to perform the following operations: -
PUSH: For inserting new element in Data Structure.
POP: For deleting element from Data Structure.
TRAVERSE: For List out the elements of Data Structure.
If we want to place CD’s in a CD cover then we have to use the stack data structure. The stack is
used because we have to pick the last item placed first. If we choose the wrong data structure
then the following problems must be faced:
Q2:- Elaborate the concept of “Algorithm complexity” and Complexity notations with
suitable examples.
ANSWER:-
ALGORITHM:-
Algorithm is a step by step processing to solve a particular problem.
Or
An algorithm is a well-defined list of steps for solving a particular problem. The algorithm
developed must be efficient so that the data must be processed in an efficient manner. The time
and space the algorithm uses are two major issues for the efficiency of algorithm. The
complexity of an algorithm is the function which gives the running time and space in terms of
the input size.
The various data structures are used to solve a problem. So, the choice of data structure depends
on many things including the type of data and the frequency with which various data operations
are applied. Sometimes, the choice of data structure involves a time-space tradeoff:
• By increasing the amount of space for storing the data, one may be able to reduce the
time needed for processing the data or vice-versa.
To obtain the complexity of an algorithm , we generally causes
(i) Worst case: the maximum value of f(n) for any possible input.
Example:-
with algorithm notations
O(1) Push and Pop Operations
O(n) Linear Search, Factorial
O(log2n) Binary Search, Insertion in a Tree
O(nlog2n) Quick Sort, Merge Sort, Heap Sort
Ques.3
Consider the array NAME
Mary
Jane
Diana
Susan
Karen
Edith
(i) Write the Algorithm to delete an element from an array and also delete “Jane”
from the array NAME
Ans: -
a.1):- Algorithm to delete an element from an array:-
Lets take NAME is an array with N elements and K is the position from where we want to
delete an element.
1. Start
2. Input array, item
3. Set item=NAME[K]
4. Repeat for I = K to N-1
a. NAME[I] = NAME[I+1]
[end of loop]
5. Set N = N-1
6. Exit
Q4:- Let an array A contains 30 positive integers. Write algorithm which finds all pair of
elements whose sum is 25.
ANSWER:
1. Start
2. Input array[30]
3. Repeat steps for I = 1 to 30
4. While ( A[I] != 0)
a. P = A[I] % 10
b. Sum = sum + p
c. A[I] = A[I] / 10
d. If(sum == 25)
i. Write A[I]
Endif
Endwhile
Endfor
5. Exit
Q5. Write a module which finds the number of students who have failed that is
whose final grade is less than 60.
ANSWER:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,m;
clrscr();
printf("\n Enter the NUMBER of students : ");
scanf("%d",&n);
printf("\n Enter the total Marks of students");
for(i=0;i<n;i++)
printf(“\n Enter marks of %d student : ”,++i);
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\n the marks of %d student == %d",++i,a[i]);
t=0;
for(i=0;i<n;i++)
{
if(a[i]<60)
m++;
}
getch();
}
OUTPUT
Enter the total number of student :10
Enter the marks for the 1 student : 11
Enter the marks for the 2 student : 40
Enter the marks for the 3 student : 70
Enter the marks for the 4 student : 99
Enter the marks for the 5 student : 77
Enter the marks for the 6 student : 88
Enter the marks for the 7 student : 32
Enter the marks for the 8 student : 56
Enter the marks for the 9 student : 72
Enter the marks for the 10 student : 39
Q.6 Write a program to sort the data using Bubble sort and
Insertion Sort technique.
for(i=0;i<n;i++)
{
temp=a[i];
j=n-1;
while((temp < a[j]) && (j>=0))
{
a[i+1]=a[i];
j=j-1;
}
a[j+1]=temp;
}
Printf(“\n Elements of array after sorting are : \n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}
Enter total number of elements : 5
Enter the elements :
2
5
7
9
0
The sorted data is :
0
2
5
7
9