0% found this document useful (0 votes)
405 views10 pages

Master of Computer Application (MCA) - Semester 2 MC0066 - OOPS Using C++ - 4 Credits

The document provides information about an assignment for the course MC0066 – OOPS using C++. It includes two assignment sets with multiple programming problems and solutions in each set. The problems cover topics like generating Fibonacci series, checking palindromes, implementing structures and classes, and stack operations. The document references two book IDs (B0681 and B0715) that contain additional problems and solutions.

Uploaded by

Varun Burman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
405 views10 pages

Master of Computer Application (MCA) - Semester 2 MC0066 - OOPS Using C++ - 4 Credits

The document provides information about an assignment for the course MC0066 – OOPS using C++. It includes two assignment sets with multiple programming problems and solutions in each set. The problems cover topics like generating Fibonacci series, checking palindromes, implementing structures and classes, and stack operations. The document references two book IDs (B0681 and B0715) that contain additional problems and solutions.

Uploaded by

Varun Burman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Master of Computer Application (MCA) – Semester 2

MC0066 – OOPS using C++ – 4 Credits


(Book ID: B0681 & B0715)

Assignment Set – 1

Book ID: B0681

1. Write a program that accepts a number ‘n’ from the user and generates Fibonacci series till n
(Fibonacci series starts with 0 and 1 and then the subsequent numbers are generated by adding
the two previous numbers in the series.

Ans. Ans.
#include
#include
voidmain()
{
inti,j,k,n;
clrscr();
printf("Enter The Numbers Of Numbers To Be Printed In The Series");
scanf("%d",&n);
i=0;
j=1;
printf("%d %d",i,j);
while(n>2)
{
k=i+j;
printf(" %d ",k);
n=n-1;
i=j;
j=k;
}
getch();
}
2. Write a program to check whether a string is a palindrome or not. Please note that palindrome
is one which remains the same if you reverse the characters in the string. For example
“MADAM”.

Ans.

#include <iostream>
#include <list>
using namespace std;

char phrases[][80] = {
  "Madam, I'm Adam.",
  "Able was I ere I saw Elba.",
  "A man, a plan, a canal: Panama!",
  "This is not one.",
  ""
};

int main()
{
  list<char> pal;
  int i, j;
  list<char>::iterator p;

  for(i = 0; *phrases[ i ]; i++) {
    for(j = 0; phrases[ i ][ j ]; j++) 
      pal.push_back(phrases[ i ][ j ]);

    cout << "Phrase # " << i << " forward: ";
    p = pal.begin();
    while(p != pal.end()) cout << *p++;
    cout << endl;

    // remove extraneous characters
    pal.remove(',');
    pal.remove('.');
    pal.remove('!');
    pal.remove(':');
    pal.remove('\'');
    pal.remove(' ');

    cout << "Phrase # " << i << " after deletions: ";
    p = pal.begin();
    while(p != pal.end()) 
       cout << *p++;
    cout << endl;

    pal.reverse(); // reverse the list

    cout << "Phrase # " << i << " backward:        ";
    p = pal.begin();
    while(p != pal.end()) 
       cout << *p++;
    cout << endl;

    pal.clear(); // get ready to try next phrase
  }
  return 0;
}

3. What is structure in C++? Define a structure named product with elements productcode,
description, unitprice and qtyinhand. Write a C++ program that implements the structure and
enables to store atleast 100 product data.

Ans. Structure is a collection of variables under a single name. Variables can be of any
type: int, float, char etc. The main difference between structure and array is that arrays are
collections of the same data type and structure is a collection of variables under a single
name.

Three variables: custnum of type int, salary of type int, commission of type float are


structure members and the structure name is Customer. This structure is declared as
follows:

In the above example, it is seen that variables of different types such as int and float are
grouped in a single structure name Customer. 

Arrays behave in the same way, declaring structures does not mean that memory is
allocated. Structure declaration gives a skeleton or template for the structure. 

After declaring the structure, the next step is to define a structure variable.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;

movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;

cout << "Enter title: ";


getline (cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;

cout << "\nYou have entered:\n";


cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";

return 0;
}

Book ID: B0715

4. What is the purpose of exception handling? How do you infer from the phrase, “Throwing an
exception”?.

Ans. Of course, the thrown exception must end up someplace. This is the exception handler, and
there’s one for every exception type you want to catch. Exception handlers immediately follow
the try block and are denoted by the keyword catch:

try {

// code that may generate exceptions

} catch(type1 id1) {

// handle exceptions of type1

} catch(type2 id2) {

// handle exceptions of type2

// etc…
Each catch clause (exception handler) is like a little function that takes a single argument of one
particular type. The identifier (id1, id2, and so on) may be used inside the handler, just like a
function argument, although sometimes there is no identifier because it’s not needed in the
handler – the exception type gives you enough information to deal with it.

The handlers must appear directly after the try block. If an exception is thrown, the exception
handling mechanism goes hunting for the first handler with an argument that matches the type of
the exception. Then it enters that catch clause, and the exception is considered handled. (The
search for handlers stops once the catch clause is finished.) Only the matching catch clause
executes; it’s not like a switch statement where you need a break after each case to prevent the
remaining ones from executing. Notice that, within the try block, a number of different function
calls might generate the same exception, but you only need one handler.
Master of Computer Application (MCA) – Semester 2

MC0066 – OOPS using C++ – 4 Credits


(Book ID: B0681 & B0715)

Assignment Set – 2

1. Write a program which accepts a number from the user and generates prime numbers till
that number

Ans.

#include<stdio.h>
#include<conio.h>
main()
{
int n,s=0,i;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<n/2;i++)
{
if(n%i==0)
s++;
}
if(s>=1)
printf("%d is not prime",n);
else
printf("%d is prime",n);
getch();
}

2. Implement a class stack which simulates the operations of the stack allowing LIFO
operations. Also implement push and pop operations for the stack.

Ans. A FIFO is an opaque structure that allows items to be placed or pushed onto it at one end (head)
and removed or pulled from it, one at at time and in sequence, from the other end (tail). A LIFO is a
similarly opaque structure that allows items to be pushed onto it at one end (head or top) and removed or
popped from it from that same end (head). This means that items of a LIFO are removed one at a time
but in the reverse order of when they were entered. Let us further suppose that our only FIFO primitives
are an append operator ‘push’ and a pull operator ’shift’. ‘push’ adds elements to the head of the FIFO
and ’shift’ grabs (and removes) items from the tail of the structure.

Therefore, we want to build a stack (LIFO) using the pipe (FIFO) structure and its two primitive operators,
‘push’ and ’shift’. Essentially, we wish to combine ‘push’ (onto head) and ’shift’ (from tail) pipe operators in
some fashion to simulate ‘push’ (onto head) and ‘pop’ (from head) stack operators.

class Stack
def initialize *s # splat operator allows variable length argument list
@q1 = []
@q2 = []
s.each { |e| push e }
end
def push v
# pure FIFO allows only 'push' (onto head) and 'shift' (from tail)
# LIFO requires 'push' (onto head) and 'pop' (from head)
empty_queue, full_queue = @q1.length == 0 ? [ @q1, @q2 ] : [ @q2, @q1 ]
empty_queue.push v
until (v = full_queue.shift).nil? # shift each item from non-empty queue,
empty_queue.push v # pushing it onto initially empty queue:
# produces new full queue in reverse entry order
end
end
def pop
full_queue = @q1.length > 0 ? @q1 : @q2
# full queue has been maintained in reverse order
# by the push method of the Stack class, so a simple
# shift is all that is needed to simulate a stack
# pop operation
el = full_queue.shift
end
def empty?
@q1.length == 0 && @q2.length == 0
end
end
 
# test:
puts "STACK: tail/bottom -> x, y, z <- head/top"
stack = Stack.new
stack.push 'x'
stack.push 'y'
stack.push 'z'
# or equivalently, stack = Stack.new('x', 'y', 'z')
until stack.empty?
puts "popping stack, item: #{stack.pop}"
end
# prints items in reverse order: 'z', 'y', 'x'

Book ID: B0715

3. What are allocators? Describe the sequence container adapters.

Ans. The vector, list and deque sequences cannot be built from each other without loss of
efficiency.
On the other hand, stacks and queues can be elegantly and efficiently implemented using those

three basic sequences. Therefore, stack and queue are defined not as separate containers, but
as

adaptors of basic containers.

A container adapter provides a restricted interface to a container. In particular, adapters do not

provide iterators; they are intended to be used only through their specialized interfaces.

The techniques used to create a container adapter from a container are generally useful for
nonintrusively adapting the interface of a class to the needs of its users.

Any class that fulfills the allocator requirements can be used as an allocator. In particular, a


class A capable of allocating memory for an object of type T must provide the types A:
pointer, A:const_pointer, A:reference, A:const_reference, and :value_type for
generically declaring objects and references (or pointers) to objects of type T. It should also provide
type A:size_type, an unsigned type which can represent the largest size for an object in the allocation
model defined by A, and similarly, a signed integral A:difference_type that can represent the
difference between any two pointers in the allocation mode.

4. Describe the extensibility mechanisms of UML.

Ans. Ans. The extensibility mechanisms allow you to customize and extend the UML by adding

new building blocks, creating new properties, and specifying new semantics in order

to make the language suitable for your specific problem domain. There are three

common extensibility mechanisms that are defined by the UML: stereotypes, tagged

values, and constraints.

Stereotypes :

Stereotypes allow you to extend the vocabulary of the UML so that you can create

new model elements, derived from existing ones, but that have specific properties that

are suitable for your problem domain. They are used for classifying or marking the

UML building blocks in order to introduce new building blocks that speak the

language of your domain and that look like primitive, or basic, model elements.

For example, when modeling a network you might need to have symbols for
representing routers and hubs. By using stereotyped nodes you can make these things

appear as primitive building blocks.

You might also like