Sie sind auf Seite 1von 13

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]; }

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. Ans: #include <stdio.h> #include <string.h> #include <ctype.h> void trimSpaces(char *buffer); int main() { char buffer[1024]; int start_ctr, end_ctr; int is_pal; fgets(buffer, 1023, stdin); trimSpaces(buffer); printf("After trim: %s\n", buffer); end_ctr = strlen(buffer) - 1; start_ctr = 0; is_pal = 1; while( (start_ctr != end_ctr) && (end_ctr > start_ctr) ) { if( tolower(buffer[start_ctr]) != tolower(buffer[end_ctr]) ) { is_pal = 0; break; } start_ctr++; end_ctr--; } if( is_pal ) { printf("Palindromic, indeed)\n"); }

else { printf("Not palindromic (\n"); } return 0; } void trimSpaces(char *buffer) { int i = 0, j = 0; for(; buffer[i] != '\0'; i++ ) { if( isalpha(buffer[i]) ) { buffer[j] = buffer[i]; j++; } } buffer[j] = '\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.

Ans: Structure is a feature in C++ that enables us to define a user-defined data type. Structures are a group of dissimilar data that are related with each other.We can define a structure using the keyword struct followed the name of the structure. For example: If we want to store all the details of an employee such as employee id(integer), name(string),department code(integer),and salary as one entity, we can do this using structure as below: Struct employee { Int empid; Char name[35]; Int deptcode; Float salary; }; //arrayproduct.cpp #include #include 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 do you infer from the phrase, Throwing an exception? Ans: Purpose of Exception Handling: Exception handling is a programming language construct to handle the occurrence of exceptions, special conditions that change the normal 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 doesnt have an unhandled error. Throwing an exception: If we encounter an exceptional situation in our code-that is, one where we dont have enough information in the current context to decide what to dowe can send information about the error into 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.

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

2. 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 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; }

Book ID: B0715 3. What are allocators? Describe the sequence container adapters.

Ans:
There are three types of sequence containers in the STL. These, as their name suggests, store data in linear sequence. They are the vector,deque and list:

vector<Type> deque<Type> list<Type>

To choose a container, decide what sort of operations you will most frequently perform on your data, and then use the following table to help you. Table 8.1 Time overhead of operations on sequence containers Operation Access first element Access last element Access random element Add/delete at beginning Add/delete at end Add/delete at random Vector constant constant constant linear constant linear Deque constant constant constant constant constant linear List constant constant linear constant constant constant

Each container has attributes suited to particular applications. The subsections and code samples below should further clarify when and how to use each type of sequence container.

4. Write about the following with the help of suitable programming examples: A) Throwing an Exception B) Catching an Exception

Ans:

Das könnte Ihnen auch gefallen