You are on page 1of 7

`SRI RAMAKRISHNA INSTITUTE OF TECHNOLOGY

(Approved by AICTE, New Delhi & Affiliated to Anna University)


(Accredited by NAAC with ‘A’ Grade and Accredited by NBA)
Coimbatore -641 010
Academic Year 2016 – 17
Internal Test – II (August, 2016)

CS 6301- PROGRAMMING AND DATA STRUCTURES – II


(Common to both B.E.(CSE) and B.Tech.(IT))
Class : II B.E.(CSE)/ B.Tech.(IT) Semester : 03

Portion : ½ of Unit II and UNIT III Max. Marks : 50

Duration : 90 mins Date of Exam :02-09-2016


PART - A (5 × 2 = 10 Marks) Course Outcome
Answer all questions / Cognitive Level
What is pure virtual function?
1. CO1 / C1
Pure virtual Functions are virtual functions with no definition. They start with virtual
keyword and ends with = 0. Here is the syntax for a pure virtual function,
State the need for template.

2. Templates are a feature of the C++ programming language that allows CO1 / C2
functions and classes to operate with generic types. This allows a function or
class to work on many different data types without being rewritten for each one.
Enlist the function adaptors.
A function adaptor is an instance of a class that adapts a global or member function so
3. CO1 / C2
that the function can be used as a function object. A function adaptor may also be
used to alter the behavior of a function
List down any two manipulators in C++.
Setw()
4. CO1 / C2
Setfill()

5. Give an example for arithmetic exception. CO1 /C2


#include<iostream.h>
#include<conio.h>
void main()
{
   int a,b,c;
   float  d;
   clrscr();
   cout<<"Enter the value of a:";
   cin>>a;
   cout<<"Enter the value of b:";
   cin>>b;
   cout<<"Enter the value of c:";
   cin>>c;
   
   try
   {
              if((a-b)!=0)
              {
                 d=c/(a-b);
                 cout<<"Result is:"<<d;
              }
              else
              {
                 throw(a-b);
              }
   }
 
   catch(int i)
   {
              cout<<"Answer is infinite because a-b is:"<<i;
   }
  
   getch();
}

PART - B (4 × 6= 24 Marks)
Answer all questions
Debug in the following program.
(a) ifstream.infile(“DATA”);
(b) finl.getline(); //finl is input stream
(c) if(finl.eof() == 0) exit(1);
(d) close(f1);
(e) infile.open(argc);
(g) sfinout.open(file,ios::in |ios::out| ios::ate);

(a) Error : Improper use of ifstream.

Correction : ifstream infile (“DATA”);


(b) Here you must give two argument for getline ( ) function one is string type
variable name and another is string size. Such as
char s[20];
getline(s, 20);
6. CO1 / C4
(c) If we write
if (fin1, eof ( ) = = 0)
exit (1);
it input only one character from file and then it will exit.
(d) close ( ) is void type argument function so it will be fl.close ( )
(e) argc is called argument counter and agrv is called argument vector so to
open data file we can write the statement like this:
infile.open (argv[1]) :

(f) The argument file must write as ” file” because this is string type.
Correction :
fstream sfinout;
sfinout.open (“file”, ios::inios::out, ios::ate);

7. The formatted output of a Program is as follows: CO1 / C3


ABCD
******ABCD
ABCD******
10.20304@$%^*
102.3004
**********
Write a program and apply appropriate ios class function for the above.

#include <iostream.h>
void main()
{
     cout.width(5);
     cout << "99" << endl;
  
     cout.setf(ios::left);
     cout.width(5);
     cout << "99" << endl;
  
     cout.setf(ios::right);
     cout << "99" << endl;
}

Write a program to read 10 numbers. Generate the exceptions for negative and
even numbers. Provide handlers to display appropriate messages.

#include <iostream>
using namespace std;

int main()
{
double a, b, c;

// Request two numbers from the user


cout << "Please provide two numbers\n";
8. cout << "First Number: "; CO1/ C3
cin >> a;
cout << "Second Number: ";
cin >> b;

// Multiply the numbers and display the result


c = a * b;

cout << "\n" << a << " * " << b << " = " << c << "\n\n";
return 0;
}

Apply class template concept for finding the scalar product for int and float type
vector.
#include <iostream>
#include <vector>

using namespace std;

class Scalar_product
{
public:
Scalar_product(vector<double> a, vector<double> b);
};
double scalar_product(vector<double> a, vector<double> b)
9. { CO1 /C3
double product = 0;
for (int i = 0; i <= a.size()-1; i++)
for (int i = 0; i <= b.size()-1; i++)
product = product + (a[i])*(b[i]);
return product;
}

int main() {
cout << product << endl;
return 0;
}
PART - C (2 × 8 = 16 Marks)
Answer any Two Questions
10. Consider an example of book shop which sells books and video tapes. It is CO1 / C6
modeled by book and tape classes. These two classes are inherited from the
base class called media. The media class has common data members such as
title and publication. The book class has data members for storing a number of
pages in a book and tape class has the playing time in a tape. Each class will
have the member functions such as read ( ) and show ( ). In a base class, these
members have to be defined as virtual functions. Write a program which models
this class hierarchy and processes their objects.

#include
#include
#include
#include
#include
 
class book
{
char **author;
char **title;
float *price;
char **publisher;
int *stock_copy;
int size;
 
public:
book();
void book_detail(int i);
void buy(int i);
int search();
};
 
book :: book()
{
size=4;
author=new char*[80];
title=new char*[80];
publisher=new char*[80];
 
for(int i=0;i&lt;size;i++)
{
author[i]=new char[80];
title[i]=new char[80];
publisher[i]=new char[80];
}
stock_copy=new int[size];
price=new float[size];
 
title[0]="object oriented programming with c++";
title[1]="programming in ANCI";
title[2]="electronic circuit theory";
title[3]="computer algorithm";
 
author[0]="balagurusamy";
author[1]="balagurusamy";
author[2]="boyelstade";
author[3]="shahani";
 
stock_copy[0]=200;
stock_copy[1]=150;
stock_copy[2]=50;
stock_copy[3]=80;
 
price[0]=120.5;
price[1]=115.75;
price[2]=140;
price[3]=180.5;
 
}
void book::book_detail(int i)
{
cout&lt;&lt;" *********book detail **********\n";
cout&lt;&lt;setw(12)&lt;&lt;"Title"&lt;&lt;setw(25)&lt;&lt;"Auth
or Name"
&lt;&lt;setw(18)&lt;&lt;"Stock copy\n";
cout&lt;&lt;setw(15)&lt;&lt;title[i]&lt;&lt;setw(16)&lt;&lt;auth
or[i]&lt;&lt;setw(15)
&lt;&lt;stock_copy[i]&lt;&lt;"\n";
 
}
int book::search()
{
char name[80],t[80];
cout&lt;&lt;"Enter author name : ";
 
gets(name);
cout&lt;&lt;"and title of book in small letter : ";
gets(t);
 
int count=-1;
int a,b;
for(int i=0;i&lt;size;i++)
{
 
a=strcmp(name,author[i]);
b=strcmp(t,title[i]);
if(a==0 &amp;&amp; b==0)
 
count=i;
 
}
 
return count;
}
 
void book::buy(int i)
{
if(i&lt;0)
cout&lt;&lt;" This book is not available \n";
 
else
{
book_detail(i);
cout&lt;&lt;" How many copies of this book is required : ? ";
int copy; cin&gt;&gt;copy;
int remaining_copy;
if(copy&lt;=stock_copy[i])
{
remaining_copy=stock_copy[i]-copy;
float total_price;
total_price=price[i]*copy;
cout&lt;&lt;"Total price = "&lt;&lt;total_price&lt;&lt;" TK\n";
}
else
cout&lt;&lt;" Sorry your required copies is not available \n";
}
}
 
int main()
{
book b1;
int result;
 
result=b1.search();
b1.buy(result);
return 0;
}

11. Create a class FLOAT that contains one float data member. The user requires CO1 / C6
all the four arithmetic operators so that they operate on the objects of FLOAT.

#include<iostream.h>
 
class FLOAT
{
          float data;
           public:
           FLOAT(){};
           FLOAT(float d)
           { data=d;}
           FLOAT operator+(FLOAT f1);
           FLOAT operator-(FLOAT f2);
           FLOAT operator*(FLOAT f3);
           FLOAT operator/(FLOAT f4);
          void display();
};
FLOAT FLOAT::operator+(FLOAT f1)
{
           FLOAT temp;
           temp.data=data+f1.data;
          return (temp);
}
FLOAT FLOAT::operator-(FLOAT f2)
{
          FLOAT temp;
          temp.data=data-f2.data;
          return temp;
}
FLOAT FLOAT::operator*(FLOAT f3)
{
          FLOAT temp;
      temp.data=data*f3.data;
         return temp;
}
FLOAT FLOAT::operator/(FLOAT f4)
{
             FLOAT temp;
             temp.data=data/f4.data;
             return (temp);
}
void FLOAT:: display()
{
             cout<<data<<"\n";
}
int main()
{
                      FLOAT F1,F2,F3,F4,F5,F6;
                      F1=FLOAT(2.5);
                      F2=FLOAT(3.1);
                      F3=F1+F2;
                      F4=F1-F2;
                      F5=F1*F2;
                      F6=F1/F2;
                      cout<<" F1 = ";
                      F1.display();
                     cout<<" F2 = ";
                     F2.display();
                     cout<<" F1+F2 = ";
                     F3.display();
                    cout<<" F1-F2 = ";
                    F4.display();
                    cout<<" F1*F2 = ";
                    F5.display();
                    cout<<" F1/F2= ";
                    F6.display();
     return 0;
}

Write a Program to reads an array of numbers from file named sanjay.dat.


Create odd.dat store the odd numbers and even.dat to store even numbers.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
 
int main()
{
             char name[100],n[100],number[100];
 
              ifstream  santo("robin.txt");
             cout<<" Enter your desired name to find mobile
number :" ;
              cin>>n;
 
        again:
          santo>>name;
12.           if(!strcmp(name,n)) CO1 / C6
           {
                   santo.getline(number,100);
                   cout<<setw(-
20)<<name<<setw(25)<<number<<"\n";
 
           }
         else
          {
                if(santo.eof()!=0)
                cout<<" Sorry your input name is not found in
list \n";
                 else
                 goto again;
          }
     return 0;
}

You might also like