Sie sind auf Seite 1von 7

Lovely Professional University

Department of CA

HOMEWORK-1

Homework Title/No: 1 Course Code: CAP 205

Course Instructor: Lect.Sanjay sood CourseTutor (if applicable): NA

Date of Allotment : 27/08/2010 Date of submission: 09/09/2010

Student’s Roll Number: RD3912A14 Section No.:D3912

Declaration:
I declare that this assignment is my individual work. I have not
copied from any other student’s work or from any other source except where
due acknowledgement is made explicitly in the text, nor has any part been
written for me by another person.

Student’s Sign:
Munish Sharma
Part-A
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.
Ans: - A 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 Structuring
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.

Example:-
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:

a) The first problem we faced is the wastage of the memory space.


b) The time utilize to access that data must be wasted because the data is not
stored in a particular manner.
c) The accessing of data must be difficult because data is not stored at
particular location.
d) We cannot be able to locate the position of the element stored in a
memory.
Q2:- Delineate the concept of abstract data type in data structures.

Ans: -
Abstract Data Type: - set of data values and associated operations that are
precisely specified independent of any particular implementation is known as
abstract data type.
Example:- an abstract stack data structure could be defined by two operations:
push, that inserts some data item into the structure, and pop, that deletes an
item from it. When analyzing the efficiency of algorithms that use stacks, one
may also specify that both operations take the same time no matter how many
items have been pushed into the stack, and that the stack uses a constant amount
of storage for each element.
Abstract data types are purely theoretical entities, used to simplify the
description of abstract algorithms, to classify and evaluate data structures, and
to formally describe the type systems of programming languages.

Q3:- List out Applications areas of various data structure.


Ans:- The types of application areas of data structure are as follows: -
1) Compiler Design: - The data structure is used to design the compiler.
The compiler is that which is used to translate the high level language
into low level language. When we design the compiler the data structures
are used by the designer to implement the compiler.
2) Organization of database: - The databases maintained in organizations
are made with the help of data structures. The various data structures like
arrays, linked lists are used to maintain the database.
3) Used in C language: - The data structure is also used in the C language
to implement various programs. We can be able to store the elements
using linked list, arrays, stacks and queues.
Part-B
Q4:- Elaborate the concept of “Algorithm complexity” and Complexity
notations with suitable examples.
Ans:- Firstly Algorithm: - 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.
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
O(n2) Bubble Sort, Selection Sort, Insertion Sort
O(n3) Multiplication of Square Matrix
O(an) Recursion, Towers of Hanoi

Q5:- 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.
(ii) Write the algorithm to INSERT an element in an algorithm
and also add “ABC” into 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

a.2) :- Algorithm to delete “Jane” from an array


Let NAME is an array with N elements
1. Start
2. Input array
3. Set item = “Jane”
4. Repeat for I = K to N-1
a. If ( NAME[I] == item)
i. NAME[I] = NAME[I+1]
Endif
Endloop
5. Set N = N-1
6. Exit
b.1):- Algorithm to insert an element in an array
Lets take NAME is a linear array with N elements and K is a positive integer
such that K<= N. This algorithm inserts an element ITEM into the Kth position.
1. Start
2. Input array
3. Set J = N
4. Repeat Steps while J >= K
a. Set NAME[J+1] = NAME[J]
b. J = J-1
endloop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit

b.2) :- Algorithm to insert an element “ABC” in an array


Let NAME is a linear array with N elements and K is a positive integer such
that K<= N. This algorithm inserts an element ITEM into the Kth position.
1. Start
2. Input array
3. Set J = N, ITEM= “ABC”
4. Repeat Steps while J >= K
a. Set NAME[J+1] = NAME[J]
b. J = J-1
endloop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit
Q6:- Let an array A contains 30 positive integers. Write algorithm which
finds all pair of elements whose sum is 25.

Ans:- Algorithm to find all pair of elements whose sum is 25


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

Q7:- For Q5, Write a module which finds the number of students who
have failed that is whose final grade is less than 40.
Ans: - Algorithm to the number of students who have failed whose grade
is less than 40

1. Start

2. Input n, array
3. Repeat steps ‘for’ I= 1 to n

4. If ( A[I] < 40)


Num = num +1
Endif
Endfor
5. Write “ Num”
6. Exit

Das könnte Ihnen auch gefallen