MC0066 Assignment

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Master of Computer Application (MCA) MC0066 OOPS using C++

Assignment Set 1

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> void main (){ clrscr(); int i,j,k,sum,a,b,c; int m[10][10]; int n[10][10]; int l[10][10]; cout<<"ENTER THE NO. OF ROWS AND COLUMN OF FIRST MATRIX"; cin>>a>>b; cout<<"ENTER THE VALUES OF FIRST MATRIX-"; for(i=0;i<a;i++) {for(j=0;j<b;j++) {cin>>m[i][j];}} cout<<"ENTER THE NO. OF ROWS AND COLUMN OF SECOND MATRIX"; cin>>b>>c; cout<<"ENTER THE VALUES OF SECOND MATRIX-"; for(i=0;i<b;i++) {for(j=0;j<c;j++) {cin>>n[i][j];}} for(i=0;i<a;i++) {for(j=0;j<c;j++) {sum=0; for(k=0;k<b;k++) {

sum=sum+m[i][k]*n[k][j]; l[i][j]=sum; }}} cout<<"THE RESULTANT MATRIX IS-"<<endl; for(i=0;i<a;i++) { for(j=0;j<c;j++) { cout<<l[i][j]<<" "; } cout<<endl; } 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 <iostream.h> #include <conio.h> #include <string.h> void main() { clrscr(); char x[100]; char y[100]; int temp1,temp2=0; cout << "Enter the word "; cin >> x; temp1 = strlen(x)-1; for(int j= 0;j<strlen(x);j++) { y[j] = x[temp1 - j]; } y[j] = '\0'; x[j] = '\0';

for(int k =0; k<strlen(x); k++) { if (x[k]==y[k]){ temp2 = temp2 + 1; } } if (strlen(x) == temp2) {cout<<"Palindrome"; } else {cout<<"not a palindrome"; } getch(); }

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: A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures are declared in C++ using the following syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name. Programe: # 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 an exception?. Ans: The main purposes of exception handling are as follows: i) Writing error-handling code becomes trivial and it does not get mixed up with the "normal" code that we write. The code is written without worrying about the errors the code might throw. Later in a separate section the code is written to cope with the errors. If you make multiple calls to a function, the errors are handled from that function once, in one place. ii) Errors cannot be ignored. If a function needs to send an error message to the caller of that function, it throws an object representing that error out of the function. If the caller doesnt

catch the error and handle it, it goes to the next enclosing scope, and so on until someone catches the error. Meaning of throwing an exception: If an exceptional situation is encountered in the code that is, a situation where there is not enough information in the current context to decide what to do the information about the error can be sent into a larger context by creating an object containing that information and throwing it out of the current context. This is called throwing an exception. Heres what it looks like: throw my error(something bad happened); my error is an ordinary class, which takes a char* as its argument. Any type can be used to throw (including built-in types), but often special types are used which are created just for throwing exceptions. The keyword throw does the following: i) It creates an object that isnt there under normal program execution, and of course the constructor is called for that object. ii) Then the object is, in effect, returned from the function, even though that object type isnt normally what the function is designed to return.

You might also like