CS2311编程 写作、C++语言程序

” CS2311编程 写作、C++语言程序CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20211CS2311 Computer Programming2020/21 Semester BDepartment of Computer ScienceCity University of Hong KongAssignment TwoDue Date: 30 April 2021 (Friday) 23:59All questions should be submitted in Canvas. Note that all solutions will be assessed by (1)correctness, (2) programming Styles (including comments), (3) non-redundancy in expressingsolutions, and (4) efficiency, if applicable. In particular, Question 4 will consider the efficiency aspectin marking.This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.Your solutions are forbidden to INCLUDE any library other than iostream, iomanip,cstring, fstream, and/or cmath. Each solution using any function/facility in otherlibraries will receive 0 mark. If possible, not using cstring in your solution.Solution Submission Instruction: You only need to submit the content of .cpp files of your solutions for the five questions. In Canvas, there are 5 submission links, one for each question.o Submit your C++ code for Question 1 to the submission link for Question 1,o Submit your C++ code for Question 2 to the submission link for Question 2,o Submit your C++ code for Question 3 to the submission link for Question 3,o Submit your C++ code for Question 4 to the submission link for Question 4,o Submit your C++ code for Question 5 to the submission link for Question 5.Submission Example.Copy and Paste Your Code developed for Question 1 into Submission for Q1 and then press[submit assignment]. Repeat the procedure for each question. Remember to submit throughdifferent submission links.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 2021Q1. Sum Matrix Game [20%]Sum Matrix is a number placement game.In this game, an n x n matrix is filled with numbers such that (1) the sum of the numbers in each row oreach column is all same, and (2) all the numbers from 1 to n*n should be placed in the matrix. Theprogram should develop the matrix based on the following given rules.1. The program should ensure the input value n as an odd number in the range of 1 to 11. Otherwise,the program should display the following message and terminate: Please enter an oddpositive number in the range of [1-11]. Bye.2. The possible range of values in each cell of the matrix should be a product of the input odd number. E.g., For sum matrix of order 3*3, the cell values should be in the range of (1-9) E.g., For sum matrix of order 5*5, the cell values should be in the range of (1-25)3. In the output, set the width of each column of the matrix to 4 characters and there is one spaceto separate two columns.4. Note that the cells in your matrix may have different values. (There are multiple solutions, andyou only need to generate one possible solution.)Hints: Observe where to place 1 in a particular matrix and the pattern to place 2, 3, 4, and soon. You will observe a general pattern.Example 1Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]2Please enter an odd Positive number in the range of [1-11]. Bye.Example 2Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]3The Sum Matrix for 3 * 3 Order is8 1 6 ——- 153 5 7 ——- 154 9 2 ——- 15—- —- —-15 15 15Example 3Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]5The Sum Matrix for 5 * 5 Order is17 24 1 8 15 ——- 6523 5 7 14 16 ——- 654 6 13 20 22 ——- 6510 12 19 21 3 ——- 6511 18 25 2 9 ——- 65—- —- —- —- —-65 65 65 65 65CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20214Example 4Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]7The Sum Matrix for 7 * 7 Order is30 39 48 1 10 19 28 ——- 17538 47 7 9 18 27 29 ——- 17546 6 8 17 26 35 37 ——- 1755 14 16 25 34 36 45 ——- 17513 15 24 33 42 44 4 ——- 17521 23 32 41 43 3 12 ——- 17522 31 40 49 2 11 20 ——- 175—- —- —- —- —- —- —-175 175 175 175 175 175 175Example 5Enter an odd number (n) For (n*n) matrix [Max Matrix Size: 11*11]9The Sum Matrix for 9 * 9 Order is47 58 69 80 1 12 23 34 45 ——- 36957 68 79 9 11 22 33 44 46 ——- 36967 78 8 10 21 32 43 54 56 ——- 36977 7 18 20 31 42 53 55 66 ——- 3696 17 19 30 41 52 63 65 76 ——- 36916 27 29 40 51 62 64 75 5 ——- 36926 28 39 50 61 72 74 4 15 ——- 36936 38 49 60 71 73 3 14 25 ——- 36937 48 59 70 81 2 13 24 35 ——- 369—- —- —- —- —- —- —- —- —-369 369 369 369 369 369 369 369 369Example 6Enter an odd number (n) for (n*n) matrix [Max Matrix Size: 11*11]11The Sum Matrix for 11 * 11 Order is68 81 94 107 120 1 14 27 40 53 66 ——- 67180 93 106 119 11 13 26 39 52 65 67 ——- 67192 105 118 10 12 25 38 51 64 77 79 ——- 671104 117 9 22 24 37 50 63 76 78 91 ——- 671116 8 21 23 36 49 62 75 88 90 103 ——- 6717 20 33 35 48 61 74 87 89 102 115 ——- 67119 32 34 47 60 73 86 99 101 114 6 ——- 67131 44 46 59 72 85 98 100 113 5 18 ——- 67143 45 58 71 84 97 110 112 4 17 30 ——- 67155 57 70 83 96 109 111 3 16 29 42 ——- 67156 69 82 95 108 121 2 15 28 41 54 ——- 671—- —- —- —- —- —- —- —- —- —- —-671 671 671 671 671 671 671 671 671 671 671CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20215Q2. Document Correction and Alignment Problem [20%]Nowadays, the natural language processing technology is increasingly more mature. Recently theBeetles Phone Technology company released their latest smart phone, Beetles Resolver 66. The mosteye-catching part of their release conference is not their new smart phone, but rather, a speechrecognition input method Taxman input method.Now suppose we have a document, and the content of which is generated by a speech recognition inputmethod. Due to the error of The speech recognition input method, some continuously duplicated wordsappear in the content. Also, the format of the content is messy because of different length of each line(see the content of D1.txt in example).You are required to write a program to remove the word duplication and align the content based on thefollowing instructions:1) Your program can assume that every word in the input document consist of at most 20 characters,the input document may be jammed with empty line, but the whole input document represents onesingle paragraph with at most 1000 words. Similarly, your output document should contain oneparagraph with at most 1000 words.2) The given document is a text file, named as document.txt, and the output text file is called output.txt.Your program should place it in the project directory of your VS C++ project.3) Your program only needs to handle the duplication for a single word, like The the the world. Theduplication for multiple words, like The world the world, will not appear in the given documentread by your program. That is, if there is a consecutive sequence of same word (irrespective to theuse of capital letters and lowercase letters), then it is a duplication of that single word.4) Your program can assume that the following characters consider as punctuations in an Englishsentence: comma and full stop. All other characters are treated as a part of a word. In Example 2 onnext page, (Friday) is a word. In this case, (Friday) Friday has no word duplication andyour program needs not to correct this sequence of characters.5) The number of the duplication is indefinite.6) Each duplication of a single word should be replaced by a single occurrence of that word after yourcorrection. For example, The the the world should become The world after your correction.7) After the alignment, each line should contain exactly 60 characters (contain the space andpunctuations), with the exception of the last line.8) Your program can align the content by adjusting the number of words per line and the spacebetween the words. The space number between the words should be determined by the total spaceavailable and the total words number in this line. There is no space between words andpunctuations.a) E.g., if a line can contain 4 words and 3 spaces, then the amounts of space characters inbetween two consecutive words on this line should be 1, 1, and 1.b) E.g., if one line can contain 4 words and 5 spaces, then the amounts of space characters inbetween two consecutive words should be 2, 2, 1.9) After the alignment, each line should start with the word and end with the word or punctuations.10) No word can be spited during the alignment process.11) The result is required to be stored in a text file named as output.txt.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20216Hints:1. Your program may Consider using a two-dimensional array to keep 1000 text strings, eachwith a length sufficient to keep your required characters (including the end-of-string character\0)).2. Your program may consider the logics outlined below.a. It read the lines from the given text file line by line.b. For each line, identify each word together with a possible ending punctuationappending to it and keep that string into an entry in the above-mentioned twodimensionalarray.c. Now you keep the whole text document as a sequence of words in an array.d. Your program should then compare whether two consecutive words in the array referto the same word. If you find a duplicated word, then you can use your strategy toeither remove that duplicated work from the array or mark the corresponding entryin the array as duplicated.e. Now, you can scan the array from the first entry and decide the length of each entry.While your scanning, you can determine which words to be placed in the same line inthe output text file together with the number of space characters you needed toseparate the words in that output line.f. You should then output that line to the text file.g. Of course, before your program ends, it should close all files opened by it.3. Your program may use strlen() and strcmp(), strcpy(). Note that in Visual Studio 2019, thefunctions strcmp() and strcpy() are not allowed by default. See Slide 28 of the Lecture noteL08a to observe how to bypass the limitations. You may use strcmp_s() instead of strcmp()and strcpy_s() instead of strcpy(), which are directly supported by VS 2019 by default.4. Since duplicated occurrences of the same words (e.g., The the the) may be in differentcombinations of uppercase and lowercase characters. Your program may firstly convert theminto all lowercases and then do a comparison. You may just assume that the first occurrenceis the one you wish to put into the text file output.txt.5. A word in the given document may be appended with a full stop or a comma. Since it is a textfile, a line also ends with the end-of-line character (\0). To identify a word, your programshould consider whether a word is ended with a full stop, a comma, or the end-of-linecharacter.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20217Example 1:Input document:The the the town revolved around the river river. In summer,when the blazing sun sun Sun sun beat beat down, it dozed under the weightof of sultry days. On Main Street a sow and her litter litterlitter of pigs might root along the wooden side walks, sharing thedeeply deeply rutted rutted roadway with foraging hens andand and and a hound languidly scratching his fleas fleas.Output Content of Output.txt is as followsThe town revolved around the river. In summer, when theblazing sun beat down, it dozed under the weight of sultrydays. On Main Street a sow and her litter of pigs mightroot along the wooden side walks, sharing the deeply ruttedroadway with foraging hens and a hound languidly scratchinghis fleas.Example 2:Input document:CS2311 Computer Programming2020/21 Semester BDepartment of Computer ScienceCity University of Hong KongAssignment TwoDue Date: 30 April 2021 (Friday) 23:59All questions should be submitted in Canvas. Note that all solutions willbe assessed by (1) correctness, (2) programming styles (includingcomments), and (3) non-redundancy in expressing solutions, if applicable.This assignment contains 5 questions named as Q1 to Q5. Q5 has 10% bonus.Your solutions are forbidden to INCLUDE any library other than iostream,iomanip, cstring, fstream, and/or cmath. Each solution using anyfunction/facility in other libraries will receive 0 mark. If possible, notusing cstring in your solution.Output: Content of Output.txt is as followsCS2311 Computer Programming 2020/21 Semester B Departmentof Computer Science City University of Hong Kong AssignmentTwo Due Date: 30 April 2021 (Friday) 23:59 All questionsshould be submitted in Canvas. Note that all solutions willbe assessed by (1) Correctness, (2) programming styles(including comments), and (3) non-redundancy in expressingsolutions, if applicable. This assignment contains 5questions named as Q1 to Q5. Q5 has 10% bonus. Yoursolutions are forbidden to INCLUDE any library other thaniostream, iomanip, cstring, fstream, and/orcmath. Each solution using any function/facility in otherlibraries will receive 0 mark. If possible, not usingcstring in your solution.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20218Q3. Pointer and Dynamic Array [20%]The code on the next page is an executable solution for Exercise 5 of Tutorial 7 on Array.Your task is to (1) rewrite the program to perform the same computation that meets the followingrequirements. Note that you can use your own array-version programs as long as your program meetsthe following requirements. Every array in the program is a dynamic array. Exceptthe statement in which it uses of the new operator (e.g., int* faces = new int[12];),there is no any use of any array construct in the program. (hint: Use Pointers!)o For example, all of the following array constructs in the code to refer to the elements inthe corresponding dynamic arrays are forbidden to be used. sum[i + j – 1] sum[k] sum[index] sorted[index][0] sorted[index][1] sorted[k][0] sorted[k – 1][0] sorted[k][1] sorted[k – 1][1] sorted[i][0] sorted[i-1][0] faces[sorted[0][1] – 1] faces[sorted[i][1] – 1] faces[k] Every dynamic array must be deleted before the program terminates.o Hint: when deleting the 1D array faces, use delete: delete[] faces;o Hint: when deleting a 2D array, you need to delete all the rows of the 2D array first, andthen the 2D array.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 20219#include iostreamusing namespace std;int main() {char faces[12] = { 0 };int sum[12] = { 0 }; // max 12 values of occurrence countsint sorted[12][2]; // a 2D array// sorted[i][0] = sum of face values kept in sum[index]// sorted[i][1] = index of sum[index]for (int i = 1; i = 6; i++) // i is dice 1for (int j = 1; j = 6; j++) // j is dice 2sum[i + j – 1] = sum[i + j – 1] + 1;for (int k = 0; k 12; k++)if (sum[k] != 0)cout sum[k] occurrence(s) of the sum k + 1 endl;// we firstly copy Sum to rankfor (int index = 0; index 12; index++){sorted[index][0] = sum[index];sorted[index][1] = index + 1;}// then we sort the array sorted[] using bubble sort// check out the lecture note slide 31 of Lec07// we want sorted[0] as the LARGEST after the sorting processint tmp;for (int j = 0; j 12 – 1; j++) // outer loopfor (int k = 12 – 1; k j; k–) // bubblingif (sorted[k][0] sorted[k – 1][0]) { // check against col 0// note that the condition is rather than .tmp = sorted[k][0]; // swap neighbors for col 0sorted[k][0] = sorted[k – 1][0];sorted[k – 1][0] = tmp;tmp = sorted[k][1]; // swap neighbors for col 1sorted[k][1] = sorted[k – 1][1];sorted[k – 1][1] = tmp;}for (int i = 0; i 12; i++)if (sorted[i][0] != 0)cout sorted[i][0] sorted[i][1] endl;char c = A; // the 1st character to be shownfaces[sorted[0][1] – 1] = c;for (int i = 1; i 12; i++) {if (sorted[i][0] != sorted[i – 1][0])c++;if (sorted[i][0] != 0)faces[sorted[i][1] – 1] = c;}// print out the letter rankfor (int k = 0; k 12; k++)if (faces[k] != 0)cout faces[k] Occurrence(s) of the sum k + 1 endl;return 0;}CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 202110Example:1 occurrence(s) of the sum 22 occurrence(s) of the sum 33 occurrence(s) of the sum 44 occurrence(s) of the sum 55 occurrence(s) of the sum 66 occurrence(s) of the sum 75 occurrence(s) of the sum 84 occurrence(s) of the sum 93 occurrence(s) of the sum 102 occurrence(s) of the sum 111 occurrence(s) of the sum 126 75 65 84 54 93 43 102 32 111 21 12F occurrence(s) of the sum 2E occurrence(s) of the sum 3D occurrence(s) of the sum 4C occurrence(s) of the sum 5B occurrence(s) of the sum 6A occurrence(s) of the sum 7B occurrence(s) of the sum 8C occurrence(s) of the sum 9D occurrence(s) of the sum 10E occurrence(s) of the sum 11F occurrence(s) of the sum 12CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 202111Q4. RecursionIn tutorial T04, Exercise 4 asks students to develop a program using four nested loops. The tutorialexercise is reproduced below.Write a program without using any loop (i.e., without any while-loop, for-loop, or do-while loop) to solvethe same program. I.e., the program should accept a positive integer number N and outputs a sequenceof lines in the format of p = a^3 + b^3 = c^3 + d^3 where p, a, b, c, d are all distinct positive integers suchthat p = N and p = a3 + b3 = c3 + d3 and no two lines contains the same set of {a, b, c, d} (in the sense ofvalues).Your program only needs to handle 0 N 1000000.Your program should be efficient. For instance, when a user inputs a large number, your program shouldoutput the correct list and terminates within a reasonable time (e.g., 1 minute). FYI, a correct programcan produce the output within a few seconds.Important Hints: The solution of this question does not require to use any arrays. Students are advised to work on Exercise 4 in Tutorial 4, particularly following the slides 22-27to resolve the subproblems stated on the slides. Or else your program will easily incur bugs. Your program will be at least four recursions, one on each of the four variables a, b, c, andd to enumerate their combinations. The recursion on d is nested in the recursion on c,which in turn is nested in the recursion on b, and which is also in turn nested on a. Students are advised to study the patterns in the reference solution of Exercise 4 in Tutorial 4. Inthat reference solution, when looping on variable b, it needs the information of the currentvalue of the Variable a within the loop body of this loop. Similarly, when looping on variablec, it needs the information of the current value of variable a, and when looping on variabled, it needs the information of the current values of variables a, b, and c.Exercise 4: 1729 is Interesting There was an Indian mathematician who was famous for his intuition fornumbers. When the English mathematician G. H. Hardy came to visit him inthe hospital one day, Hardy mentioned that the number of his taxi was 1729,a rather dull number. To which the Indian mathematician replied, No! It is avery interesting number. 1729 is the smallest positive integer expressibleas the sum of two cubes in two different ways . Verify this claim by writing a program twoCubric.cpp thattakes an integer N and prints out all positive integers less thanor equal to N that can be expressed as the sum of two cubes intwo different ways: i.e., find distinct positive integers a, b, c,and d such that N = a3 + b3 = c3 + d3. hint: Use four nested for loops, one for each of a, b, c, d. Lets assume that N is not a very large number.Read the next slide 20Examples of Console for Ex421Input: 1728Input: 17291729 = 1^3 + 12^3 = 9^3 + 10^3Input: 100001729 = 1^3 + 12^3 = 9^3 + 10^34104 = 2^3 + 16^3 = 9^3 + 15^3Input: 1000001729 = 1^3 + 12^3 = 9^3 + 10^34104 = 2^3 + 16^3 = 9^3 + 15^313832 = 2^3 + 24^3 = 18^3 + 20^339312 = 2^3 + 34^3 = 15^3 + 33^346683 = 3^3 + 36^3 = 27^3 + 30^332832 = 4^3 + 32^3 = 18^3 + 30^340033 = 9^3 + 34^3 = 16^3 + 33^320683 = 10^3 + 27^3 = 19^3 + 24^365728 = 12^3 + 40^3 = 31^3 + 33^364232 = 17^3 + 39^3 = 26^3 + 36^3Check whether or not our Solution contains duplicated entries.1729 = 1 3 + 123 = 93 + 1034104 = 2 3 + 163 = 93 + 153CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 202112Q5. Ash and Pikachu [20% + 10% Bonus]In the Pokemon series of manga, Ash is the main character who joins force with Pikachu, a Pokmon, forvarious adventure. We now have the Pokemon Go. In this question, we are going to implement someof the concepts involved through C++ classes.In this question, your program should allow a user to input: (1) a Pokemon world, (2) a list of Pokemonsand (3) [For Bonus] the information about a trainer called Ash.Ensure that the output follows the format presented in Examples given at the end of this question.Important note: Your program can assume the following: (1) the number of Pokemons is at least 1 and atmost 1000, (2) the name of any object is a cstring consisting of at most 10 characters (including \0), and(3) all user inputs are valid. The concept of PokemonWorld is implemented as a C++ class.o Each PokemonWorld object owns two PokemonStation objects. We simply refer to eachPokemonStation object of this class as a station.o Each PokemonWorld object is responsible to initialize the two (2) stations. (Note: The use of an array to keep two stations will make your program significantly morecomplex than keeping them as two separate variables. The reason is due to our limitedknown-how introduced in CS2311 to use object pointers rather than the limitation of C++.)o When a Pokemon enters a PokemonWorld, the PokemonWorld is responsible to assign thisPokemon to one of the two stations on its own. The assignment of each Pokemon will be done tothe PokemonStation in a round-robin manner (i.e., the first Pokemon is assigned toPokmonStation1, the second Pokemon is assigned to PokmonStation2, the third Pokemon isassigned to PokmonStation1 again, the fourth Pokemon is assigned to PokmonStation2 and so on).o The name variable of the PokemonWorld object can be accessed by the object itself.o Hint: study https://stackoverflow.com/questions/12927169/how-can-i-initialize-c-objectmember-variables-in-the-constructor The concept of PokemonStation is also implemented as a C++ class.o Each object of this class Represents a station which has a station identifier. We simply refer toeach object of this class as a station. The station identifiers are s1 and s2, respectively.o The identifier of each station is provided by the PokemonWorld object that owns the stationthrough the class constructor of PokemonStation. (Hint: You may wish to initialize eachPokemonStation when the constructor of PokemonWorld object is being called.)o Each station maintains its own list of Pokemons assigned by the PokemonWorld object.o Both the identifier of a PokemonStation and the list of Pokemons (i.e., each Pokemon thatbelongs to a particular PokemonStation) should not be accessible by any other object nor bereturned via a function.o Only PokemonWorld and the PokemonStation itself can invoke the functions of that station. The concept of Pokemon is also implemented as a C++ class.CityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 202113o Each Pokemon is an object of this class.o Each Pokemon object has its own name (a cstring) and hp value (an integer).o All data members (name, hp) of each Pokemon object once initialized can only be accessed bythat Pokemon object. The main() function is responsible for the following:o To accept all the inputs from the user.o To create a PokemonWorld and all the Pokemons.o To set each Pokemon to enter the PokemonWorld.o To call the method print() of Pokemon, which in turn calls the method print() of eachPokemonStation, which in turn calls the method print() of each Pokemon to print out theinformation of the PokemonWorld, the PokemonStation, and the Pokemon, respectively.Bonus Part [Extra 10%] The requirements stated above should remain intact. A Trainer needs to be implemented as a C++ class.o Each trainer (e.g. Ash) is an object of this class.o Each trainer has his/her own name (cstring), identifier (integer), and strength (integer). All thisinformation is taken from the user as input from console.o All data members of each trainer object cannot be accessed outside the trainer object. That is,the value of each data member should not be returned via a function nor be retrieved via callby-reference. The main() function is responsible to create a trainer called Ash and let Ash enter the PokemonWorld. PokemonWorld is responsible to guide Ash to visit its two stations one by one once Ash enters thisworld. Each station is responsible to determine whether a trainer can capture each Pokemon kept by thestation. Specifically, if the hp of the Pokemon is strictly less than () the strength of the trainer, thenthe Pokemon is marked as captured by the trainer.o Note that following the above requirements, both the hp of the Pokemon and the strength ofthe trainer should not be returned via any function. The print() method of each captured Pokemon calls the print() method of the trainer object thatcaptures it to print the information of Ash.More description about the Above three classesIdeas on the data members of thePokeworld class.[The class may contain other data members]Ideas on the data members of thePokeStation class.[The class may contain other data members]Ideas on the data members of thePokemon class.[The class may contain other data membersName The name of this PokeworldobjectID The identifier of this station,such as 1 and 2.Name The name of this Pokemonobjects1 1st station owned by thePokeworldlist An array of Pokemon hp The HP of the Pokemonobjects2 2nd station owned by thePokeworldCityU CS2311 2020/21 Sem B Assignment 2 due on 30 April 202114Example 1: (without Bonus)What is the World Name?MacauPlease input the number of Pokemon in the Macau world7Please input the Characteristics of all Pokemon: Name HPPikachu 50Regice 100Articuno 100Charizard 150Blastoise 240Mew 340Gengar 101World is MacauPokemon in Station #1:Pikachu HP 50Articuno HP 100Blastoise HP 240Gengar HP 101Pokemon in Station #2:Regice HP 100Charizard HP 150Mew HP 340Example 2: (without Bonus)What is the World Name?HongkongPlease input the number of Pokemon in the Hongkong world1Please i”

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导