0% found this document useful (0 votes)
130 views

Programming Fundamentals (CS 1002) Fall 2021 Assignment # 2: Due Date

The document provides instructions for Assignment 2 of the Programming Fundamentals course. It outlines 10 problems to solve involving programming concepts like functions, decision control, bitwise operations, and random number generation. It also provides details on file submission format, plagiarism policy, marking criteria, and the deadline of November 14, 2021.

Uploaded by

Hassan Farid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Programming Fundamentals (CS 1002) Fall 2021 Assignment # 2: Due Date

The document provides instructions for Assignment 2 of the Programming Fundamentals course. It outlines 10 problems to solve involving programming concepts like functions, decision control, bitwise operations, and random number generation. It also provides details on file submission format, plagiarism policy, marking criteria, and the deadline of November 14, 2021.

Uploaded by

Hassan Farid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Fundamentals (CS 1002)

FALL 2021 ASSIGNMENT # 2


Due Date: Sunday, November 14, 2021 (05:00 pm)

Instructions
Please follow the following submission instructions. Failure to submit according to the above
format would result in deduction of 10% marks. Submissions on the email will not be
accepted.

• Combine all your work (solution folder) in one .zip file. Use proper naming convention
for your submission file. Name the .zip file as SECTION_ROLL-NUM_02.zip (e.g.
A_21i0412_02.zip). Your zip file should only contain .cpp files, each file should
correspond to its question/problem number. Submit .zip file on Google Classroom
within the deadline.

Plagiarism: Plagiarism cases will be strictly dealt with. If found plagiarized, both the involved
parties will be awarded zero marks in this assignment, all of the remaining assignments, or
even an F grade in the course. Copying from the internet is the easiest way to get caught!

Deadline: The deadline to submit the assignment is Sunday, November 14, 2021 (05:00 pm).
Late submission with marks deduction will be accepted. Correct and timely submission of the
assignment is the rresponsibility of every student; hence no relaxation will be given to anyone.

Marking criteria: Your submitted programs will be marked on the following criteria.
Functional requirements 50%
Good user interface (user friendly instructions, layout, presentation) 20%
Proper source code indentation 10%
Programming conventions followed (e.g. variable names, ) 20%

Note: Start early so that you can finish it on time

Programming Fundamentals – Assignment 2 (Fall 2021)


Problem 1: A country has Currency named TIKKA and Currency Notes of amount 750, 350, 200, 85,
750, 450, 350, 75, 18 and 1
15, 3 and 1 TIKKA’s. Write a C++ program which:
• accepts amount in TIKKA’s as input (integer).
• Pass the user entered TIKKAs to a function which displays total number of Currency Notes of
TIKKA 750, 450, 350, 75, 18 and 1.
For example: when user enter a number 1964, the results would be like this.
Currency Note : Number
750 :2
450 :0
350 :1
75 :0
18 :2
1 :3

Problem 2: Write a C++ program that takes up to 10-digit integer input from user (can be 1 digit, 2
digit, and so on..); passes this to a function which reverses the digits. Finally, the reversed number
should be displayed in the main function. For example: when user enters 10-digit like 1234567890
your function will reverse it to 987654321. Be careful for big integer values. [use functions, decision
control]

Problem 3: Read about doing multiplication using Karatsuba method


(https://en.wikipedia.org/wiki/Karatsuba_algorithm ). Write a C++ program which:

• Prompts user to enter two large integer numbers (x, y). Both numbers must be more
than 4 digits and less than 10 digits (do input validation, don’t use strings). The entered
numbers could be negative or positive. For example: 820778 and -58712979 where x is
6-digit positive number and y is 8-digit negative number.
• Make a function called Karatsuba() which takes these two integers and returns the
multiplication result using Karatsuba algorithm. You might need to make another helper
function called getDigits() which will get an integer as input and returns number of digits
of that integer. getDigits() will help you find the value of m and Bm.
• Display result in the main function.

Problem 4: The information about colours is to be stored in bits of a variable called colour. The bit
number 0 to 7, each represent 8 colours of a rainbow, i.e. bit 1 represents Blue, 2 represents Green,
and so on (see table below). Write a C++ program that asks the user to enter a number and based on
this number, an eight lined rainbow (of asterisks *) is to be displayed, such that, if the bit is ON,
respective colour is displayed otherwise black line is drawn (not visible).

Programming Fundamentals – Assignment 2 (Fall 2021)


Bit 1 2 3 4 5 6 7 8
Color Blue Green Cyan Red Purple Yellow White Gray
How you will set text color:

More info: http://www.cplusplus.com/forum/beginner/54360/

How you will check whether a bit is ON or OFF:

You will need bit masking, that is, if you want to check whether most significant bit is ON or OFF, you
will have to do bitwise AND as follows:

Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7


Position 128 64 32 16 8 4 2 1
Number 1 0 1 1 1 0 0 1
(185)
Bitwise AND Operation
Masking 1 0 0 0 0 0 0 0
No.
(128)
Result
Bitwise 1 0 0 0 0 0 0 0
AND result
(128)

See help material: Google Class Room -> All PF Course Material -> Reference Material -> Bit Level
Operations.html

Programming Fundamentals – Assignment 2 (Fall 2021)


How to print binary bitstream of an integer:

Sample Program Run:

Problem 5: In a Military database system, IDs of the army personnel are stored in a 32-bit value
which is coded as follows:

a. 7-bits Belt number


b. 10-bits Batch number
c. 5-bits Log number
d. 10-bits Unit number

And stored in following format:

Programming Fundamentals – Assignment 2 (Fall 2021)


Your Task is to write a C++ Program which inputs a four-byte integer ID, and a string Name of the
army man. Your Program will separate the Belt number, Batch number, Log number and Unit
number and prints the information in the following manner.

Enter Name of Army Man: Abdullah Khan


Enter ID of Army Man: 858993459

Belt number of Abdullah Khan is : 51


Batch number of Abdullah Khan is: 614
Log number of Abdullah Khan is: 25
Unit number of Abdullah Khan is: 204

Problem 6: Write a C++ program to perform 128-bit encryption and decryption using XOR operation.
Your program must ask for 128-bit key and plain text then perform encryption and decryption.

Problem 7: In a computing system (FAST-MAC), a machine level instruction is represented in a 16-bit


value, which is further divided into three subparts.

a. 4-bit Operation code (Most Significant bits)


b. 6-bit Left Operand (Middle bits)
c. 6-bit Right Operand (Least significant bits)
And stored in following format:

4-bit opcode 6-bit Left Operand 6-bit Right Operand

Four-bit operation code (opcode) of FAST-MAC can define following 16 operations:


Decimal Code Binary Code Operation Description
0 0000 Display Value of Right Operand
1 0001 Display Value of Left Operand
2 0010 Take (6-bit) Random input in Right operand
3 0011 Take (6-bit) Random input in Left operand
4 0100 Add Left and Right operand
5 0101 Calculate Subtraction of operands (Subtract Smaller from Larger)
6 0110 Multiply Left and Right operand

Programming Fundamentals – Assignment 2 (Fall 2021)


7 0111 Calculate division of operands (Divide Larger from smaller)
8 1000 Calculate remainder operands (Larger from smaller)
9 1001 Bitwise OR of Left and Right operand
10 1010 Bitwise AND of Left and Right operand
11 1011 Bitwise XOR of Left and Right operand
12 1100 Complement of Right
13 1101 Complement of Left
14 1110 Left Operand << Right Operand
15 1111 Left Operand >> Right Operand
Write complete C++ program that implements FAST-MAC using appropriate data types and operators.
Your program should get a two-byte input from user and then perform the operation as per FAST-
MAC. The output to show what operation was performed and on what operand values. The result of
the operation should also be displayed.

Problem 8: Write a lottery game application that will generate three random numbers each between
0 and 9. The user should guess three numbers and the program should compare each of the user's
guess to the three random and display an appropriate output based on whether they got:
• any one matching
• two matching
• three matching, not in order
• three matching in exact order
• or no matches at all

Problem 9: A library charges a fine for every book returned late. For first 5 days the fine is 50 rupees,
for 6-10 days fine is 100 rupees and above 10 days fine is 150 rupees. If you return the book after 30
days your membership will be cancelled. Write a program to accept the number of days the member
is late to return the book and display the fine or the appropriate message.

Problem 10: Any character is entered through the keyboard, write a program to determine whether
the character entered is a capital letter, a small case letter, a digit or a special symbol.

Problem 11: The colors red, yellow and blue are known as the primary colors because they cannot be
made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown
here:
o When you mix red and blue, you get purple.
o When you mix red and yellow, you get orange.
o When you mix blue and yellow, you get green.
Write a program that prompts the user to enter the first letter of names of two primary colors to mix.
If the user enters anything other than “r,” “b,” or “y,” the program should display an error message.
Otherwise, the program should change textcolor to that color displays the name of the secondary
color that results. Implement it using if\else structure and then switch structure.

Programming Fundamentals – Assignment 2 (Fall 2021)


Problem 12: A certain grade of steel is graded according to the following conditions:
• Hardness must be greater than 50
• Carbon content must be less than 0.7
• Tensile strength must be greater than 5600
The grades are as follows:
a. Grade is 10 if all three conditions are met
b. Grade is 9 if conditions (i) and (ii) are met
c. Grade is 8 if conditions (ii) and (iii) are met
d. Grade is 7 if conditions (i) and (iii) are met
e. Grade is 6 if only one condition is met
f. Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and tensile
strength of the steel under consideration and output the grade of the steel.

Problem 13: Write a code that takes two integers as input representing a month and day and prints
the season for that month and day. Assume that months are specified as an integer between 1 and 12
(1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and
31. If the date falls between 16/12 and 15/3, you should print "Winter". If the date falls between 16/3
and 15/6, you should print "Spring". If the date falls between 16/6 and 15/9, you should print
"Summer". And if the date falls between 16/9 and 15/12, you should print "Fall".

Problem 14: Write a program that displays the following menu:


Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
a. If the user enters 1, the program should ask for the radius of the circle and then display its
area. Use the following formula: area = πr2 Use 3.14159 for π and the radius of the circle for
r.
b. If the user enters 2, the program should ask for the length and width of the rectangle and
then display the rectangle’s area. Use the following formula: area = length * width
c. If the user enters 3 the program should ask for the length of the triangle’s base and its
height, and then display its area. Use the following formula: area = base * height * .5
d. If the user enters 4, the program should end.
Input Validation: Display an error message if the user enters a number outside the range of 1
through 4 when selecting an item from the menu. Do not accept negative values for the circle’s
radius, the rectangle’s length or width, or the triangle’s base or height.
Program Design: Use Switch statement for menu. Use functions to calculate areas. Inputs should not
be taken in the functions.

Programming Fundamentals – Assignment 2 (Fall 2021)


Problem 15: Time Calculator: Design a program that asks the user to enter number of seconds, and
convert and display equivalent days/hours/minutes/seconds as follows:
a. There are 60 seconds in a minute. If the number of seconds entered by the user is
greater than or equal to 60, the program should display the number of minutes in
that many seconds.
b. There are 3,600 seconds in an hour. If the number of seconds entered by the user is
greater than or equal to 3,600, the program should display the number of hours in
that many seconds.
c. There are 86,400 seconds in a day. If the number of seconds entered by the user is
greater than or equal to 86,400, the program should display the number of days in
that many seconds.

Happy coding: Start you coding now

Programming Fundamentals – Assignment 2 (Fall 2021)

You might also like