COMP2113 Programming Technologies / ENGG1340 Computer Programming II Assignment 1 Deadline: 20 October 2022 (Thursday) 23:59 If you have any questions, please post to the Moodle discussion forum on Assignment 1. 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 3 and 4? 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 4 test case 3 as an example. The sample input and the expected output are given in the files input4_3.txt and output4_3.txt, respectively. Suppose that your program is named "4", do the followings at the command prompt of the terminal to check if there is any difference between your output and the expected output. ./4 < input4_3.txt > myoutput.txt diff myoutput.txt output4_3.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 For Problem 3 and Problem 4, 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 [yourprogram].cpp 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 shell script (for Problem 1 and Problem 2) and C++ programs (for Problem 3 and Problem 4) as the following table shows and put them together into one directory. Make sure that the folder contains only these source files (*.sh / *.cpp) 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.sh Problem 1 2.sh Problem 2 3.cpp Problem 3 4.cpp Problem 4 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 1 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. A. Shell Programming [45%] A certain railway company in Hong Kong uses a signaling system to keep track of trains in its railway system. The system generates a log file (.txt) every day. Each line in the log file represents a train calling at a station and has the following format: [time_HH:MM:SS], [train_id], [station_id], [paltform_no.] Each column is separated by a comma “,” in the log file. The log files have “Trains_” as prefix in their file names, followed by a date in form of YYYY-MM-DD. The records are sorted by time in ascending order Field Description time_HH:MM:SS The time has the format HH:MM:SS train_id Identify a train Train ID starting with “E” represents a passenger train. (e.g E201, E217) Train ID starting with a number represents a maintenance train. (e.g. 8001, 59) station_id Identify a station platform_no. The platform number in that particular station Example: Consider an example Trains_2022-09-01.txt that stores the records on 2022-09-01. 06:01:10,E201,SHS,1 06:06:28,E209,SHS,1 ... 07:10:27,8001,MOK,2 ... From the log file, we know that the E201 passenger train called at SHS station platform No.1 at time 06:01:10. Problem 1 [20%]: Create a shell script 1.sh that performs the following: 1. For each “Trains_” log file, find out the three busiest stations that have the most number of passenger trains calling at. The output should display the count of passenger trains that called at that station and the station ID. 2. If two or more stations have the same count, we output them in alphabetical order of the station ID. Therefore “4 FAN” is put ahead of “4 HHK” under “Trains_2022-09-03.txt” in the example below. If we run 1.sh, the following will output. The sample output can be found in the file output1.txt. Trains_2022-09-01.txt: 25 LMC 23 TWO 22 FAN Trains_2022-09-02.txt: 30 SHS 24 LMC 24 SHT Trains_2022-09-03.txt: 4 FAN 4 HHK 4 KOT Trains_2022-09-04.txt: 17 SHS 11 MOK 9 FAN Problem 2 [25%]: Create a shell script 2.sh that performs the following: 1. The 2.sh takes one command line input argument, which represents the train ID of a train that we would like to trace. 2. The 2.sh generates a file
.txt that contains a list of stations called by the train in all “Trains_” log files, organized by the filenames and the records in the order of Date and Time. 3. If the number of input arguments is not 1, then the error message “Usage: ./Train_trace.sh ” should be output in shell prompt. 4. If there is no train record found, then the log file will be deleted (or no need to generate it). The script should outputs “No records found for train id” in shell prompt. (where id is the train ID inputted by the user). For example, If we run: $ ./2.sh E201 The shell script will generate a file “E201.txt” that contains the following contents: Trains_2022-09-01.txt 06:01:10,E201,SHS,1 06:19:13,E201,FAN,2 06:48:48,E201,FTR,3 … Trains_2022-09-02.txt 06:00:50,E201,SHS,1 06:23:40,E201,HHK,3 … 23:15:24,E201,SHS,1 Trains_2022-09-03.txt Trains_2022-09-04.txt Sample Test Cases: 2_1 $ ./2.sh E201 E201.txt will be generated. Please refer to the files given. 2_2 $ ./2.sh E209 E209.txt will be generated. Please refer to the files given. 2_3 $ ./2.sh E213 E213.txt will be generated. Please refer to the files given. 2_4 $ ./2.sh E229 E229.txt will be generated. Please refer to the files given. 2_5 $ ./2.sh 8001 8001.txt will be generated. Please refer to the files given. B. C++ Programming [50%] Problem 3 [20%]: Write a program 3.cpp to read an integer N and then print all prime numbers in the range [1, N]. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Hint: For each number M in the range [2, N], you have to check whether it is divisible by any number in the range [2, M – 1] in order to determine if it is a prime number. Sample Test Cases: User inputs are shown in blue. 3_1 10 2 3 5 7 3_2 50 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 3_3 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Problem 4 [30%]: Write a C++ program 4.cpp which encrypts and decrypts some input characters using the Caesar Shifting algorithm detailed below. Input: • A line of input s k c1 c2 c3 …, where o s is either the character e for encryption, or the character d for decryption o k is an integer for the number of shifts used in the Caesar shift algorithm (key for encryption or decryption) o c1 c2 c3 … is a sequence of space separated characters, ended by !, to be encrypted or decrypted Output: • The encrypted/decrypted message ended by !. No space between two consecutive characters. Algorithm: To encrypt (decrypt) a letter c (within the alphabet A-Z or a-z) with a shift of k positions: 1. Let x be c’s position in the alphabet (0 based), e.g., position of B is 1 and position of g is 6. 2. For encryption, calculate y = x + k modulo 26; For decryption, calculate y = x − k modulo 26. Here modulo is equivalent to remainder in mathematics. 3. Let w be the letter corresponding to position y in the alphabet. If c is in uppercase, the encrypted (decrypted) letter is w in lowercase; otherwise, the encrypted (decrypted) letter is w in uppercase. A character which is not within the alphabet A-Z or a-z will remain unchanged under encryption or decryption. Example. Given letter B and k = 3, we have x = 1, y = 1 + 3 mod 26 = 4, and w = E. As B is in uppercase, the encrypted letter is e. Requirement: • Implement a function CaesarShift() which takes in a char c and an int k, where c is the character to undergo Caesar Shifting and k is the number of positions (can be negative) to shift, and return the processed char after Caesar Shifting. The returned char is c if c is not within the alphabet range. The prototype of the function is given by: char CaesarShift(char c, int k). • You can ONLY use the simple data types char, bool, int, double. In other words, you are not allowed the use other data types or data structures such as strings, arrays, vectors, etc. Sample Test Cases: User inputs are shown in blue. 4_1 e 1 ! ! 4_2 e 3 a B c D e ! DeFgH! 4_3 d 3 D e F g H ! aBcDe! 4_4 e -1 H e l l o E N G G 1 3 4 0 / C O M P 2 1 1 3 ! gDKKNdmff1340/bnlo2113! 4_5 d 10 n 3 V 3 D 3 N _ M Y N 3 _ S C _ N 3 L E Q Q 3 N _ M Y N 3 ! D3l3t3d_cod3_is_d3bugg3d_cod3! If you face any problems, please feel free to contact us! We are very happy to help you! We wish you enjoy this assignment! ☺ 欢迎咨询51作业君