You are on page 1of 9

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. Answer: void main() { int m1[10][10],i,j,k,m2[10][10],add[10][10], printf("Enter number of rows and columns of first matrix MAX 10\n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of second matrix MAX 10\n"); scanf("%d%d",&r2,&c2); if(r2==c1) { printf("Enter rows and columns of First matrix \n"); printf("Row wise\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); } printf("We have entered the first matrix as follows:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",m1[i][j]);

printf("\n"); } printf("Enter rows and columns of Second matrix \n"); printf("Again row wise\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); } printf("We have entered the second matrix as follows:\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",m2[i][j]); printf("\n"); } if(r1==r2&&c1==c2) { printf("Now we add both the above matrix \n"); printf("The result of the addition is as follows;\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { add[i][j]=m1[i][j]+m2[i][j]; printf("%d\t",add[i][j]); } printf("\n"); } } else { printf("Addition cannot be done as rows or columns are not equal\n"); } printf("Now we multiply both the above matrix \n"); printf("The result of the multiplication is as follows:\n"); /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/ for(i=0;i<r1;i++) {

for(j=0;j<c2;j++) { mult[i][j]=0; for(k=0;k<r1;k++) { mult[i][j]+=m1[i][k]*m2[k][j]; /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]* } printf("%d\t",mult[i][j]); } printf("\n"); } getch(); } else { printf("Matrix multiplication cannot be done"); } }

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. Answer: // palindrome.cpp #include<iostream.h> #include<string.h> void main() { char a[10]; int I,j,len; int flag=0; cout<<Please enter the text to be checked;

cin>>a; len=strlen(a); for (i=0,j=len-1,i<len/2;i++,j--) { if(a[i]!=a[j]) {flag=1; break;} } if (flag==0) cout<<The text you have entered is a palindrome; else cout<<The text you have entered is not a palindrome; }

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. Answer: //arrayproduct.cpp #include <iostream.h> #include <conio.h> struct product { int productcode; char description; float unitprice; int qtyinhand; } p[100] void main() { int i; clrscr(); for(i=0;i<100;i++) {

cout<<Enter Product Code; cin>>p[i].productcode; cout<<Enter Product Description; cin>>p[i].description; cout<<Enter Unit Price; cin>>p[i].unitprice; cout<<Enter qty in hand; cin>>p[i].qtyinhand; } getch(); } Book ID: B0715 4. What is the purpose of exception handling? How Throwing an exception?. Answer: Purpose of exception handling

do you infer from the phrase,

Exception handling is a programming language construct to handle the occurrence of exception, special conditions that change the normal flow of program execution. We use exception handling to control your application and improved error recovery. The purpose of exception handling are to simplify the creation of large, reliable programs using less code than currently possible, with more confidence that our application doesn't have an unhandled error. Throwing an exception: If we encounter an exception situation in our code-that is, one where we don't have enough information in the current context to decide what to do-we can send information about the error inro a larger context by creating an object containing that information and throwing it out of our current context. This is called "throwing an exception." Example: Throw myerror("something bad happened."); myerror is an ordinary class, which takes a char as its argument. We can use any type when we throw, but often we will use special types created just for throwing exceptions. . .

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 Marks Each Question carries ten

Book ID: B0681 1. Write a program which accepts a number from the user and generates numbers till that number Answer: //Primegen.cpp #include <iostream.h> Void main() { int number; int prime; cout<<enter a number; cin>>number; for (int i=2; i<=number;i++) { prime=0; for (int j=2;j<=i/2;j++) { if ((i%j)==0) { prime=1; break; } } if (prime==0) cout<<i<<endl; } }

prime

2.

Implement a class stack which simulates the operations of the stack allowing LIFO

operations. Also implement push and pop operations for the stack. Answer: //stack.cpp #include <iostream.h> # 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(); }; coid stack::push(int i) { if (top==size) { cout<<stack is full; return; } stck[top]=I; top++; } int stack::pop() { if (top==0) {cout<<stack underflow; return 0; } top--;

return stck[top]; } void main() { stack a,b; a.push(1); b.push(2); a.push(3); b.push(4); cout<<a.pop()<< ; cout<<a.pop()<< ; cout<<b.pop()<< ; cout<<b.pop()<<endl; } Book ID: B0715 3. What are allocators? Describe the sequence container adapters. Answer: Allocators allocate raw memory, and return it. They do not create or destroy objects. allocators are very "low level" features in the 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. Sequence Container adapters: A container is an object that contains other objects. The sequence container store data in linear sequence. The sequence container adapters are used to change the "user interface" to other STL sequence container or to user written containers if they satisfy the access functions requirement. Example: If we wanted to implement a stack of items, we might at first decide to base our stack class on the list container-lets call it ListStack-and define public member functions for push(), pop(), empty() and top(). However, we later decide that another container like a vector might be better suited to the task. we would then have to define new stack class, with the same public interface, but based on the vector, e.g. VectorStack, so that the other

programmers could choose a list or a vector based queue. It is obvious that the number of names for what is essentially the same thing start to mushroom 4. Write about the following with the help of suitable programming examples: A) Throwing an Exception Answer: If we encounter an exceptional situation in wer code that is, one where we dont have enough information in the current context to decide what to do we can send information about the error into a larger context by creating an object containing that information and throwing it out of wer current context. This is called throwing an exception. Heres what it looks Example: throw myerror(something bad happened); myerror is an ordinary class, which takes a char* as its argument. We can use any type when we throw (including built-in types), but often well use special types created just for throwing exceptions. The keyword throw causes a number of relatively magical things to happen. First 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. B) Catching an Exception Answer: If a function throws an exception, it must assume that exception is caught and dealt with. As mentioned before, one of the advantages of C++ exception handling is that it allows we to concentrate on the problem were actually trying to solve in one place, and then deal with the errors from that code in another place. This is called the try block because try we r various function calls there. The try block is an ordinary scope, preceded by the keyword try: try { // Code that may generate exceptions }

You might also like