Skill Rack PPS3

CSE 1002 PPS3 new Winter ’16 (Id=2260)
1-Feb-2017 00:00 to  17-Feb-2017 00:00
Total time : 600 mins
Challenges : 5

Question 1

Vectors are used in everyday life to locate individuals and objects. They are also used to describe objects acting under the influence of an external force. A vector, is a quantity with a direction and magnitude. A vector with magnitude alone, is an n-tuple (list of n numbers) Given two vectors v1, v2, develop an algorithm and the subsequent C++ program to determine the sum and dot product of the two vectors. Sum of  v1 and v2, written as v1+v2,   is got by adding the respective elements: first element of  v1+v2 is got by  adding the first element of v1  with the first element of v2; second element of v1+v2 is got by adding the second element of v1 and the second element of v2 and so on. The dot product v1*v2 is a scalar ( a number), which is the sum of the product of the respective elements of v1 and v2. In other words, dot product of two vectors is got by adding : the product of the first element of v1 with the first element of v2, product of the second element of v1 with the second element of v2 and so on
if the input vectors v1 and v2 are:
v1= 3, 4, -5, 6, 7, 8
v2 = 4, 6, 8, 10, 1, 4
Then sum of the two vectors is 7, 10, 3, 16, 8, 12 and their dot product is : 3*4+4*6+(-5)*8+6*10+7*1+8*4
12+24+(-40)+ 60+7+32=95
Input Format
First line contains the number elements in vector v1 n
Next line contains the number elements in vector v2, m
Next ‘n’ lines contains the elements of the vector v1
Next ‘m’ lines contains the elements of the vector v2
Output Format
First ‘n’ lines contain the sum of the vectors
Next line contains the dot product of the vectors

Question 2

An automatic vending machine has many snack items arranged in its shelves. Each item has an item code and a cost. A user can enter the amount and key-in the item code. If the itemcode matches an entry in the item list and amount entered by user is less than cost of the item, then the item will be dispensed. Develop an algorithm and write a C++ code to print name of the item if the amount entered is greater than the cost of the item and item code is valid. If the amount is less than the cost of the item then throw a double exception, if the item code entered is wrong then throw a integer exception and when the item entered is not in stock throw a string exception. Print appropriate messages such as “Insufficient amount”, “Wrong item code” or “Less stock”.
Input Format:
Enter the number of items in the vending machine
Itemcode-1
Cost of item1
stock in hand item1
Itemcode-2
Cost of item2
stock in hand item2
Itemcode-n
Cost of item1
stock in hand item3
Item code entered by user
Cost entered by user
Output Format:
Print either item name or “Insufficient amount” or “Wrong item code” or “Less Stock”
Boundary Conditions:
Number of Item  >1

Solution

#include< iostream >
using namespace std;
int main()
{
    struct item
    {
        int code,cost,stock;
    }stack[20];
    int num,i,cd,co,flag=0;
    cin>>num;
    for(i=0;i < num;i++)      cin>>stack[i].code>>stack[i].cost>>stack[i].stock;
    cin>>cd>>co;
    for(i=0;i < num;i++)       {                      if(stack[i].code==cd)                      {                            flag=1;                            if(co > stack[i].cost)
                      {
                            if(stack[i].stock>0)
                            cout<<stack[i].code;
                            else
                            cout<<"Less stock";
                      }
                      else
                      cout<<"Insufficient amount";
        }
    }
    if(flag==0)
    cout<<"Wrong item code";
    return(0);
}

Input

INPUT :

num - number of items
item - values of each item

Output

OUTPUT :

if(stack[i].stock > 0)
cout<<stack[i].code;
else if
cout<<"Insufficient amount";
else if
cout<<"Less stock";
else
cout<<"Wrong item code";

Processing

for(i=0;i < num;i++)  {                  if(stack[i].code==cd)            {                            flag=1;                      if(co > stack[i].cost)
            {
                if(stack[i].stock>0)
                cout<<stack[i].code;
                else
                cout<<"Less stock";
            }
            else
            cout<<"Insufficient amount";
        }
    }
    if(flag==0)
    cout<<"Wrong item code";

Pseudocode

BEGIN

Read num and stack
for(i=0;i < num;i++)  {                        if(stack[i].code==cd)            {                     flag=1;                    if(co > stack[i].cost)
            {
                if(stack[i].stock>0)
                cout<<stack[i].code;
                else
                cout<<"Less stock";
            }
            else
            cout<<"Insufficient amount";
        }
    }
    if(flag==0)
    cout<<"Wrong item code";

END

Question 3

A government organization received tenders from ‘n’ vendors for their campus renovation work. It has to determine and select the vendor who had quoted the least amount for this renovation work. Given the registration number, address and amount quoted by vendor, design an algorithm and write a C++ code to print the details of vendor for whom the tender will be given. Write a function that returns the details of vendor by reference concept.
Input Format:
Number of tenders received
Registration number of vendor1
Address of vendor1
Amount quoted by vendor1
Registration number of vendor2
Address of vendor2
Amount quoted by vendor2
………
………….
Registration number of vendor n
Address of vendor n
Amount quoted by vendor n
Output Format:
Print the registration number of vendor who won the tender
Print address of vendor who won the tender
Boundary Conditions:
Number of Tender >1

Solution

#include< iostream >
#include< stdlib.h >
using namespace std;
struct vendor
{
    int reg,amt;
    char add[30];
}list[10];
void find_lowest(vendor *ptr,int num)
{
    int i;
    *ptr=list[0];
    for(i=1;i < num;i++)
    if(list[i].amt < (*ptr).amt)        *ptr=list[i];  }   int main()     {           int num;         cin>>num;
    vendor final={0,0,""};
    for(int i=0;i < num;i++)          cin>>list[i].reg>>list[i].add>>list[i].amt;
    find_lowest(&final,num);
    cout<<final.reg<<"\n"<<final.add;
    return(0);
}

Input

INPUT :

num - number of vendors
list - details of all vendors

Output

OUTPUT :

cout<<final.reg<<"\n"<<final.add;

Processing

void find_lowest(vendor *ptr,int num)
{
    int i;
    *ptr=list[0];
    for(i=1;i < num;i++)
    if(list[i].amt < (*ptr).amt)
    *ptr=list[i];
}

Pseudocode

BEGIN

Read num and list
void find_lowest(vendor *ptr,int num)
{
    int i;
    *ptr=list[0];
    for(i=1;i < num;i++)
    if(list[i].amt < (*ptr).amt)
    *ptr=list[i];
}
final=find_lowest(&final,num);
cout<<final.reg<<"\n"<<final.add;

END

Question 4

Each student in a class of ‘n’ plays at least one indoor game chess, carrom and scrabble. Given three list of names of students who play chess, scrabble and carrom, develop an algorithm and write a C++ code to find the students who can
(i) Play chess and carrom
(ii) Chess, carrom but not scrabble
Understand the template code and implement the member functions of the class
Input Format
Number of students who play chess
Name of student1 who plays chess
Name of student2 who plays chess
Name of student-n who plays scrabble
Number of students who play scrabble
Name of student1 who plays scrabble
Name of student2 who plays scrabble
Name of student-n who plays scrabble
Name of student-n who plays scrabble
Number of students who play carrom
Name of student1 who plays carrom
Name of student2 who plays carrom
Name of student-n who plays carrom
Output Format
Name of students who play chess and carrom. order the names as given in the chess input list . separate names by a comma
Name of students who play chess and carrom but not scrabble. Order the names as given in the chess input list. separate names by a comma
Boundary Conditions
Number of students in class will not be more than 30
Length of name of students in class will not be more than 20
Assume that none of the set will become empty

Solution

#include
#include
using namespace std;
class set
{
int num_Of_Ele;
char names[30][20];
public:
void get();
void print()const;
set intersection(set&);
set difference(set&);
};
void set :: get()
{
    cin>>num_Of_Ele;
    for(int i=0;i < num_Of_Ele;i++)           cin>>names[i];
}
void set :: print() const
{
    for(int i=0;i < num_Of_Ele-1;i++)
    cout<<names[i]<<",";
    cout<<names[num_Of_Ele-1]<<"\n";
}
set set :: intersection(set& var)
{
    set inter;
    inter.num_Of_Ele=0;
    int i,j;
    for(i=0;i < num_Of_Ele;i++)
    for(j=0;j < var.num_Of_Ele;j++)
    if(strcmp(names[i],var.names[j])==0)
    {
        strcpy(inter.names[inter.num_Of_Ele++],names[i]);
        break;
    }
    return(inter);
}
set set :: difference(set& var)
{
    set diff;
    diff.num_Of_Ele=0;
    int i,j,flag;
    for(i=0;i < num_Of_Ele;i++)
    {
        flag=0;
        for(j=0;j < var.num_Of_Ele;j++)
        if(strcmp(names[i],var.names[j])==0)
        {
            flag=1;
            break;
        }
        if(flag==0)
        strcpy(diff.names[diff.num_Of_Ele++],names[i]);
    }
    return(diff);
}
int main()
{
set chess, carrom,scrabble;
chess.get();
carrom.get();
scrabble.get();
set inter = chess.intersection(carrom);
inter.print();
set diff = inter.difference(scrabble);
diff.print();
return 0;
}

Input

INPUT :

void set :: get()
{
    cin>>num_Of_Ele;
    for(int i=0;i < num_Of_Ele;i++)        cin>>names[i];
}

Output

OUTPUT :

void set :: print() const
{
    for(int i=0;i < num_Of_Ele-1;i++)
    cout<<names[i]<<",";
    cout<<names[num_Of_Ele-1]<<"\n";
}

Processing

set set :: intersection(set& var)
{
    set inter;
    inter.num_Of_Ele=0;
    int i,j;
    for(i=0;i < num_Of_Ele;i++)
    for(j=0;j < var.num_Of_Ele;j++)
    if(strcmp(names[i],var.names[j])==0)
    {
        strcpy(inter.names[inter.num_Of_Ele++],names[i]);
        break;
    }
    return(inter);
}
set set :: difference(set& var)
{
    set diff;
    diff.num_Of_Ele=0;
    int i,j,flag;
    for(i=0;i < num_Of_Ele;i++)
    {
        flag=0;
        for(j=0;j < var.num_Of_Ele;j++)
        if(strcmp(names[i],var.names[j])==0)
        {
            flag=1;
            break;
        }
        if(flag==0)
        strcpy(diff.names[diff.num_Of_Ele++],names[i]);
    }
    return(diff);
}

Pseudocode

BEGIN

Read void set :: get()
set set :: intersection(set& var)
{
    set inter;
    inter.num_Of_Ele=0;
    int i,j;
    for(i=0;i < num_Of_Ele;i++)
    for(j=0;j < var.num_Of_Ele;j++)
    if(strcmp(names[i],var.names[j])==0)
    {
        strcpy(inter.names[inter.num_Of_Ele++],names[i]);
        break;
    }
    return(inter);
}
set set :: difference(set& var)
{
    set diff;
    diff.num_Of_Ele=0;
    int i,j,flag;
    for(i=0;i < num_Of_Ele;i++)
    {
        flag=0;
        for(j=0;j < var.num_Of_Ele;j++)
        if(strcmp(names[i],var.names[j])==0)
        {
            flag=1;
            break;
        }
        if(flag==0)
        strcpy(diff.names[diff.num_Of_Ele++],names[i]);
    }
    return(diff);
}
Output void set :: print() const

END

Question 5

Snakes and Ladders is an ancient Indian board game. It is played between two or more players on a gameboard having numbered, gridded squares (which is in the form a matrix). The board has a  number of  pictures of the “ladders” and “snakes”  on the board, each connecting two specific board squares. The object of the game is to navigate one’s game piece, according to die rolls, from the start (bottom square) to the finish (top square), helped or hindered by ladders and snakes respectively.
Given an m X n board, details of snakes and ladders in the board, current position of coin of player1 and sequence of dice rolls by player1, design an algorithm and write a C++ code to find the new position of coin of player1. The grids of the board are numbered from 1 to m*n. After a die roll, if the coin ends up in a square with snake / ladder start position then update position to end position of snake / ladder. Each grid in the board is given a number ‘g’ which is computed as (r-1)*n + c, where ‘r’ and ‘c’ are the row and column position of the grid.
Understand the template code and implement the functions appropriately
Input Format
Value for ‘m’
Value for ‘n’
Number of snakes in board k
Start Grid position of snake1
End Grid position of snake1
Start Grid position of snake2
End Grid position of snake2
….
Start Grid position of snake-k
End Grid position of snake-k
Number of ladders in board t
Start Grid position of ladder1
End Grid position of ladder1
Start Grid position of ladder2
End Grid position of ladder2
….
Start Grid position of ladder-t
End Grid position of ladder-t
Current row position of coin of player1
Current column position of coin of player1
Number of dice rolls
Value of roll1
Value of roll2
Value of roll-n
Output Format
Final row position of player1
Final column position of player1
Boundary Conditions
Assume that there will be only maximum of ten ladders and ten snakes
The position will never go out of the board

Solution

#include
using namespace std;
typedef struct
{
//elements to indicate the number of rows and columns
int row,col;
}board;
//A struct to define a position in the board
typedef struct
{
int row;
int col;
}position;
//Struct to represent the details of snake positions
typedef struct
{
int num;
int st_Grid[10];
int end_Grid[10];
}snakes;
//Struct to represent the details of ladder positions
typedef struct
{
int num;
int st_Grid[10];
int end_Grid[10];
}ladders;
//Struct to represent the details of rolls in the play
typedef struct
{
int num;
int roll[10];
}rolls;
// Function to read input from user board, snake and ladder details
// then current position and details of rolls
// Parameters are passed by reference
void read_Values(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos,rolls &r);
//Function to compute new position of coin
int find_New_Pos(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos, rolls &r);
void read_Values(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos,rolls &r)
{
    int i;
    cin>>b.row>>b.col;
    cin>>snake_Det.num;
    for(i=0;i < snake_Det.num;i++)       cin>>snake_Det.st_Grid[i]>>snake_Det.end_Grid[i];
    cin>>ladder_Det.num;
    for(i=0;i < ladder_Det.num;i++)        cin>>ladder_Det.st_Grid[i]>>ladder_Det.end_Grid[i];
    cin>>cur_Pos.row>>cur_Pos.col;    
    cin>>r.num;
    for(i=0;i < r.num;i++)       cin>>r.roll[i];
}
int find_New_Pos(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos,rolls &r)
{
    int i,j;
    int pos=((cur_Pos.row-1)*b.row)+cur_Pos.col;
    for(i=0;i < r.num;i++)
    {
        pos+=r.roll[i];
        for(j=0;j < snake_Det.num;j++)
        if(pos==snake_Det.st_Grid[j])
        {
            pos=snake_Det.end_Grid[j];
            break;
        }
        for(j=0;j < ladder_Det.num;j++)
        if(pos==ladder_Det.st_Grid[j])
        {
            pos=ladder_Det.end_Grid[j];
            break;
        }
    }
    return(pos);
}
int main()
{
board b;
position cur_Pos;
int final_Grid;
rolls r;
snakes snake_Det;
ladders ladder_Det;
read_Values(b,snake_Det,ladder_Det,cur_Pos,r);
final_Grid = find_New_Pos(b,snake_Det,ladder_Det,cur_Pos,r);
cout<<final_Grid;
}

Input

INPUT :

cin>>b.row>>b.col;
cin>>snake_Det.num;
for(i=0;i < snake_Det.num;i++)    cin>>snake_Det.st_Grid[i]>>snake_Det.end_Grid[i];
cin>>ladder_Det.num;
for(i=0;i < ladder_Det.num;i++)    cin>>ladder_Det.st_Grid[i]>>ladder_Det.end_Grid[i];
cin>>cur_Pos.row>>cur_Pos.col;
cin>>r.num;
for(i=0;i < r.num;i++)   cin>>r.roll[i];

Output

OUTPUT :

cout<<final_Pos;

Processing

for(i=0;i < r.num;i++)
    {
        pos+=r.roll[i];
        for(j=0;j < snake_Det.num;j++)
        if(pos==snake_Det.st_Grid[j])
        {
            pos=snake_Det.end_Grid[j];
            break;
        }
        for(j=0;j < ladder_Det.num;j++)
        if(pos==ladder_Det.st_Grid[j])
        {
            pos=ladder_Det.end_Grid[j];
            break;
        }
    }

Pseudocode

BEGIN

Read input
for(i=0;i < r.num;i++)
    {
        pos+=r.roll[i];
        for(j=0;j < snake_Det.num;j++)
        if(pos==snake_Det.st_Grid[j])
        {
            pos=snake_Det.end_Grid[j];
            break;
        }
        for(j=0;j < ladder_Det.num;j++)
        if(pos==ladder_Det.st_Grid[j])
        {
            pos=ladder_Det.end_Grid[j];
            break;
        }
    }
return(pos)
cout<<final_Pos;

END
Share on Google Plus

About Unknown

This blog is exclusively for VITIAN'S TO fulfill their needs. Also you can tell our team your need and we will assist you as soon as possible. For any quaries send your emails to "everything4vitians@gmail.com". Thank you
    Blogger Comment

0 comments:

Post a Comment