You are on page 1of 6

July 2011 Master of Computer Application (MCA) Semester 2 MC0066 OOPS using C++ 4 Credits

(Book ID: B0681 & B0715)

Assignment Set 1 (40 Marks)


Answer all Questions Book ID: B0681 Each Question carries ten Marks

1. Write a program in C++ for matrix multiplication. The program should accept the
dimensions of both the matrices to be multiplied and check for compatibility with appropriate messages and give the output. #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int a[10][10],b[10][10],p[10][10],r,c,m,n,i,j,s; cout<<"Enter number of rows of first matrix: "; cin>>r; cout<<"Enter number of columns of first matrix: "; cin>>c; cout<<"Enter number of rows of second matrix: "; cin>>m; cout<<"Enter number of columns of second matrix: "; cin>>n; if (c!=m) cout<<"\nMatrices cannot be multiplied", getch(), exit(0); clrscr(); cout<<"Enter elements of first matrix...\n\n"; for (i=0;i<r;i++) for (j=0;j<c;j++) cin>>a[i][j];

cout<<"Enter elements of first matrix...\n\n"; for (i=0;i<m;i++) for (j=0;j<n;j++) cin>>b[i][j]; clrscr(); cout<<"1st matrix...\n"; for (i=0;i<r;i++) { for (j=0;j<c;j++) cout<<" "<<a[i][j]; cout<<"\n"; } cout<<"2nd matrix...\n"; for (i=0;i<m;i++) { for (j=0;j<n;j++) cout<<" "<<b[i][j]; cout<<"\n"; } cout<<"\nProduct...\n"; for (i=0;i<r;i++) { for (j=0;j<n;j++) { p[i][j]=0; for (s=0;s<c;s++) p[i][j]+=a[i][s]*b[s][j]; cout<<" "<<p[i][j]; } cout<<"\n"; } 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. #include <iostream> #include <string> using namespace std; int main() {

char str[100]; cout << "Enter word :"; cin >> str; int x = strlen(str)-1; for(int i = 0; i <= x; i++) { if (str[i] == str[x-i]) { continue; } else { cout<<"Not a palidrome"<<endl; return 0; } } cout << "Indeed Palidrome"<<endl; 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. 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. After declaring the structure, the next step is to define a structure variable. #include <iostream> #include <string> #include <sstream> using namespacestd; structmovies_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";

return0; } Structure declaration gives a skeleton or template for the structure. Book ID: B0715 4. What is the purpose of exception handling? How do you infer from the phrase, Throwing an exception?. . Of course, the thrown exception must end up someplace. This is the exception handler, and theres 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 its 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; its 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.

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

Assignment Set 2 (40 Marks)


Answer all Questions Each Question carries ten Marks

Book ID: B0681 1. Write a program which accepts a number from the user and generates prime numbers till that number 2. Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack.

Book ID: B0715 3. What are allocators? Describe the sequence container adapters. 4. Write about the following with the help of suitable programming examples: A) Throwing an Exception B) Catching an Exception

You might also like