VIT SKILL RACK SOLUTIONS

SKILLRACK SOLUTIONS PRACTICE PROBLEM 6

CSE 1002 PPS6
Total time : 600 mins
Challenges : 5

Question 1(Generic right shift)

Given a set of ‘n’ elements and ‘r’, write a generic function to right shift the set of elements by ‘r’ position. If the elements are to moved to position greater than ‘n’ then wrap the shift process to the beginning of the collection. For example, if the set of five elements are 1,7,8,9,12 and value of ‘r’ is 3 then the set of elements would be 8, 9, 12, 1, 7.
Input Format
Choice of data type (0 for integer and 1 for string)
Number of elements in the collection
Element1 in the collection
Element2 in the collection
….
Elementn in the collection
Value for ‘r’
Output Format
Elements in the collection after right shift
Element1
Element2
Elementn

Solution

#include
using namespace std;
#include
template
//shift the set of ‘n’ elements ele by ‘r’ positions
void right_Shift(T *ele,int n,int r);
template < class T >
void right_Shift (T *ele,int n,int r)
{
    for(int i=0;i < r;i++)  
   {  
        T temp=ele[n-1];  
        for(int j=n-1;j > 0;j--)
        ele[j]=ele[j-1];
        ele[0]=temp;
    }
}
main()
{
int n, a[20],r;
int ch;
cin>>ch;
string s[10];
if(ch==0)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
cin>>r;
right_Shift(a,n,r);
for(int i=0;i<n;i++)
cout<<a[i]<<endl; } else { cin>>n;
for(int i=0;i<n;i++) cin>>s[i];
cin>>r;
right_Shift(s,n,r);
for(int i=0;i<n;i++)
cout<<s[i]<<endl;
}
}

Question 2(Sparse matrix)

There are numerous applications of matrices, both in mathematics and other sciences. Some of them merely take advantage of the compact representation of a set of numbers in a matrix. Few to mention the application of matrices are game theory, economics, graph theory, quantum theory, probabilty theory and statistics. Each type of application use different type of data. Design a generic class matrix with functions to check if a matrix is sparse and add two matrices. A matrix is said to be sparse, if the number of zero’s in the matrix is larger than the number of non-zero elements. Define a user defined exception mismatchDimension and throw it when the dimension of matrix1 is not equal to dimension of matrix 2. Print “Dimension of matrices do not match” when the exception is caught.
Input Format:
Number of rows in matrix1
Number of columns in matrix1
Element1 in matrix1
Element2 in matrix1
Elementn in matrix1
Number of rows in matrix2
Number of columns in matrix2
Element1 in matrix2
Element2 in matrix2
Elementn in matrix2
Output Format:
Print elements of resultant matrix or Dimension of matrices do not match if exception is caught
Element1 in resultant matrix
Element2 in resultant matrix
Elementn in resultant matrix
Matrix is sparse or Matrix is not sparse

Solution

#include
#include
using namespace std;
class mismatchDimension:public exception
{
public:
void error_Msg()const;
};
template
class matrix
{
int row;
int col;
T ele[10][10];
public:
void get();
bool check_Sparse();
matrix add(matrix&);
void print();
};
void mismatchDimension :: error_Msg() const
{
    cout<<"Dimension of matrices do not match\n";
}
template < class T >
void matrix < T > :: get()
{
    cin>>row>>col;
    for(int i=0;i < row;i++)
    for(int j=0;j < col;j++)  
    cin>>ele[i][j];
}
template < class T >
bool matrix < T > :: check_Sparse()
{
    int count=0;
    for(int i=0;i < row;i++)
    for(int j=0;j < col;j++)  
    if(ele[i][j]==0)  
    count++;  
    if(count > ((row*col)-count))
    return(true);
    return(false);
}
template < class T >
matrix < T > matrix < T > :: add(matrix& m)
{
    matrix mat;
    if(row==m.row && col==m.col)
    {
        mat.row=row;
        mat.col=col;
        for(int i=0;i < row;i++)
        for(int j=0;j < col;j++)
        mat.ele[i][j]=ele[i][j]+m.ele[i][j];
    }
    else
    {
        mismatchDimension err;
        throw(err);
    }
    return(mat);
}
template < class T >
void matrix < T > :: print()
{
    for(int i=0;i < row;i++)
    for(int j=0;j < col;j++)
    cout<<ele[i][j]<<"\n";
}
int main()
{
matrix m1,m2,m3;
m1.get();
m2.get();
try
{
m3 = m1.add(m2);
m3.print();
}catch(mismatchDimension &m)
{
m.error_Msg();
}
if(m1.check_Sparse())
cout<<“Matrix is sparse”<<endl;
else
cout<<“Matrix is not sparse”<<endl;
}

Question 3(Count elements)

Given a collection of elements and a value specifying a value to be checked, develop a generic function count the number of elements with the value. Use the function to check a collection of primitive data types such as integers, floating point values and characters. Also given a set of books in a library and the name of the book to be searched, use the generic function to count the number of books in the library with the given name. Use predefined string class for representing name of the book, ‘==’ operator is overloaded in predefined class string to check if two strings are same.
Input Format:
Choice of type of input (0- int, 1 – char, 2- book)
The first line will contain the number of elements ‘n’
The next ‘n’ lines will contain the details of each element
If the elements are of type book then the order of details are:
book id, book name, author name and cost
Element to be searched
Output Format:
Count of elements in the collection

Solution

#include
#include
using namespace std;
#include
//Collection of data in which search has to be made
//and data to be searched may be different
//so have two template class variables
template
int count(T *data,int size, S search);
class book
{
int id;
string name;
string a_Name;
float price;
public:
void get();
// overload the ‘==’ operator to check if
// name of book is same as the string passed
//return true if it is same
bool operator==(string);
};
#include< string.h >
template < class T, class S >
int count(T *data,int size,S search)
{
    int count=0;
    for(int i=0;i < size;i++)  
    if(data[i]==search)  
    count++;  
    return(count);  
}  
void book :: get()  
{  
    cin>>id>>name>>a_Name>>price;
}
bool book :: operator == (string s)
{
    //c_str() converts basic string to const char * string
    if(strcmp(name.c_str(),s.c_str())==0)
    return(true);
    return(false);
}
int main()
{
    int a[20],size;
    book b[20];
    char c[20];
    int ch;
    cin>>ch>>size;
    if(ch==1)
    {
        int s;
        for(int i=0;i < size;i++)  
        cin>>a[i];
        cin>>s;
        cout<<count(a,size,s);
    }
    else if(ch==2)
    {
        char s;
        for(int i=0;i < size;i++)  
        cin>>c[i];
        cin>>s;
        cout<<count(c,size,s);
    }
    else
    {
        char s[20];
        for(int i=0;i < size;i++)  
        b[i].get();  
        cin>>s;
        cout<<count(b,size,s);
    }
    return(0);
}

Question 4(Student grade)

The grade sheet of a student to be produced in a college. As a programmer, write a program to calculate the grade of a student. Get minimum of 5 subject marks from the user. While getting the values from the keyboard, raise the following user defined exceptions in appropriate place:
1.     If the mark entered is < ‘0’ then throw NegativeMarkException 2.     If the mark entered is > ‘100’ then throw OutofRangeException
3.     Otherwise calculate the grade on the basis of following data:
Average >= ’90’ and <= ‘100’ S Grade Average >= ’80’ and < ‘90’ A Grade Average >= ’70’ and < ‘80’ B Grade Average >= ’60’ and < ‘70’ C Grade Average >= ’50’ and < ‘60’ D Grade Average >= ’40’ and < ‘50’ E Grade
Average < ‘40’ F Grade
Input Format:
Roll number of the student
Name of the student
Mark secured by student in subject1
Mark secured by student in subject2
Mark secured by student in subject3
Mark secured by student in subject4
Mark secured by student in subject5
Output Format:
Roll number of the student
Name of the student
Grade secured by student

Solution

#include
#include
using namespace std;
class negException:public exception
{
public:
void error_Msg()const;
};
class moreException:public exception
{
public:
void error_Msg()const;
};
class student
{
int rollno;
char name[30];
float marks[5];
float avg;
char grade;
public:
void get();
void print();
void calc_Avg();
void find_Grade();
};
void negException :: error_Msg() const
{
    cout<<"Negative Marks Not Allowed";
}
void moreException :: error_Msg() const
{
    cout<<"Marks Greater than 100 Not Allowed";  
}  
void student :: get()  
{  
    cin>>rollno>>name;
    for(int i=0;i < 5;i++)  
    cin>>marks[i];
}
void student :: find_Grade()
{
    if(avg >=90)
    grade='S';
    else if(avg >=80)
    grade='A';
    else if(avg >=70)
    grade='B';
    else if(avg >=60)
    grade='C';
    else if(avg >=50)
    grade='D';
    else if(avg >=40)
    grade='E';
    else
    grade='F';
}
void student :: print()
{
    cout<<rollno<<"\n"<<name<<"\n"<<grade;
}
void student :: calc_Avg()
{
    avg=0;
    try
    {
        for(int i=0;i < 5;i++)
        {
            if(marks[i] < 0)  
            {  
                negException n;  
                throw n;  
            }  
            else if(marks[i] > 100)
            {
                moreException m;
                throw m;
            }
            else
            avg+=marks[i];
        }
        avg/=5;
        find_Grade();
        print();
    }
    catch(negException& n)
    {
        n.error_Msg();
    }
    catch(moreException& m)
    {
        m.error_Msg();
    }
}
int main()
{
    student s;
    s.get();
    s.calc_Avg();
    return(0);
}

Question 5(Elements in generic box)

A generic box has three entry points:
First – Points to the first element in the box
Last – Points to the last element in the box
Mid – Points to the middle element in the box (calculated as (first+last)/2)
Insertion and deletion can be done through all the three points. Initially all the three entry points are -1. All the three entry points are updated for the first element that is inserted into the box. And for other insertions last and mid are updated. For each delete operation, update mid and last entry points. All entry points must become -1 when the box becomes empty. Provide a function to check if the box is full and also a function to check if the box is empty. Define a function print for the generic box. If the box is empty then it must print “Box empty”, print all elements from first position to last otherwise.
Input Format:
Choice for type of element (1 for int and 2 for string data)
Choice for operation in generic box
(1- check empty, 2 – is full, 3 – Insert through first entry point, 4 – Insert through last entry point, 5 – Insert through mid entry point
6 – Delete through first entry point, 7 – Delete through last entry point, 8 – Delete through mid entry point, 9 -Print, 10 – Exit
For insert options 3, 4 and 5
Element to be inserted
Output Format:
For options 1 and 2 print ‘Empty’ or ‘Not empty’, ‘Full’ or ‘Not full’ appropriately
Print elements in the box when print is given
Hint: Implement your isfull and isempty as given in the problem description

Solution

Note : The code is unable to pass private test cases. Let me know if you can spot any mistakes

#include
#include
using namespace std;
#include
template
class g_Box
{
int first,mid,last;
//initially number of elements is 0
// increment for each insert and
// decrement for each delete
int no_Of_Ele;
//represents maximum number of elements
// that can be stored
int capacity;
T *ele;
public:
// allocate memory to store elements and
// initialize all members
// let the maximum capacity be 20
g_Box();
// parameterised constructor
// with maximum capacity as parameter
g_Box(int);
bool isEmpty();
bool isFull();
void insert_First(T);
void insert_Last(T);
void insert_Mid(T);
void delete_First();
void delete_Last();
void delete_Mid();
void print();
};
template < class T >
g_Box < T > :: g_Box()
{
    no_Of_Ele=0;
    first=last=mid=-1;
    ele=new T[20];
    capacity=20;
}
template < class T >
g_Box < T > :: g_Box(int num)
{
    no_Of_Ele=0;
    first=last=mid=-1;
    ele=new T[num];
    capacity=num;
}
template < class T >
bool g_Box < T > :: isEmpty()
{
    return(no_Of_Ele==0);
}
template < class T >
bool g_Box < T > :: isFull()
{
    return(no_Of_Ele==capacity);
}
template < class T >
void g_Box < T > :: insert_First(T data)
{
    if(!isFull())
    {
        if(isEmpty())
        {
            first=last=0;
            ele[first]=data;
        }
        else
        {
            last++;
            for(int i=last;i > 0;i--)
            ele[i]=ele[i-1];
            ele[first]=data;
        }
        no_Of_Ele++;
        mid=(first+last)/2;
    }
}
template < class T >
void g_Box < T > :: insert_Last(T data)
{
    if(!isFull())
    {
        if(isEmpty())
        {
            first=last=0;
            ele[last]=data;
        }
        else
        ele[++last]=data;
        no_Of_Ele++;
        mid=(first+last)/2;
    }
}
template < class T >
void g_Box < T > :: insert_Mid(T data)
{
    if(!isFull())
    {
        if(isEmpty())
        {
            first=last=0;
            ele[first]=data;
        }
        else
        {
            last++;
            for(int i=last;i > mid;i--)
            ele[i]=ele[i-1];
            ele[mid]=data;
        }
        no_Of_Ele++;
        mid=(first+last)/2;
    }
}
template < class T >
void g_Box < T > :: delete_First()
{
    if(!isEmpty())
    {
        for(int i=0;i < last;i++)
        ele[i]=ele[i+1];
        last--;
        no_Of_Ele--;
        mid=(first+last)/2;
        if(isEmpty())
        first=last=-1;
    }
}
template < class T >
void g_Box < T > :: delete_Last()
{
    if(!isEmpty())
    {
        last--;
        no_Of_Ele--;
        mid=(first+last)/2;
        if(isEmpty())
        first=last=0;
    }
}
template < class T >
void g_Box < T > :: delete_Mid()
{
    if(!isEmpty())
    {
        for(int i=mid;i < last;i++)
        ele[i]=ele[i+1];
        no_Of_Ele--;
        last--;
        mid=(first+last)/2;
        if(isEmpty())
        first=last=-1;
    }
}
template < class T >
void g_Box < T > :: print()
{
    if(isEmpty())
    cout<<"Box empty";
    else
    for(int i=0;i <=last;i++)
    cout<<ele[i]<<"\n";
}
int main()
{
int d_Choice;
cin>>d_Choice;
g_Box g;
int data;
g_Box g1;
string data1;
if(d_Choice == 1)
{
while(1)
{
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g.isEmpty())
cout<<“Empty”<<endl;
else
cout<<“Not empty”<<endl;
}
else if(opt_Choice==2)
{
if(g.isFull())
cout<<“Full”<<endl;
else
cout<<“Not full”<<endl; } else if(opt_Choice==3) { cin>>data;
g.insert_First(data);
}
else if(opt_Choice==4)
{
cin>>data;
g.insert_Last(data);
}
else if(opt_Choice==5)
{
cin>>data;
g.insert_Mid(data);
}
else if(opt_Choice==6)
{
g.delete_First();
}
else if(opt_Choice==7)
{
g.delete_Last();
}
else if(opt_Choice==8)
{
g.delete_Mid();
}
else if(opt_Choice==9)
{
g.print();
}
else if(opt_Choice==10)
{
break;
}
}
}
if(d_Choice == 2)
{
while(1)
{
int opt_Choice;
cin>>opt_Choice;
if(opt_Choice==1)
{
if(g1.isEmpty())
cout<<“Empty”<<endl;
else
cout<<“Not empty”<<endl;
}
else if(opt_Choice==2)
{
if(g1.isFull())
cout<<“Full”<<endl;
else
cout<<“Not full”<<endl; } else if(opt_Choice==3) { cin>>data1;
g1.insert_First(data1);
}
else if(opt_Choice==4)
{
cin>>data1;
g1.insert_Last(data1);
}
else if(opt_Choice==5)
{
cin>>data1;
g1.insert_Mid(data1);
}
else if(opt_Choice==6)
{
g1.delete_First();
}
else if(opt_Choice==7)
{
g1.delete_Last();
}
else if(opt_Choice==8)
{
g1.delete_Mid();
}
else if(opt_Choice==9)
{
g1.print();
}
else if(opt_Choice==10)
{
break;
}
}
}
}

zero

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment