Vous êtes sur la page 1sur 5

MC0066 OOPS using C++

Book ID: B0681 & B0715 SET-1 Semester-2

1 . W r i t e a p r o g r a m i n C + + f o r M a t r i x Multiplication. The program should accept the d i m e n s i o n s o f b o t h t h e m a t r i c e s t o b e m u l t i p l i e d and check for compatibility with appropriate messages and give the output. Ans:The following program accepts from the user two 2x3 matrices and multiplies the two matrices. //matrix.cpp # include <iostream.h> void main() { int a[2][3],b[2][3], c[2][3]; int i,j;cout<<Enter the elements for the first 2x3 matrix; for (i=0;i<2;i++) for (j=0;j<3;j++) { cin>>a[i][j]; } cout<<Enter the elements for the second 2x3 matrix; for (i=0;i<2;i++) for (j=0;j<3;j++) { cin>>b[i][j]; c[i][j]=a[i][j]*b[i][j]; } cout<<The resultant matrix is; for ( i=0;i<2;i++) { for ( j=0;j<3;j++) { cout<<c[i][j]<< ; } cout<< endl; } } 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:2 Palindrome: #include <iostream>

MC0066 OOPS using C++


Book ID: B0681 & B0715 SET-1 Semester-2

#include <string> using namespace std; void rString(string s, char *c0) { int i = 0; int j = s.size(); j--; while (j != -1) { c0[i] = s[j]; j--; i++; } c0[i] = '\0'; } int main() { string s = ""; string s1 = ""; string s2 = ""; cout << "Enter a string to test for a Palindrome: "; getline(cin,s); int i = 0; int slen = s.size(); int hslen = slen/2; char *c0; c0 = new char[hslen+1]; // allocate new memory for char if (slen%2 == 0) // string is even, split the string in half, reverse second half and compare two pieces { s1 = s.substr(0, hslen); // first half of string into s1 s2 = s.substr(hslen, hslen); // second half of string into s2 rString(s2, c0); // reverse s2 const char *c1 = s1.c_str(); i = strcmp(c0, c1); // compare s1 with s2 (converted to char array's) if (i == 0) { cout << " Palindrome........: " << s << endl; } else {

MC0066 OOPS using C++


Book ID: B0681 & B0715 SET-1 Semester-2

cout << not a Plaindrome" << endl; delete[] c0; return 0; } } if (slen%2 != 0) // string is odd, ignore centre char and test the two even halves { s1 = s.substr(0, hslen); // move the first half of the string to s1 s2 = s.substr(hslen+1, hslen); // ignore the middle odd char and move the second half of the string to s2 rString(s2, c0); // reverse the second half of the string const char *c1 = s1.c_str(); // convert s1 to char to test c0 vs c1 with strcmp(); i = strcmp(c0, c1); if (i == 0) // if i = 0 c0 and c1 are the same { cout << " Palindrome.........: " << s << endl; } else { cout << " not a Plaindrome" << endl; delete[] c0; return 0; } }

delete[] c0; }

3. What is structure in C++? Define a structure named product with elements productcode, description, unit price and qtyinhand. Write a C++ program that implements the structure and enables to store at least 100 product data. Ans:3 (a) Structures in C++: 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

MC0066 OOPS using C++


Book ID: B0681 & B0715 SET-1 Semester-2

{ Int empid; Char name[35]; Int deptcode; Float salary; }; (b) //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(); }

4. What is the purpose of exception handling? How do you infer from the phrase, Throwing an exception?. Ans:4 Purpose of Exception Handling: Exception handling is a programming language construct to handle the occurrence of exceptions, special conditions that change the normal flow of program execution. We use

MC0066 OOPS using C++


Book ID: B0681 & B0715 SET-1 Semester-2

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 do-we 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.

Vous aimerez peut-être aussi