MC0066 Smu Mca Sem2 2011
MC0066 Smu Mca Sem2 2011
MC0066 Smu Mca Sem2 2011
Set-1
1. Write a program that accepts a number ‘n’ from the user and generates Fibonacci
series till n (Fibonacci series starts with 0 and 1 and then the subsequent numbers are
generated by adding the two previous numbers in the series.
Ans:1
Fibonacci Numbers:
0,1,1,2,3,5,8,13,21,34,55,89,. . . …..
By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the
sum of the previous two.
#include<iostream.h>
void main()
{
int number;
int i=0,j=1,k;
cout<<”Enter the upper limit of the series”;
cin>>number;
cout<<”The fibonaci series till”<<number<<”is”<<endl;
if(number<0)
cout<<”invalid number”;
else if(number= = 0)
cout<<i;
else
{
cout<<j<<endl<<j<<endl;
K=i+j;
Do
{
cout<<k<<end;
K=i+j;
I=j;
J=k;
}
while(k<=number);
}
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
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>
#include <string>
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)
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
if (i == 0)
{
cout << " Palindrome........: " << s << endl;
}
else
{
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
{
delete[] c0;
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
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: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
{
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();}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
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
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 doesn’t have an unhandled error.
Throwing an exception:
If we encounter an exceptional situation in our code-that is, one where we don’t 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.
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
Set-2
1. Write a program which accepts a number from the user and generates prime
numbers till that number.
Ans:1
//primegen.cpp
#include<iostream.h>
Void main()
{int number;
Int prime;
Cout<<”Enter a number”;
Cin>>number;
For(int j=2;i<=number;j++)
{
Prime=0;
For(int j=2;j<=i/2;j++)
{
If((i%j)==0)
{
Prime=1;
Break;
}
}if(prime==0)
Cout<<i<<endl’
}
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
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:2
//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 stckp[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;
}
MC0066 – OOPS using C++
(Book ID: B0681 & B0715)
Ans:3
Allocators:
Allocators allocate raw memory, and return it. They do not create or destroy objects. Allocators
are very “low level” features in the STL, and are designed to encapsulate memory allocation and
deallocation. This allows for efficient storage by use of different schemes for particular container
classes. The default allocator, alloc, is thread-safe and has good performance characteristics.