首页 > > 详细

讲解COMP2113、C++语言程序辅导

COMP2113 Programming Technologies / ENGG1340 Computer Programming II
Assignment 2
Deadline: 17 November 2022 (Thursday) 23:59


If you have any questions, please post to the Moodle discussion forum on Assignment 2.

Total marks: 100 marks
5 marks for proper code comments and indentation
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 tasks 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.

Sample test cases are provided with each task in this document. Note that the test cases may or may not cover
all boundary cases for the problem. It is also part of the assessment whether you are able to design proper test
cases to verify the correctness of your program. We will also use additional test cases when marking your
assignment submission.

Input and output format

Your C++ programs should read from the standard input. Also, your answer should be printed through
the standard output. If you failed to follow this guide, the tester may not able to give a score for your program.
Additionally, you should strictly follow the sample output format (including space, line breaker, etc.), otherwise,
your answer might be considered as wrong.

How to use the sample test cases for problems 1 and 2?
For problems 1 and 2, sample test cases in text file formats are made available for you to check against your
work. Here's how you may use the sample test cases. Take Question 2 test case 2 as an example. The sample
input and the expected output are given in the files input2_2.txt and output2_2.txt, respectively. Suppose that
your program is named "2", do the followings at the command prompt of the terminal to check if there is any
difference between your output and the expected output.

./2 < input2_2.txt > myoutput.txt
diff myoutput.txt output2_2.txt

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 C++ program can compile, execute and generate the required outputs on our
standard environment, namely, the gcc C++11 environment we have on the CS Linux servers (academy*).
Make sure the following compilation command is used to compile your programs:

g++ -pedantic-errors -std=c++11 [program_name].cpp -o [object_code_name]

The name after the “-o” option is the name of the object code. In the above example, you can run the program
using the following command:

./[object_code_name]

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.

While you may develop your work on your own environment, you should always try your program (compile &
execute & check results) on our standard environment before submission.

Submission
Name your C++ program files (and Makefile for Problem 3) as the following table shows and put them together
into one directory. Make sure that the folder contains only these source files (*.cpp / *h / Makefile) and no other
files. Compress this directory as [uid].zip file where [uid] is your university number and check carefully
that the correct file have been submitted. We suggest you to download your submitted file from Moodle, extract
them, and check for correctness. You will risk receiving 0 marks for this assignment if you submit incorrect
files. Resubmission after the deadline is not allowed.

Filename Description
1.cpp Problem 1
2.cpp Problem 2
d2boh.cpp, d2boh.h, main.cpp, Makefile Problem 3

Late submission
If you submit n days after the deadline, there will be 20n% mark deduction. The 24 hours right after the
deadline is considered as the 1st day, and so on so forth. In another word, no mark will be given if you submit
after 5 days.

Evaluation
Your code will be auto-graded for technical correctness. In principle, we use test cases to benchmark your
solution, and you may get zero marks for not being able to pass any of the test cases. Normally partial credits
will not be given for incomplete solution, as in many cases the logic of the programs are not complete and an
objective assessment could be difficult. However, your work may still be considered on a case-by-case basis
during the rebuttal stage.

Academic dishonesty
We will be checking your code against other submissions in the class and from the Internet for logical
redundancy. Please be reminded that no matter whether it is providing your work to others, assisting others to
copy, or copying others will all be considered as committing plagiarism and we will follow the departmental
policy to handle such cases. Please refer to the course information notes for details.

Getting help
You are not alone! If you find yourself stuck on something, post your questions to the course forum, send us
emails or come to our support sessions. 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.

Discussion forum
Please be careful not to post spoilers. Please don’t post any code that is directly related to the assignments.
However, you are welcome and encouraged to discuss general ideas on the discussion forums. If you have any
questions about this assignment you should post them in the discussion forums.

Problem 1 [30%]:
The completed program will read the dimension dim and the elements of a dim x dim matrix, and then display
the elements of its inner dim-2 x dim-2 matrix rotated clockwise through 90 degrees.

You should start working from the 1.cpp provided. The main() function has been completed for you and your
task is to write definitions of functions read_matrix() and display_inner_cw_rotated() to satisfy the
following specifications:

read_matrix(): reads dim, an integer supplied by the user, into the variable passed as the second
argument, where dim is guaranteed to be in {3, 4, ...,10}. It then reads values of elements of
a dim x dim square matrix of integers, each separated by whitespace. The values are supplied by the
user row by row and are read into the 2D array passed as the first argument. The size of the array
is MAXDIM x MAXDIM, where MAXDIM is a global constant and thus is available for use in your function
definitions. MAXDIM is initialized with a value of 10 and, therefore, the array is large enough to hold the
maximum number of values that the user will supply.

display_inner_cw_rotated(): takes as a first argument a 2D array containing a square matrix and,
as a second, the dimension, dim, of that matrix. It displays the values of a dim-2 x dim-2 matrix formed
by deleting the outermost rows and columns of the original matrix (that is, those that form its outer
perimeter) and then rotating that reduced matrix clockwise through ninety degrees. The elements of the
resulting matrix are displayed row by row with a trailing space after each element (check carefully
your outputs against the given sample outputs using diff).

Sample Test Cases
User inputs are shown in blue.

1_1:
3
11 12 13
21 22 23
31 32 33
22

1_2:
4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
32 22
33 23

1_3:
5
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
42 32 22
43 33 23
44 34 24

Problem 2 [30%]:
Write a C++ program that searches a word from multiple files, and output the number of lines that the word
appears and the total number of occurrences of the word in each file.
You are provided with a template program 2.cpp.

Input:
? The program accepts command line argument as input. The command line of the program is given
by:
E.g., if you compile your program using g++ -pedantic-errors -std=c++11 2.cpp -o 2, then the
following command at the command prompt
./2 abc t1.txt t2.txt t3.txt
will ask your program ./2 to search for the string abc in the files t1.txt, t2.txt, t3.txt.
Command line arguments in C/C++ are implemented by having input paramters for the main() function:
int main(int argc, char* argv[])
Read the comments in the provided template to see the usage of the paramaters argc (an integer)
and argv (an array of C-strings).
The main() body has been completed for you, which means that command line argument parsing has
been done for you. But you should read the code and understand how it works, as you may need to
deal with command line arguments in your later courses too.

Output:
A line for each file specified in the command line arguments: first the filename, then the number of lines
the word appears in the file (n1), followed by the number of total occurrences of the word in the file (n2):
:
or if there is any error accessing the file:
: error opening file

Requirements:
You should start working from the 2.cpp provided.
Complete the function
int SearchWord(string word, string fileName, int &nLines, int &total)
which search for a word in a file named fileName, and stores the number of lines that word appears in
the file in nLines and the number of total occurrences in total. The function returns 0 if file operation
is successful and 1 if otherwise.
The matching of word is case-insensitive.

Note:
The main() function has been completed for you, and you do not need to make any change to it. Read
the code in main() and understand what it does before you start working.
You may add your own functions wherever appropriate for better program modularity.

Sample Test Cases
Sample text files t1.txt, t2.txt, t3.txt, t4.txt are provided.
We show only the contents of t1.txt, t2.txt, t3.txt here:
t1.txt t2.txt t3.txt
Cab AbB CAB Ab CaB ABc aBb AB
cAb caB aBcAB CAB ABcAb AbcAb
aBCAB AB ABcAb aBCaB ABCAB Abb
aBC abC AbC ABCAB abCab ABC
aBCAB ABcAB AB cAb
ab abcab Ab abc aBCAB AbCAb abC
AbC ab abcab aBc aBc ab abcAB
Abc aBC ab abC ab aB AbCAB AbcaB
Ab ab ABc aBcab abC ABc AbCab
ABcAb
app THe Elf DeEd keep SHe DEED
dEEd aPP Ab sheeP Cde THe sHe
ElF CDE sHe KeEP aB eLF An CaT
sHE hE KEeP he SHE dEED ab she

Commands entered by the users at the command line prompt ">" to run your program are shown in blue,
assuming that your program name is wordsearch.

Note that since there is no user input from the standard input, here's how you should test your output against
the sample output (e.g., for test case 3_1) to check for correctness:
./2 abc t1.txt t2.txt t3.txt > myoutput.txt
diff myoutput.txt output2_1.txt

2_1
./2 abc t1.txt t2.txt t3.txt
t1.txt: 2 5
t2.txt: 4 11
t3.txt: 0 0

2_2
./2 ab t1.txt t2.txt t3.txt
t1.txt: 3 4
t2.txt: 4 9
t3.txt: 3 3

2_3
./2 he t3.txt t4.txt t5.txt
t3.txt: 1 2
t4.txt: 3 4
t5.txt: error opening file


Problem 3 [35%]:
In this question, let us implement a simple converter to convert a given integer in the range [1, 100] to its binary,
octal and hexadecimal representation.

To allow other programs to reuse our program code for decimal-to-binary, decimal-to-octal and decimal-to-
hexadecimal conversions, let us put the code in a separate file.

Let us first define the header file “d2boh.h” as follows:
// d2boh.h
#ifndef D2BOH_H
#define D2BOH_H
int decimal_to_binary(int input, int output[10]);
int decimal_to_octal(int input, int output[10]);
int decimal_to_hexadecimal(int input, char output[10]);
#endif

The interfaces of the functions are explained below:

decimal_to_binary:
Parameter 1: An input integer (1 – 100) in decimal form
Parameter 2: An integer array to hold the binary representation of the input integer. For example, if the input
integer is 10, the array will contain {1, 0, 1, 0}.
Return value: An integer to represent the number of digits in parameter 2. Using the above example, the return
value will be 4.

decimal_to_octal:
Parameter 1: An input integer (1 – 100) in decimal form
Parameter 2: An integer array to hold the octal representation of the input integer. For example, if the input
integer is 10, the array will contain {1, 2}.
Return value: An integer to represent the number of digits in parameter 2. Using the above example, the return
value will be 2.

decimal_to_hexadecimal:
Parameter 1: An input integer (1 – 100) in decimal form
Parameter 2: An integer array to hold the hexadecimal representation of the input integer. For example, if the
input integer is 10, the array will contain {A} (one single element).
Return value: An integer to represent the number of digits in parameter 2. Using the above example, the return
value will be 1.

The skeleton of the implementation file “d2boh.cpp” is given below:
// d2boh.cpp
#include
#include "d2boh.h"
using namespace std;
int decimal_to_binary(int input, int output[10]) {
// To be implemented
}
int decimal_to_octal(int input, int output[10]) {
// To be implemented
}
int decimal_to_hexadecimal(int input, char output[10]) {
// To be implemented
}

Requirements
Task 1: Please complete the implementation of the 3 functions.
Task 2: Please implement a main program “main.cpp” which accepts 2 positive integral command line
arguments. The first argument indicates whether we want to do decimal-to-binary, decimal-to-octal or decimal-
to-hexadecimal conversion. The indicators of these 3 conversions are 1, 2 and 3 respectively. The second
argument is an integer that we want to convert. Some sample runs are shown below.

> ./main 1 13
1101
> ./main 2 13
15
> ./main 3 13
D
> ./main 1 36
100100
> ./main 2 36
44
> ./main 3 36
24
> ./main 1 95
1011111
> ./main 2 95
137
> ./main 3 95
5F

You must call the functions
decimal_to_binary(), decimal_to_octal(), decimal_to_hexadecimal()
defined in “d2boh.cpp” in your main program.

Please compile your programs using the command g++ -pedantic-errors -std=c++11 main.cpp
d2boh.cpp -o main. Then run the main program using the command ./main
to see whether it behaves as expected.

Task 3: Please create a Makefile to generate the following targets:
d2boh.o: which depends on d2boh.cpp and d2boh.h
main.o: which depends on main.cpp and d2boh.h
main: which depends on d2boh.o and main.o
clean: for cleaning main, main.o and d2boh.o

You should assume that there can be a file named “clean” in the folder and you need to make sure that
running make clean will not cause any conflict.

Upon Makefile is created appropriately, you should be able to compile all programs by issuing the
command make main and cleaning all object files by issuing the command make clean.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!