BSC Sem4 Algorithms Assignment 1 Ans
BSC Sem4 Algorithms Assignment 1 Ans
Subject: Algorithms
Subject Code: BSIT - 41
Assignment: TA (Compulsory)
Once an algorithm has been devised, it becomes necessary to show how it works. And
if we require correct answer to all possible, legal inputs the simple way to get results is
code it into a program but converting an algorithm into program is a time consuming
process. Hence, it is essential to be reasonably sure about the effectiveness of
algorithm before it is coded and at the algorithm level this process is called
“Validation”.
5. Find out the address of the element A (4,5,2) where A is a 3-dimensional array with
subscript limits 1≤ i ≤6, 1≤ j ≤7 and 0≤ k ≤4 when A is in row major addressing. Assume
base address is 1000 and word size is 2.
© NIIT Page 1 of 11
n
6. List out at least 5 real life instances where stack operations are being used.
a) Books of Library
b) Set of Cards
c) Set of CDs
d) Set of Tapes
e) Dinning Plates
7. List out at least 5 real life instances where queue and circular queue operations are
being used.
In circular queue you can come directly to the front from rear where in linear queue it is
not possible so it was the main advantage of circular queue, which saves time.
9. List out the differences between linear data structures and non-linear data structures.
Major difference between linear data structure and non – linear data structure is as
shown as below :
• In linear data structure every data element has got exactly two neighbors
or two adjacent elements where in non – linear data structure data
elements are allowed to have more than two adjacent elements.
© NIIT Page 2 of 11
11. What are the properties of a tree?
13. List out 10 situations that can be represented by means of graphs. Explain what each
vertex and edge represent.
Graphs can be used when tracing out routes between places. Each vertex represents
the place and the edge represents the various routes that can be taken between the
places.
It can be used in 3D graphics of objects where the vertex represents the corners of the
object and the edge represents the distance between these points.
© NIIT Page 3 of 11
Kuvempu University
Laboratory Assignment 1
(Academic Year 2005 - II Cycle)
Subject: Algorithms
Subject Code: BSIT - 41
Assignment: TA (Compulsory)
1. Implement the algorithm for computing the row major address of the element A(i1, i2, i3,
…, in) where A is a n-dimensional matrix with subscript limits l1≤ i1 ≤ u1, l2≤ i2 ≤ u2, l3≤ i3 ≤
u3 …. ln≤ in ≤ un. Assume base address, as B and word size is w.
Input:
1. n, dimensions
2. u1, u2,u3…un n upper limits.
3. l1,l2,l3…ln n lower limits.
4. w, word size
5. B, base address
Method:
N
A=B+{ ∑(ij-lj)Pj}w
n
# include<iostream.h>
# include<process.h>
# include<conio.h>
# define SIZE 20
class stack
{
int a[SIZE];
int tos; // Top of Stack
© NIIT Page 4 of 11
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}
int stack::isempty()
{
return (tos==0?1:0);
}
Int stack::isfull()
{
return (tos==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
cerr<<"Stack overflow error !
Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}
void main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Main Menu 1.Push 2.Pop 3.IsEmpty
4.IsFull 0.Exit";
cin>>ch;
switch(ch)
{
case 0:
exit(1); //Normal Termination of Program
© NIIT Page 5 of 11
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not
full.");
break;
default:
cout<<"Illegal Option. Please try again";
}
}//end of while
getch();
}
#include <dos.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#define MAX 5 // MAXIMUM CONTENTS IN QUEUE
class task
{
public:
virtual void dotask(){}
task(){}
int exists;
};
class note: public task
{
public:
notep(){exists=1;}
void dotask()
{
system("notepad");
}
};
class regt:public task
{
public:
regt(){exists=1;}
void dotask()
{
© NIIT Page 6 of 11
system("regedit");
}
};
case 2:
t[al]=new winex;
break;
case 3:
t[al]=new regt;
break;
case 4:
t[al]=new Bep;
break;
case 5:
cout<<"Enter Style Number:";
cin>>a;
t[al]=new MsBox("Task Performed.”,” Queue Implementation”, a);
break;
default:
© NIIT Page 9 of 11
cout<<"Programming Error"; // No Possibility of this executing
};
}
};
void main()
{
queue a;
while(1)
{
a. menu();
}
}
--------------------------------------------------------------------------------------------------------------------
Implementing Circular Queue
# include<iostream.h>
# include<conio.h>
# define SIZE 20;
class queue
{
int a[SIZE];
int front;
int rear;
public:
queue();
~queue();
void insert(int i);
int remove();
int isempty();
int isfull();
};
queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{
delete []a;
}
void queue::insert(int i)
{
if(isfull())
{
cout<<"******Queue is FULL !!!No insertion allowed
further.******";
return;
}
a[rear] = i;
© NIIT Page 10 of 11
rear++;
}
int queue::remove()
{
if(isempty())
{
cout<<"******Queue Empty !!!Value returned will be garbage.
******";
return (-9999);
}
return(a[front++]);
}
int queue::isempty()
{
if(front == rear)
return 1;
else
return 0;
}
int queue::isfull()
{
if(rear == SIZE)
return 1;
else
return 0;
}
void main()
{
clrscr();
queue q;
q.insert(1);
q.insert(2);
cout<<""<<q.remove();
cout<<""<<q.remove();
cout<<""<<q.remove();
getch();
}
© NIIT Page 11 of 11