Uploaded by afsanamimi.wd

Data Structure and algorithm assignment 1

advertisement
Al-Madinah International University
Name: Afsana Mimi
Ref No: cc392
Subject: Data Structure and Algorithm
Subject Code: CCPS2513-OC
Lecturer: Mouiad Fadeil Alawneh
Assignment: 01
Faculty of Computer and Information Technology
Bachelor of information technology (Hons)
FEB2022B
Date:04-29-2022
Q1: What is the different between programs and algorithms with 3 examples?
Answer:
Algorithms:
Algorithm is solving the problem in a generic logical language. An algorithm is
focused more on general human understanding and sits at a higher abstract level,
that unites all computer programming languages. A problem could have the same
algorithm, but 10 different implementations in 10 different programming languages.
Example:

First you input a number from user

Then the second number

Add the number
This is the algorithm.
Programs:
A program is the implementation of this algorithm in code. The code can be in any
programming language.
A program is a set of instructions that a computer follows to perform a specified task.
Many programming languages can be used to write computer programs. Some of
the popular programming languages include Python, Java, C++, JavaScript, PHP,
and Ruby. These high-level programming languages are human-readable and
writable
Example:

The input will be the characters you type from the keyboard

The program will format the text and correct spelling

Well-organized will be displayed on the screen as output
Q2: What is the different between Linear & non- Linear with examples? (table
Answer:
Linear
Non- Linear
1. a linear data structure traverses the
1. every data item is attached to
data elements sequentially, in which
several other data items in a way that
only one data element can directly be
is specific for reflecting relationships
reached.
the data items are not arranged in
sequential structure.
2. In linear data structure, single level
2.Whereas in non-linear data structure,
is involved.
multiple levels are involved.
3. Its implementation is easy in
3. While its implementation is complex
comparison to non-linear data
in comparison to linear data structure.
structure.
4. In linear data structure, data
4. While in non-linear data structure,
elements can be traversed in a single
data elements can’t be traversed in a
run only.
single run only.
5. In a linear data structure, memory is
5. While in a non-linear data structure,
not utilized in an efficient way.
memory is utilized in an efficient way.
6. Examples are: array, stack, queue,
6. Examples are: trees and graphs
linked list etc.
Q3: One Dimensional array Find X[6], Based address= 600
Answer:
Location (X[i]) = Based address+ (i – 1)
Find X[6], Based address= 600
Location (X[i]) = Based address+ (i – 1)
Location (X[i]) =600+ (6 – 1)
=600+ 5
=605
Q4: Two-Dimensional Array A[7,9] , M=7,N=9, Based address= 800 Find A[4,6]

1-ROW- Wise Method

2-Column- Wise Method
Answer:

1-ROW- Wise Method
Location (A[i ,j ]) = Based address+ N*(i – 1)+ (j – 1)
A[7,9] , M=7,N=9, Based address= 800 Find A[4,6]
Location (A[i ,j ]) = Based address+ N*(i – 1)+ (j – 1)
Location (A[4,6]) = 800+ 9*(4 – 1)+ (6 – 1)
= 800+ 9*(3) + (5)
= 800+ 27+ 5
= 832

2-Column- Wise Method
Location (A[I ,j ]) = Based address+ M*(j – 1) +(I – 1)
A[7,9] , M=7,N=9, Based address= 800 Find A[4,6]
Location (A[I ,j ]) = Based address+ M*(j – 1) +(I – 1)
Location (A[4,6]) = 800+ 7*(6 – 1)+ (4 – 1)
= 800+ 7*(5) + (3)
= 800+ 35+ 3
= 838
Q5. Advantages and disadvantages of Arrays?
Advantages:

We can put in place other data structures like stacks, queues, linked lists,
trees, graphs, etc. in Array.

Arrays can sort multiple elements at a time.

We can access an element of Array by using an index.
Disadvantages:

We have to declare Size of an array in advance. However, we may not know
what size we need at the time of array declaration.

The array is static structure. It means array size is always fixed, so we cannot
increase or decrease memory allocation.
Q6: Create 3 d array?
Answer:
This array can store upto 12 elements (2*3*2)
Int test[2] [3] [2] ={
{
{1,2},
{3,4},
{5,6}
},
{
{7,8},
{9,10},
{11,12}
}
};
Q7: Why does Array Indexing start with 0?
Answer:
In array, the index tells the distance from the starting element. So, the first element is
at 0 distance from the starting element. So, that's why array start from 0.
Zero is allocated to all the blocks by default until we assign value for it.
Array starts with zero because first address will be pointing to the reference.
Q 8: Why we need array data structure?
Answer:
We need array data structure because,
1.Arrays are best for storing multiple values in a single variable
2. Arrays are better at processing many values easily and quickly.
3. Sorting and searching the values is easier in arrays.
Download