0% found this document useful (0 votes)
346 views

Master of Computer Application (MCA) - Semester 2 MC0066 - OOPS Using C

The document contains 4 programming assignments related to data structures and algorithms in C++. The first assignment asks students to write a program to generate the Fibonacci series up to a given number. The second asks students to write a program to check if a string is a palindrome. The third asks students to define a structure called 'product' and write a program to store data for 100 products using arrays. The fourth assignment asks students to explain the purpose of exception handling and the meaning of "throwing an exception".

Uploaded by

Arvind Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views

Master of Computer Application (MCA) - Semester 2 MC0066 - OOPS Using C

The document contains 4 programming assignments related to data structures and algorithms in C++. The first assignment asks students to write a program to generate the Fibonacci series up to a given number. The second asks students to write a program to check if a string is a palindrome. The third asks students to define a structure called 'product' and write a program to store data for 100 products using arrays. The fourth assignment asks students to explain the purpose of exception handling and the meaning of "throwing an exception".

Uploaded by

Arvind Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Master of Computer Application (MCA) – Semester 2

MC0066 – OOPS using C


Assignment 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:- //Fibonacci.cpp
#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 Fibonacci series till “<<number<<”is”<<endl;
if(number<0)
cout<<i;
exit(0);
else
{
cout<<i<<endl<<j<<endl;
k=i+j;
do
{
cout<<k<<endl;
k=i+j;
i=j;
j=k;
}
while(k<=number);
}
}

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:- //palindrome.cpp
#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”;
else
cout<<”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.

Ans:- Structure is a feature in C++ that enables you to define a user-defined datatype. Once you specify
the definition of the structure, you can create variables of that structure. In the above definition, we have
defined a structure using the keyword struct followed by the name of the structure (employee). All the
data items that needs to be defined are defined by specifying the datatype and name of the variable. Once
the 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 an
exception”?.
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 explicitly
designed to handle. In C++, exception handling is useful 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 and
writing the code easier.
Throwing an Exception:- if you encounter an exceptional situation in your code – that is, one where you
don’t have enough information in the current context to decide what to do – you can send information
about the error into a larger context by creating an object containing that information and “throwing” it
out of your current context. This is called throwing an exception. Here’s what it looks like:
Throw myerror(“something bad happened”);
Master of Computer Application (MCA) – Semester 2
MC0066 – OOPS using C++
Assignment Set – 2

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;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;
}
3. 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 not
create or destroy objects. Allocators are vary “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. On the whole, it is best to regard allocators as a “black box”, party because
their 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” to
other STL sequence containers or to user written containers if they satisfy the access function
requirements. Why might you want to do this? Well, if you wanted to implement a stack of items, you
might at first decide to base your stack class on the list container – let’s call it ListStack – and define
public member functions for push(),pop(), empty() and top(). However, you might later decide that
another container like a vector might be better suited to the task. You would then have to define a new
stack 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 what
is essentially the same thing start to mushroom. In addition, this approach rules out the programmer using
his or her own underlying class as the container.
4. 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 the
UML: stereotypes, tagged values, and constraints.
Stereotypes: Stereotypes allow you to extend the vocabulary of the UML so that you can create new
model 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 introduce
new building blocks that speak the language of your domain and that look like primitive, or basic, model
elements.
Stereotypes also allow you to introduce new graphical symbols for providing visual cues to the models
that 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 so
that 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 that
tagged value. It is important to mention that a tagged value is not equal to a class attribute. Instead, you
can regard a tagged value as being a metadata, since its value applies to the element itself and not to its
instances.

Graphically, a tagged value is rendered as a string enclosed by brackets, which is placed below the name
of 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 true
at 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy