Professional Documents
Culture Documents
USN
OR
2 Explain reference variables in C++.In the following code explain why does 8
swap method fails to swap the actual parameters. Change the code so that it
swaps the actual parameters
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
}
PART 2
3 a Explain exception handling in C++ with example program. 4
b Write a C++ function to multiply two matrices. Use reference variables and 4
BE VI semester
1 P E
USN
pointer notation
PART 4
7 a Explain the Abstract Class Linear List. 4
b Write C++ default constructor and copy constructor code for array list class 4
which represents array based implementation for Linear list
OR
8 Write C++ member function for a)inserting an element b) deleting an element 8
from array based linear list
PART 5
9 a Define class for a singly linked chain 4
b Write C++ member function to get data of node with given index position from 4
the linked list
OR
10 a Give structure definition for a node of chain(linked list) with all constructors 4
b Write C++ member function to insert a node at given index position in a single 4
linked list
BE VI semester
1 P E
USN
Solution Manual
1. Template Function : Rather than writing new version of the code for
every possible data type of the formal parameters, we can write a
generic code in which the data type is a variable whose value is to be
determined by the compiler. This generic code is written using the
template statement.
Example
BE VI semester
1 P E
USN
int count = 1;
int &cRef = count;
cRef++;
The given function code fails to swap the actual parameters as values of
actual parameters are copied to the formal parameters formal
parameters x,y are taken as the local variables and once the function is
executed local variable values are lost and the original values are as it is
we can use reference variables to change the actual parameters
BE VI semester
1 P E
USN
try
/* catch block*/
…………………….
/* catch block*/
#include<iostream.h>
void main()
int x, y;
BE VI semester
1 P E
USN
cin>>x>>y;
try
{if(y!=0)
{z=x/y;
cout<<endl<<z;
else
{throw(y);}
catch(int y)
BE VI semester
1 P E
USN
int *y;
When the program needs to actually use the integer, memory may
be allocated it using this syntax:
Y = new int;
*y = 10;
*y=10;
(or)
(or)
BE VI semester
1 P E
USN
int *y;
Y=new int(10);
delete y;
delete [] x;
BE VI semester
1 P E
USN
try {
4. A recursive function is a function that invokes itself.In direct recursion the code
for function ‘f’ contains a statement that invokes ‘f’,where as an indirect
recursion function ‘f’ invokes a function ‘g’,which invokes a function ‘h’ and soon
until function ‘f’ is again invoked.
#include<iostream.h>
#include<conio.h>
void permute(char a[], int i, int n)
{
BE VI semester
1 P E
USN
int j,temp;
if (i == n)
cout << a << endl;
else
{
for (j = i; j <= n; j++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
Column Major
BE VI semester
1 P E
USN
BE VI semester
1 P E
USN
Matrix Matrix::operator+(Matrix V)
{
if(rows!=V.rows || cols!=V.cols)
throw dimensionmismatch();
Matrix temp(rows,cols);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
temp. a[i][j]=a[i][j]+V.a[i][j];
return temp;
}
6b.
int** matmul(int** & mat1,int ** &mat2,m1,n1,m2,n2)
{
int **mat3;
if((make2dArray(mat3,mat1.rows,mat2.cols));
for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
{ sum=0;
for(k=0;k<n1;k++)
sum=sum+mat1[i][k]*mat2[k][j];
BE VI semester
1 P E
USN
mat3[i][j]=sum;
}
return mat3;
}
}
bool make2dArray(T **&x, int numberOfRows, int numberOfColumns)
{ // Create a two dimensional array,
try {
// create pointers for the rows
x = new T * [numberOfRows];
// get memory for each row
for (int i = 0; i < numberOfRows; i++)
x[i] = new int [numberOfColumns];
return true; }
catch (xalloc) {return false;}
}
7a.
AbstractDataType LinearList
{
instances
ordered finite collections of zero or more elements
operations
empty(): return true iff the list is empty, false otherwise
size(): return the list size (i.e., number of elements in the list)
get(index): return the indexth element of the list
indexOf(x): return the index of the first occurrence of x in
the list, return -1 if x is not in the list
erase(index): remove the indexth element,
elements with higher index have their index reduced by 1
insert(theIndex, x): insert x as the indexth element, elements
with theIndex >= index have their index increased by 1
BE VI semester
1 P E
USN
7b.
Default Constructor
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{// Constructor.
if (initialCapacity < 1)
{
throw illegalPrameterValue();
// u can also write a message cout<<“invalid capacity”;
}
else
arrayLength = initialCapacity;
element = new T[arrayLength];
listSize = 0;
}
}
Copy constructor
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
// Copy constructor.
arrayLength = theList.arrayLength;
listSize = theList.listSize;
element = new T[arrayLength];
copy(theList.element, theList.element + listSize, element);
}
BE VI semester
1 P E
USN
8 a) Inserting an element
template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{ // Insert theElement.
checkIndex(theIndex) ;
// valid index, make sure we have space
if (listSize == arrayLength)
{// no space, double capacity
changeLength1D(element, arrayLength, 2 * arrayLength);
arrayLength *= 2;
}
// shift elements right one position
copy_backward(element + theIndex, element + listSize, element + listSize + 1);
element[theIndex] = theElement;
listSize++;
}
b)Deleting an element
template<class T>
BE VI semester
1 P E
USN
9b.
template<class T>
T& chain<T>::get(int theIndex) const
{// Return element whose index is theIndex.
checkIndex(theIndex);
// move to desired node
BE VI semester
1 P E
USN
BE VI semester
1 P E
USN
10b.
template<class T>
void chain<T>::insert(int theIndex,
const T& theElement)
{
if (theIndex < 0 || theIndex > listSize)
{// Throw illegalIndex exception
}
if (theIndex == 0)
// insert at front
firstNode = new chainNode<T>
BE VI semester
1 P E
USN
(theElement, firstNode);
BE VI semester