Vous êtes sur la page 1sur 6

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. Ans #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]:} Getch(); }

palindrome is one which remains the same if you reverse the characters in the string. For example 2. Write a program to check whether a string is a palindrome or not. Please note that M 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; elsecout<<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 atleast 100 product data.

:Structure is a feature in C++ that enables you to define a user-defined datatype. Once you specifythe definition of the structure, you can create variables of that structure. In the above definition, we havedefined a structure using the keyword struct foll owed by the name of the structure (employee). All thedata items that needs to be defined are defined by specifying the datatype and name of the variable. Oncethe 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? .Ans: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 explicitlydesigned to handle. In C++, exception handling is use ful 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 andwriting the code easier. Throwing an Exception:if you encounter an exceptional situation in your code that is, one where youdont 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 itout of your current context. This is called throwing an exception. Heres what it looks like:Throw myerror(something bad happened);

5. Write a program which accepts a number from the user and generates prime numbers till thatnumber Ans://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; } }

6.Implement a class stack which simulates the operations of the stack allowing LIFO operations.Also implement push and pop operations for the stack. Ans://stack.cpp #include<iostream.h> #define size 100class 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)

7. What are allocators? Describe the sequence container adapters .Ans: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 different s c h e m e s f o r p a r t i c u l a r container classes. The default allocator, alloc, is thread-safe and has g o o d 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 most applications. Sequence container Adapters: Sequence container adapters are used to change the user interface too t h e r S T L sequence containers or to user written container s if they satisfy the a c c e s s f u n c t i o n requirements. Why might you want to do this? Well, i f you wanted to implement a stack of items, you might 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.

8. Describe the extensibility mechanisms of UML .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 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.