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)

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++) {

} cout<<"\n"; } getch();

p[i][j]=0; for (s=0;s<c;s++) p[i][j]+=a[i][s]*b[s][j]; cout<<" "<<p[i][j];

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://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 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 at least 100 product data. Structure is a feature in C++ that enables you to define a user-defined datatype. Once you specify the definition of the structure, you can create variables of that structure. In the above definition, we have defined a structure using the keyword struct followed by the name of the structure (employee). All the data items that needs to be defined are defined by specifying the datatype and name of the variable. Once the definition is done, variables of type employee can be created .//arrayproduct.cpp # include <iostream.h> structure product { int productcode; char description; float unitprice; int qtyinhand; } p[100]; void main() { int i; 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; cout<<p[i].qtyinhand; } } 4. What is the purpose of exception handling? How do you infer from the phrase, Throwing anexception?.

One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected circumstance that the section of code containing the problem is not explicitly designed to handle. In C++, exception handling is useful because it makes it easy to separate the error handling code from the code written to handle the chores of the program. Doing so makes reading and writing the code easier. Throwing an Exception:if you encounter an exceptional situation in your code that is, one where you dont have enough information in the current context to decide what to do you can send informationabout the error into a larger context by creating an object containing that information and throwing it out of your current context. This is called throwing an exception. Heres what it looks like: Throw myerror(something bad happened);

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

Assignment Set 2 (40 Marks)


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

//Primegen.cpp #include<iostream.h> void main() { int number; int prime; cout<<Enter a number; cin>>number; for(int i=2;j<=i/2;j++) { If((i%j)==0) { Prime=1;break;} } If(prime==0) cout<<i <<endl; } } 2.Implement a class stack which simulates the operations of the stack allowing LIFO operations.Also implement push and pop operations for the stack.

//stack.cpp #include<iostream.h> #define size 100 class stack { int stck[size]; int top; public: stack() { top=0; cout<<stack initialised<<endl; } ~stack() { cout<<stack destroyed<<endl; } void push(int i); int pop(); }; void 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; } 3. What are allocators? Describe the sequence container adapters. Allocators do exactly what it says on the can. They allocate raw memory, and return it. They do notcreate or destroy objects. Allocators are vary low level features in the STL, and are designed toencapsulate memory allocation and deallocation. This allows for efficient storage by use of differentschemes 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, party becausetheir implementation is still in a state of change, and also because the defaults work well for mostapplications. Sequence container Adapters: Sequence container adapters are used to change the user interface to oher STL sequence containers or to user written containers if they satisfy the access function equirements. Why might you want to do this? Well, if you wanted to implement a stack of items, you ight at first decide to base your stack class on the list container lets call it ListStack and define public member functions for push(),pop(), empty() and top(). However, you might later decide thatanother container like a vector might be better suited to the task. You would then have to define a newstack class, with the same public interface, but based on the vector, e.g. vectorStack, so that other programmers could choose a list or a vector based queue. It is obvious that the number of names for whatis essentially the same thing start to mushroom. In addition, this approach rules out the programmer usinghis or her own underlying class as the container.

4. Describe the extensibility mechanisms of UML. 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 theUML: stereotypes, tagged values, and constraints. Stereotypes: Stereotypes allow you to extend the vocabulary of the UML so that you can create newmodel 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 introducenew building blocks that speak the language of your domain and that look like primitive, or basic, modelelements.

Stereotypes also allow you to introduce new graphical symbols for providing visual cues to the modelsthat speak the vocabulary of your specific domain, as shown below.Graphically, a stereotype is rendered as a name enclosed by guillemots and placed above the name of another element.Alternatively, you can render the stereotyped element by using a new icon associated with that stereotype. Tagged Values:Tagged values are properties for specifying keyword-value pairs of model elements,where the keywords are attributes. They allow you to extend the properties of a UML building block sothat you create new information in the specification of that element. Tagged values can be defined for existing model elements, or for individual stereotypes, so that everything with that stereotype has thattagged value. It is important to mention that a tagged value is not equal to a class attribute. Instead, youcan regard a tagged value as being a metadata, since its value applies to the element itself and not to itsinstances.Graphically, a tagged value is rendered as a string enclosed by brackets, which is placed below the nameof another model element. The string consists of a name (the tag), a separator (the symbol =), and a value(of the tag), as shown below. Constraints:Constraints are properties for specifying semantics and/or conditions that must be held trueat all times for the elements of a model. They allow you to extend the semantics of a UML building block by adding new rules, or modifying existing ones.For example, you can use constraint notation to provide some properties of associations, such as order and changeability. Refer to the diagram below to understand the use of constraints.

You might also like