You are on page 1of 7

Master of Computer Application (MCA) Semester 2 MC0066 OOPS using C++ 4 Credits (Book ID: B0681 & B0715)

15) Assignment Set 1 (40 Marks) Book ID: B0681 Q1. 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. A1. using namespace std; #include<iostream> int main(void) { int a = 0, b = 1, c = 0, n; cout << "Enter the upper limit for the series: "; cin >> n; cout << "0, 1, "; for (int i = 0; i < n - 2; i++) { c = a + b; a = b; b = c; cout << c << ", "; } return 0; } Q2. 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. A2. #include <cstdlib> #include <cstring> using namespace std; #include<iostream> int main(int argc, char** argv) {

char str[20]; int len = 0; cout << "Enter the word: "; cin >> str; len = strlen(str); len--; for (int i = 0; i < len; i++) { if (str[i] == str[len - i]) continue; else { cout << str << " is not a palindrome." << endl; return 0; } } cout << str << " is a palindrome." << endl; return 0; } Q3. 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. A3. Data that are of different types but are logically related can be grouped together
using structures. Structures are a group of dissimilar data that are related with each other. using namespace std; #include <iostream> #define MAX_PROD 100 struct product { char productcode[10]; char description[50]; double unitprice; int qtyinhand; }; int main(void) { struct product my_prod[MAX_PROD]; for (int i = 0; i < MAX_PROD; i++) { cout << "Enter product code for product number " << i + 1 << ": "; cin >> my_prod[i].productcode;

cout << "Enter description for product number " << i + 1 << ": "; cin >> my_prod[i].description;

cout << "Enter unit price for product number " << i + 1 << ": "; cin >> my_prod[i].unitprice;

cout << "Enter quantity in hand for product number " << i + 1 << ": "; cin >> my_prod[i].qtyinhand; cin.sync(); } }

Book ID: B071 Q4. What is the purpose of exception handling? How do you infer from the phrase, Throwing an exception? A4. The main purposes of exception handling are as follows: i) Writing error-handling code becomes trivial and it does not get mixed up with
the "normal" code that we write. The code is written without worrying about the errors the code might throw. Later in a separate section the code is written to cope with the errors. If you make multiple calls to a function, the errors are handled from that function once, in one place. Errors cannot be ignored. If a function needs to send an error message to the caller of that function, it throws an object representing that error out of the function. If the caller doesnt catch the error and handle it, it goes to the next enclosing scope, and so on until someone catches the error.

ii)

Meaning of throwing an exception: If an exceptional situation is encountered in the code that is, a situation where there is not enough information in the current context to decide what to do the information about the error can be sent into a larger context by creating an object containing that information and throwing it out of the current context. This is called throwing an exception. Heres what it looks like: throw myerror(something bad happened); myerror is an ordinary class, which takes a char* as its argument. Any type can be used to throw (including built-in types), but often special types are used which are created just for throwing exceptions. The keyword throw does the following: i) ii) It creates an object that isnt there under normal program execution, and of course the constructor is called for that object. Then the object is, in effect, returned from the function, even though that object type isnt normally what the function is designed to return.

Book ID: B0681 Q1. Write a program which accepts a number from the user and generates prime numbers till that number. A1. using namespace std; #include <iostream> void print_prime(int); int main(void) { int limit = 0; cout << "Enter the limit: "; cin >> limit; print_prime(limit); return 0; } void print_prime(int limit) { bool is_prime = false; for (int i = 2; i <= limit; i++) { for (int j = 2; j <= limit; j++) { if (i != j && i % j == 0) { is_prime = false; break; } else is_prime = true; } if (is_prime) cout << i << ", "; } cout << endl; } Q2. Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack. A2. using namespace std;

#include <iostream> #define SIZE 100 class stack { int stck[SIZE]; int top; public: stack() { top = 0; cout << "Stack initialized" << endl; } ~stack() { cout << "Stack destroyed" << endl; } void push(int i); int pop(); }; void stack::push(int i) { if (top == SIZE) { cout << "Stack is full" << endl; return; } stck[top] = i; top++; } int stack::pop() { if (top == 0) { cout << "Stack underflow" << endl; return 0; } top--; return stck[top]; }

Book ID: B0715 Q3. What are allocators? Describe the sequence container adapters. A3. Allocators allocate raw memory and return it. They do not create or destroy objects. Allocators are very low level features in STL and are designed to encapsulate memory allocation and deallocation. This allows for efficient storage by use of different schemes for particular container classes. The default allocator, alloc, is thread-safe and has good performance characteristics. On the whole, it is best to regard allocators as a black box, partly because their implementation is still in a state of change, and also because the defaults work well for most applications. Sequence container adapters are used to change the user interface to other STL sequence containers or to user written containers if they satisfy the access function requirements. Container adapters make this change of user interface possible by presenting the same public interface irrespective of the underlying container. Being templatized, they avoid name proliferation. Provided the container type used supports the operations required by the adapter class, any types can be used for underlying implementation. Q4. Describe the extensibility mechanisms of UML. A4. The extensibility mechanism allows customizing and extending the UML by adding new building blocks, creating new properties, and specifying new semantics in order to make the language suitable for a specific problem domain. There are three common extensibility mechanisms that are defined by the UML as follows: i) Stereotypes: Stereotypes allows to extend the vocabulary of the UML so that new model elements can be created, which are derived from the existing ones, but which has specific properties that are suitable for a specific 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 a specific domain and that look like primitive, or basic, model elements. Constraints: Constraints are properties for specifying semantics and/or conditions that must be held true at all times for the elements of a model. They allow extending the semantics of a UML building block by adding new rules, or modifying existing ones. Tagged Values: Tagged values are properties for specifying keyword-value pairs of model elements, where the keywords are attributes. They allow extending the properties of a UML building block so that new information in the specification of that element can be created. Tagged values can be defined for existing model elements, or for individual stereotypes, so that everything with that stereotype has that tagged value.

ii)

iii)