Вы находитесь на странице: 1из 11

BACHELOR OF COMPUTER SCIENCE

SECOND YEAR SEMESTER TWO


(BCS II – I)

BSC 201:
PROGRAMMING IN C++

JUNE 2020 EXAMINATIONS

TIME ALLOWED: THREE HOURS

INSTRUCTION TO CANDIDATE:

1. This paper contains THREE sections.


Attempt ONE Questions from each section

2. Number each answer clearly in the left margin.

3. Start each answer on a new page.

4. Total marks: 60%


SECTION A: Answer one Question from in this section.

QUESTION ONE:

A Vimachitika car rental company needs to keep extensive records to run their operation
correctly. Not only does a company need to keep track of their customers, but it is also
imperative to keep good records on employees. Keeping track of what car, a customer is
driving is a good example of a record. In addition, many times, cars need to be transported
between locations of a company. If a staff member is moving a car, it is important to know
which staff members are moving certain cars.
Suppose a NIPA wanted to create a car rental company called Vimachitika rentals. This
company has employed you to keep records for the company. The company is confident you
will succeed in creating a large-scale record system, but they would like you to prove your
coding skills first. Vimachitika rentals would like you to set up an initial program to hold
records before you begin the full-scale project.
You are tasked to create 2 ADTs: customers and staff. Each customer will have a first and
last name, an indication if the customer is enrolled in the “preferred” program, the car they
are driving, and the amount of money owed to the company. On the other hand, each staff
member will have a first and last name, the car they are driving, and their salary. In a real-
world problem, there would obviously be more records associated with each staff member
and customer.? Vimachitika rentals would like you to automatically process txt files and print
the information to the console screen. When you perform this process, ensure that your
member variables are private. In addition, be sure to use getter and setter functions to
manipulate and print your data.

[Total=20 marks]
QUESTION TWO:

#include <iostream>
using namespace std;
template <class any_data_type>
any_data_type MyMax(any_data_type Var1, any_data_type Var2)
{
return Var1> Var2 ? Var1:Var2;
}
int main()
{
cout<<"MyMax(10,20) = "<<MyMax(10,20)<<endl;
cout<<"MyMax('Z','p') = "<<MyMax('Z','p')<<endl;
cout<<"MyMax(1.234,2.345) = "<<MyMax(1.234,2.345)<<endl;
cout<<"\n Logical error, comparing pointers not the string..."<<endl;
char* p = "Function";
char* q = Template;
cout<<"Address of *p = "<<&p<<endl;
cout<<"Address of *q = "<<q<<endl;
cout<<"MyMax(\"Function\",\"Template\") = "<<MyMax(,q)<<endl;
cout<<"Should use Specialization, shown later..."<<end;
return 0;
}

a) For each of the highlighted lines, provide a c programming comment and explain the
significance of each line.
(6 marks)
b) Write the output for above program after correcting the syntax error. (14 marks)

[Total=20 marks]
SECTION B: Answer one Question from in this section

QUESTION THREE

You will create a C program that prints a menu and takes user choices as input. The user will
make choices regarding different "geometric shapes" that will be printed to the screen.
Your code must contain at least one of all of the following control types:
❖ nested for () loops
❖ a while () or a do-while () loop
❖ a switch () statement
❖ an if-else statement
❖ functions (see below)
Instructions

A prompt is presented to the user to enter a choice from the menu. If the user enters a choice
that is not a valid input, a message stating the choice is invalid is displayed and the menu is
displayed again.

Menu Choice Valid User Input Choices


Enter/Change Character 'C' or 'c'
Enter/Change Number 'N' or 'n'
Draw Line 'L' or 'l'
Draw Square 'S' or 's'
Draw Rectangle 'R' or 'r'
Draw Triangle (Left Justified) 'T' or 't'
Quit Program 'Q' or 'q'
Your program must have at least five functions (not including main ()) including:
• A function that prints the menu of choices for the user, prompts the user to enter a choice and
retrieves that choice. The return value of this function must be void. The function will have
one pass-by-reference parameter of type char. On the function's return, the parameter will
contain the user's menu choice.
• A function that prompts the user to enter a single character. The return value of the function
be a char and will return the character value entered by the user. This return value will be
stored in a local variable, C, in main (). The initial default value of this character will be ' '
(blank or space character).
• A function that prompts the user to enter a numerical value between 1 and 15 (inclusive). If
the user enters a value outside this range, the user is prompted to re-enter a value until a
proper value is entered. The return value of the function be an int and will return the value
entered by the user. This return value will be stored in a local variable, N, in main(). The
initial default value of this character will be 0.
• A function for each geometric shape. Each function will take the previously entered integer
value N and character value C as input parameters (You will need to ensure that these values
are valid before entering these functions). The return values of these functions will be void.
The functions will print the respective geometric shape of N lines containing the input
character C. N is considered the height of the shape. For a line, it is just printing the character
in C, N number of times, so that it creates a vertically standing line of length N. For N = 6
and C = '*', the draw line function should output:
*
*
*
*
*
*
If a square is to be printed, then the following output is expected:
******
******
******
******
******
******

In case of a rectangle, we assume its width is N+5. It should look like the following:
***********
***********
***********
***********
***********
***********

If the user selects Triangle, then it should print a left justified triange which looks like the
following:

*
**
***
****
*****
******
[Total=20 marks]
QUESTION FOUR

(i) Create a class named Shape with a function that prints "This is a shape". Create another
class named Polygon inheriting the Shape class with the same function that prints "Polygon is
a shape". Create two other classes named Rectangle and Triangle having the same function
which prints "Rectangle is a polygon" and "Triangle is a polygon" respectively. Again, make
another class named Square having the same function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes. (10 marks)

(ii) A boy has his money deposited K2000, K5500 and K6000 in Banks-Bank X, Bank Y and
Bank Z respectively. We have to print the money deposited by him in a particular bank.
Create a class 'Bank' with a function 'getBalance' which returns 0. Make its three subclasses
named 'BankX', 'BankY' and 'BankX' with a function with the same name 'getBalance' which
returns the amount deposited in that particular bank. Call the function 'getBalance' by the
object of each of the three banks. (10 marks)
SECTION C: Answer one Question from in this section

QUESTION FOUR

Introduction: Ninja Academy


Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they
would like to become a ninja. To amuse your friend, you have decided to create a series of
programs about ninjas.
Problem: Stolen! (ninjamission.opp)
The Ninja Academy has been raided by a rival ninja school! Some of the Academy’s most
valuable relics have been stolen:
The founder’s KATANA
A leather scroll CASE
An ornate SCROLL
A velvet POUCH
A JADE statue
An ancient MASK
The Ninja Academy is nominating its best students to go on a mission to retrieve these items.
Each you will take turns trying to steal back a relic, simulated by a randomly generated
number from 0 to 6. Six of those numbers (0-5) correspond to the numbers above, while 6
corresponds to getting caught. Getting caught by the rival school’s guards will force you to
give back a relic you’ve already retrieved.
If you get a number that corresponds to an item you already have, nothing happens. If you get
a SCROLL (item 2), you may only pick up the item if you already have a CASE (item 1). If
you get a JADE (item 4) or a MASK (item 5), you may only pick up the item if you already
have a POUCH (item 3) to put them in. This is because you cannot carry the scroll with a
scroll case and you cannot carry the jade stature or the mask with a velvet pouch to keep them
from getting damaged.
If you land on “You’ve been caught!”, then you must choose which relic to discard. If you
have no items to begin with, then you don’t lose anything. If you have a SCROLL (2), then
you cannot discard the CASE (1) and if you have a JADE (4) or a MASK (5), then you
cannot discard the POUCH (3).
The first person to retrieve all six items will be a hero of the Academy!
Program Specification
You must use a 2D array to solve the problem.
There will be no more than 6 ninja trying to complete the mission.
Sample Data Items
You may wish to use these data structures to help you solve the problem. Type them into
Code::Blocks (do not copy/paste).
int cont = 1;
int ninjas[6][6];
char ITEMS[6][20] = {"KATANA", "CASE", "SCROLL", "POUCH", "JADE", "MASK"};
The values in “ITEMS” are strings and may be printed with %s. For example, to print
“CASE” we could use printf(“%s”, ITEMS[1]);
Program Format
Beginning the program
Start by asking how many ninjas will be sent on the mission.
Beginning of Each Turn
Each turn should begin with a prompt with the following format:
Ninja X, it is your turn. Enter 0 to look for an item.
where X is the number given to the current ninja, starting at 1. (Note: Internally, the player
numbers will start at 0, so when you print this out, just add 1 to the variable storing the player
number.)
Finding an Item
After the random number is generated, check what it corresponds to. If a value other than 6
(“You’ve been caught!”) is generated, a statement of the following form should be printed:
Ninja X, you found a S.
where S is the name of the item the number matches.
If the player does not yet have the item and is allowed to obtain it, follow this with a
statement of the form:
You now have a S!
If the player is not allowed to obtain the item because they don’t yet have another necessary
item, print out a statement of the form:
Sorry, you can’t obtain a S.
If the player lands on a square for a piece they already have, then print the following:
You already have that item.
Landing on “You’ve been caught!”
If the player gets this number they must choose a piece to lose. First print the warning:
You’ve been caught!
If the player has no pieces to lose, then print the following:
There is no equipment for you to lose. Lucky you, sort of.
If they have any items, prompt them with the question:
Which piece would you like to lose?
Follow this with a numbered list of items that are valid for the player to discard. (Namely,
this list will NOT contain the case if the rider has a scroll and it will not contain the pouch if
the rider has a mask or a jade) The numbering of the list should correspond to the numbering
of each item listed on page 1. Thus, the numbering won’t necessarily be sequential. For
example, if the possible pieces a player can lose are the katana, case, scroll, and pouch, the
print out would appear as follows:
0. KATANA
2. SCROLL
3. POUCH
You may assume the user will select a valid item. Then, print out a message of the following
format:
You have selected the S to discard.
where S is the item to discard.
Final Printout for Each Turn
At the end of each person’s turn, print out a complete list of their items. The first line will be:
Ninja X, here is what you currently have:
Follow this with a blank line and then a numbered list (as previously described), with each
item the player currently has.
End of the Game
The game ends when one person has all the items. If you are using the sample data structure
cont, then you can use this as a flag for continuing the game or not. The default is 1 for
continuing the game. When a person has all the items, change the flag to 0.
At the end of the game, simply print a message with the following format:
Congratulations Ninja X, you are the first to return with all of the stolen items!
Deliverables

Write a c++ programming to solve the above the above scenario


Grading Details
Your programs will be graded upon the following criteria:
1) Your correctness
2) Your programming style and use of white space. Even if you have a plan and your
program works perfectly, if your programming style is poor or your use of white space is
poor, you could get 10% or 15% deducted from your grade. [20 Marks]

[Total=20 marks]
QUESTION SIX

Write a C program to input basic salary of an employee and calculate gross salary according
to given conditions.

Basic Salary <= K1000 : HRA = 25%, DA = 80%


Basic Salary is between K1001 to 2000 : HRA = 30%, DA = 90%
Basic Salary >= K 2001 : HRA = 35%, DA = 95%

How to calculate gross salary of an employee using if else in C++ programming. Write a
Program to calculate gross salary of an employee using if else-if, Constructor and
Destructor concepts in C++ program.

Example
Input
Input basic salary of an employee: 2200

Output

Gross salary = K 5,060

[Total=20 marks]

*****END*****

Вам также может понравиться