Starting from:

$39.99

COMP2113 Assignment 2 Solution

If you have any questions, please post to the Moodle discussion forum on Assignment 1.
• General Instructions
• Problem 1: (C++) Pokémon in Order (20 marks)
• Problem 2: (C++) Sudoku (25 marks)
• Problem 3: (C++) Morse Code Decoder (25 marks)
• Problem 4: (C) Luhn algorithm (25 marks)
Total marks: 100 marks
• 5 marks for proper code comments, indentation and use of functions
• 95 marks for program correctness
A maximum of 5 marks will be deducted if you fail to follow the submission instructions strictly.

General Instructions
Read the instructions in this document carefully.
In this assignment you will solve 4 problems and a tester would automatically test your submitted program. So if your submitted files and program outputs do not conform to our instructions given here, your programs cannot be evaluated and you will risk losing marks totally.
We will also use additional test cases when marking your submission.
Input and output format
How to use the sample test cases

Testing against the sample test cases is important to avoid making formatting mistakes. The additional test cases for grading your work will be of the same formats as the sample test cases.
Coding environment
You must make sure that your program can compile, execute and generate the required outputs on our standard environment, namely, the gcc C/C++11 environment we have on the CS Linux servers (academy*).
For Problems 1, 2 and 3 on C++ programming, make sure the following compilation command is used to compile your programs:

For Problem 4 on C programming, make sure the following compilation command is used to compile your program:

As a programmer/developer, you should always ensure that your code can work perfectly as expected on a target (e.g., your client's) environment, not only on yours.
Submission

Late submission
Evaluation
Academic dishonesty
Getting help
You are not alone! If you find yourself stuck on something, post your question to the course forum. We want this assignment to be rewarding and instructional, not frustrating and demoralizing. But we don’t know when or how to help unless you ask.
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments to the discussion forums or share your work to any public domain. However you are welcome and encouraged to discuss general ideas on the discussion forums.

Problem 1: (C++) Pokémon in Order
Write a C++ program which reads in some Pokémon names from user input and outputs them in a way as described below.
Input:
• Each line contains a single word which is the name of a Pokémon.
• Last line is always "???" indicating end of input.
Output:
• Names of the input Pokémon, one on each line, in the following order: ◦ In descending order of length of Pokémon name.
◦ If two Pokémon names are of the same length, then the two names will be printed in lexicographical order (case insensitive).
Requirement:
• Your program MUST implement the selection sort algorithm you learned in Module 6. You will need to adapt the sorting algorithm to satisfy the output requirement. In other words, you are not allowed to use external libraries to handle the sorting.
• You can ONLY use the data types char , bool , int , double , strings and arrays.
• You are NOT allowed to use data structures from other external libraries such as STL containers (e.g., vectors), etc.
Sample Test Cases
User inputs are shown in blue.
1_1

1_2

1_3


Problem 2: Sudoku
Sudoku is a puzzle game whose goal is to enter the digits 1 to 9 into a game board with 9 x 9
cells, such that:
• Each digit appears exactly once in each row
• Each digit appears exactly once in each column
• Each digit appears exactly once in each of the nine 3 x 3 sub-grid.
An example game solution is shown below.


A very basic Sudoku solving technique, named Naked Single, is to determine the allowed digits of an empty cell and if the empty cell has only one allowed digits, we fill the empty cell with this allowed digit. In the above game board, the cell at position (4, 5) can be filled with the digit 5.
so we will need to redetermine them.
• We can apply the Naked Single technique to the empty cells repeatedly, until we find that no empty cells can be filled after visiting all empty cells once.
Write a C++ program that uses the Naked Single technique alone to solve a Sudoku game as much as possible as described above.
You are provided with a template program 2.cpp .
Input:
• Nine lines, each containing nine digits from 0 to 9, representing a Sudoku game board.
• A digit 0 represents an empty cell, while any digit from 1 to 9 represent a filled
cell.
Output:
• Your program should first display the given game board in the format as specified in the sample test cases, in which an empty cell is printed as x , and we have grid lines to better visualize the sub-grids.
Requirement:
• You should start working from the 2.cpp provided.
• The main() function has been written for you. A 2D array is also defined in main() for storing the game board. You do not need to change anything in the main() function.
• Complete the following functions in 2.cpp . Read the code in the template
carefully to see what have been provided for you. You will find details of the function prototypes in the in-code comments too.
◦ ReadBoard() which reads a Sudoku board from input
◦ PrintBoard() which displays a given Sudoku board to screen
◦ SolveBoard() which solves a given Sudoku board as much as possible using the Naked Single technique only.
• You can ONLY use the simple data types char , bool , int , double , strings and arrays. In other words, you are not allowed to use other data types or data structures STL containers (e.g., vectors), etc.
Sample Test Cases
User inputs are shown in blue.
2_1:

2_2:

2_3:


Problem 3: (C++) Morse Code Decoder
Morse code is a method used in telecommunication that encodes text characters with dot and dash . The following figure shows the encodings of the characters supported by our

In this problem, you are asked to write a C++ program that takes in a sequence of morse code and decodes it to text characters. The input morse code in this problem follows the rules:
• A dot is represented using . .
• A dash is represented using _ .
• The space between letters is represented using | .
• The space between words is represented using || .
For example, given the input morse code:

The decoder should output:

Given the input morse code:

The decoder should output:

Input:
• The program accepts morse code from a file. The command line of the program is given by:
<program_name> < filename
E.g., if your program is named decoder , and the morse code is stored in a file named "message.txt"; then, the following command at the command prompt
./decoder < message.txt will decode the morse code in "message.txt" and print out the text.
Output:
• Your program will print out the decoded text in one line.
Requirements:
• You should start working from the 3.cpp provided.
• Complete the function string morseCodeToText(string s)
which
◦ takes the morse code as input.
◦ returns the decoded text.
Note:
• The main() function has been completed for you, and you do not need to make any change to it.
modularity.
Sample Test Cases
User inputs are shown in blue.
3_1

3_2

3_3

3_4


Problem 4: (C) Luhn algorithm
The Luhn algorithm, also known as the "modulus 10" algorithm, is a simple checksum method to prevent simple transcription errors for a sequence of digits. It is commonly used in distinguishing valid credit card numbers. Given an input code as a string of digits, the algorithm works as follows:
1. Reverse the input string.
2. Sum the odd digits (i.e., first, third,... digits) in the reversed string to obtain a partial sum s1.
3. For every even digits (i.e., second, fourth, ... digits) in the reversed string, multiply the digit by 2
4. Obtain a partial sum s2 of the products in (3) with this rule: If a product in (3) is less than 10, just add the product to s2; otherwise, add the sum of two digits in the product to s2.
5. If s1 + s2 is divisible by 10, then the input string of digits is a valid code.
Let's work out an example. Suppose the input string is 5255638118968609:
1. we first obtain the reversed string 9068698118365525
2. summing the odd digits, we have s1 = 9+6+6+8+1+3+5+2=40
3. the multiples of the even digits are 0, 16, 18, 2, 16, 12, 10, 10
4. summing the multiples of the even digits, we have s2 = 0 + (1+6) + (1+8) + 2 + (1+6) + (1+2) + (1+0) + (1+0) = 30
5. since s1 + s2 = 70 which is divisible by 10, the input string is a valid code.
Write a C program to determine if an input string of digits is a valid code.
Input:
• An input string of digits.
Output:
• the first line displays the reversed string in step 1.
• the second line displays the values s1 and s2, separated by a space.
• the third line displays the string "valid" if the input is a valid string, or "invalid" if otherwise.
Requirement:
• Use the command gcc -pedantic-errors -std=c11 4.c -o 4 to compile your program. (Or specify -pedantic-errors -std=c11 as the compiler flags in the Atom editor.)
Sample Test Cases
User inputs are shown in blue.
4_1

4_2

More products