Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There is a C++ assignment. I need a correct answer which could fulfill all the important notes. Besides, use the skeleton codes please. Thank you

There is a C++ assignment. I need a correct answer which could fulfill all the important notes. Besides, use the skeleton codes please.

Thank you so much!!!

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Skeleton Code

/* * COMP2011 (Spring 2021) Programming Assignment 1 * * Student name: FILL YOUR NAME HERE * Student ID: FILL YOUR STUDENT ID NUMBER HERE * Student email: FILL YOUR EMAIL HERE * */ #include  #include  using namespace std; // Global Constants // You cannot modify these global constants // You cannot use your own global variables const int LONGLONG_SIZE = 64; const long long ONE = 1; // End of Global Constants // Helper Functions // Declare additional helper functions below when necessary // End of Helper Functions // Tasks // Please write your code under the TODO tag // You cannot change the function declarations // Task 1 int initialize(int numSeats, unsigned long long &bitmap) { // Task 1 TODO // End of Task 1 TODO } // Task 2 int getSeatState(int numSeats, unsigned long long bitmap, int seatIndex) { // Task 2 TODO // End of Task 2 TODO } // Task 3 void printSeats(int numSeats, unsigned long long bitmap) { // Task 3 TODO // End of Task 3 TODO } // Task 4 int setSeatOccupied(int numSeats, unsigned long long &bitmap, int seatIndex) { // Task 4 TODO // End of Task 4 TODO } // Task 5 int setSeatEmpty(int numSeats, unsigned long long &bitmap, int seatIndex) { // Task 5 TODO // End of Task 5 TODO } // Task 6 int getSeatDistance(int numSeats, unsigned long long bitmap, int seatIndex) { // Task 6 TODO // End of Task 6 TODO } // Task 7 int findSeat(int numSeats, unsigned long long bitmap) { // Task 7 TODO // End of Task 7 TODO } // DO NOT WRITE ANYTHING AFTER THIS LINE. ANYTHING AFTER THIS LINE WILL BE REPLACED void run_init(int &numSeats, unsigned long long &bitmap) { cout > numSeats; cout ::max(), ' '); } while (initResult > seatIndex; seatState = getSeatState(numSeats, bitmap, seatIndex); if (seatState > seatIndex; result = setSeatOccupied(numSeats, bitmap, seatIndex); if (result > seatIndex; result = setSeatEmpty(numSeats, bitmap, seatIndex); if (result > seatIndex; distance = getSeatDistance(numSeats, bitmap, seatIndex); if (distance > option; switch (option) { case 0: cout   COMP2011 PA1: Social Distancing Under COVID-19 O IMPORTANT NOTES There are a few things you CANNOT do. Failure to observe these rules would potentially result in ZERO mark for your assignment. You cannot define and use any arrays You cannot define and use any additional global variables. You cannot change any function headers already included in the skeleton code. You cannot include or use any additional libraries (e.g., string) This programming assignment is challenging, so you are highly recommended to start early. If you need clarification on the requirements, please feel free to post on the Piazza. However, to avoid cluttering the forum with repeated/trivial questions, please carefully read all the given code, webpage description, sample output, and latest FAQ (refresh this page regularly) carefully before you post your questions. Also please be reminded that we won't debug for any student's assignment for the sake of fairness. About academic integrity We value academic integrity very highly. Please read the Honor Code section on our course webpage to make sure you understand what is considered as plagiarism and what the penalties are. The following are some of the highlights:  DO NOT try your "LUCK" - we use sophisticated plagiarism detection software to find cheaters. We also review codes for potential cases manually.  The penalty (for BOTH the copier and the copiee) is not just getting a zero in your assignment. Please read the Honor Code thoroughly. . Serious offenders will fail the course immediately, and there may be additional disciplinary actions from the department and university, up to and including expulsion 1 Introduction Please sit here I          Under the COVID-19 pandemic, we all need to protect ourselves and work together to fight the virus. There are many measures we should take, such as wearing a mask and practicing hand hygiene. And one of the most important measures is to keep Social Distancing. The social distancing measure requires us to keep maximum distance from others in the public space as far as we can, so as to minimize the risk of COVID-19 spreading in the community. Now, imagine that you are volunteering at your local community, and you are assigned to manage a row of seats in a public area. Your job will be to arrange seats for people according to the social distancing rule. Since you have learned programming with C++ in COMP2011 , you want to design a simple Seat Management Program to help you better do this job. Sounds interesting? Let's get started now! 2 Preliminaries In this programming assignment, we hope that you can make full use of knowledges you have learned so far in the COMP2011 class, like the Control Statement, Loop Statement and Function . Beyond that, you will learn basic usage of Bitwise Operators in C++ to manipulate Binary Numbers to complete this assignment NOTE: In this assignment, we consider only binary numbers and bitwise operators for unsigned integers. 2.1 Binary Numbers The integers we used in our daily lives, such as "Year 2021 " and "iPhone 12 ", are mostly expressed in decimals . In the decimal, each digit in a number takes on one of ten possible values, called "digits", from 0 to 9, and each position to the left of the decimal indicates an increased positive power of 10. For example, 2021 = 2 * 103 +0 * 102 + 2 * 101 + 1 * 10. Alternatively, integers can also be expressed in the binaries . The Binary Numbering System is the most fundamental numbering system in all electronic and computer based systems. It follows the same set of rules as the decimal numbering system except for using powers of 2 instead of 10 . For example, the binary number 11111100101 represents 1 * 210 + 1 * 29 +1 * 28 + 1 * 2? + 1 * 26 +1 * 25 + 0 + 24 +0 * 23 + 1 * 22 +0) * 2! + 1 * 2, which is also 2021 in decimal. 2.2 Bitwise Operators in C++ Every programmer knows that computers can only process information represented by a series of binary digits (bits, 1 and (). This means when we store the decimal number 2021 in memory of the computer, it actually stores a sequence of bits represented as 11111100101. In C++ we have a special set of operators to efficiently deal with the binary representation of a number, that is AND (&), OR (1) , XOR (^), NOT (~), BIT SHIFT LEFT (). 1. The & (bitwise AND) in C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Otherwise, it returns 0. 2. The l (bitwise OR) in C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if at least one of the two bits is 1. Otherwise, it returns 0. 3. The ^ (bitwise XOR) in C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. Otherwise, it returns 0. 4. The (bitwise NOT) in C++ takes one number and inverts all bits of it. a b a&b alb a^b wa 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 1 0 1 1 1 1 0 0 5. The > (right shift) in C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. When we shift any number to the right, the right-most bits are discarded, while the left-most bits are replaced by zeroes. 0 0 0 1 1 0 0 1 25 >> 1 = 12 Replacement bits 0 0 0 0 1 1 0 0 1 Discarded bits You can run the following example code to see how the bitwise operators take effect. #include  using namespace std; int main() { // 12 = 0000 0000 0000 1100 (in binary) // 25 = 0000 0000 0001 1001 (in binary) unsigned short a = 12, b = 25; unsigned short ri, r2, r3, r4, r5, r6; cout > 1 = 0000 0000 0000 1100 = 12 (in decimal) r5 = b > > 1; cout > 1 = " >) operators should not be used for negative numbers, since it results in undefined behavior. Also, if a number is shifted more than the size of it, the behavior is undefined. For the size of different variable types, please refer to lecture notes for details. 2.3 Common Usages of Bitwise Operation in C++ 1. Multiplying x by 2 can be done with x > 1 3. Getting the k-th bit from the right end of x can be done with x >> (k - 1) & 1 4. Setting the k-th bit from the right end of x to 1 can be done with x | 1  (k - 1) 5. Setting the k-th bit from the right end of x to can be done with x & -(1  using namespace std; const long long ONE = 1; int main() { int k=4; // 132 = 10000100 in biranry // size of unsigned long long is 64 bits unsigned long long x = 132; unsigned long long ri, r2, r3, r4, r5; cout > 1; cout > (k - 1) & 1; cout >c in a loop of numSeats rounds, where c is a char variable, to read the seat states one by one. 4.2 Task 2 10 Points Query the current state of a seat int getSeatState(int numSeats, unsigned long long bitmap, int seat Index)  Parameters: 0 numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat. Description: o If seatIndex is not in [0, numSeats - 1], return -1. o Else return the state of the seat at seat Index as oor 1. Example o numSeats =9, bitmap =010000100 (binary), seat Index =1 return : 1 . 4.3 Task 3 10 Points Print the current state of all seats ( bitmap ) to screen void printSeats(int numSeats, unsigned long long bitmap)  Parameters: numSeats : total number of seats in the row. o bitmap : bitmap that represents the states of all seats. Description: Print the state of all seats to the screen ( standard output ), with an ending endl Example numSeats =9, bitmap =010000100 (binary) Print 010000100 to screen with and ending endl .  Hints You can call the function getSeatState() you have implemented in Task 2. 4.4 Task 4 10 Points Change the state of a seat to OCCUPIED int setSeatOccupied (int numSeats, unsigned long long & bitmap, int seat Index)  Parameters: numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat.  Description: olf seat Index is not in [O, numSeats -1], return -1. o If the seat at seatIndex is already occupied, return -1 . Else change the state of the seat at seatIndex to 1. Calculate and assign new value to bitmap , and return o Example numSeats =9, bitmap =010000100 (binary), seatIndex =8  After function call, bitmap =010000101 (binary), return 0 4.5 Task 5 10 Points Change the state of a seat to EMPTY int setSeatEmpty(int numSeats, unsigned long long & bitmap, int seat Index)  Parameters: numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat. Description: o If seatIndex is not in [O, numSeats -1], return -1 . o If the seat at seatIndex is already empty, return -1. Else change the state of the seat at seatIndex to 0. Calculate and assign new value to bitmap , and return o Example numSeats =9, bitmap =010000100 (binary), seatIndex =6 After function call, bitmap =010000000 (binary), return 0 4.6 Task 6 25 Points Calculate the distance from a seat to its closest occupied seat ( minDist ) int getSeatDistance(int numSeats, unsigned long long bitmap, int seat Index) . Parameters: O numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat.  Description: o If seatIndex is not in [O, numSeats - 1], return -1. Else calculate and return the distance to its closest occupied seats mindist from the seat at seatIndex . Please refer to section 3.4 for details.  Example numSeats =9, bitmap =010000100 (binary), seat Index =8 return : 2 4.7 Task 7 15 Points Find an empty seat with maximum minDist int findSeat(int numSeats, unsigned long long bitmap)  Parameters: 0 numSeats : total number of seats in the row. o bitmap : bitmap that represents the states of all seats.  Description: o If all seats are occupied, return -1. o Else return the index of the seat with maximum minDist. Example o numSeats =9, bitmap =010000100 (binary) . return :3 5 Downloads  Skeleton Code Please download and complete the skeleton code, it includes a driver program for your testing. You can modify the given driver functions and write your own test functions for testing.  Sample Executables We have also prepared sample executables for your better understanding about this assignment. o Windows 10: download macOS: download o Linux: download 6 Submission You are required to submit your solution to our auto-grading system ZINC (ZINC). Please rename your solution file to "pa1.cpp", and submit it to ZINC. 7 Grading Your assignment will be automatically graded by our auto-grading system ZINC . . Please ensure your program can be compiled without any problem in ZINC by checking the report from ZINC. If your program cannot be compiled, you will receive 0 mark.  In the skeleton code, anything written below the line // DO NOT WRITE ANYTHING AFTER THIS LINE. ANYTHING AFTER THIS LINE WILL BE REPLACED. will be replaced during grading. Make sure you write all your solution codes above this line. . Your implementation will be graded by running multiple test cases, each contains a different input parameter. A test case should finish within 3 seconds. Failing to do so would result in O mark for that test case. . Your program should not crash during a test case. If it does, you will receive 0 marks for that test case.  The examples listed above are by no means complete. Additional test cases will be used during grading. You are encouraged to test your code using your own test cases.  For tasks that deal with standard output, please compare your output with those of sample executables and make sure they are EXACTLY the same. You should also check for the ZINC grading report. Be careful about endl.  For all tasks, please make sure there is no extra output in the task functions, e.g. extra endl or your own debugging messages. Extra output will also lead to O mark for that task. Only your latest submission will be graded. You are responsible to make sure the latest submission is correct. You should wait for ZINC to grade your work and then check the grading report carefully. 8 FAQ 1. Usage of string You are not allowed to either include any additional library or use the string class. Please refer to this post for further details. 2. How to use sample executables on Mac? Please refer to this post. 3. Test cases and Grading There are 15 test cases available to you on ZINC. Those test cases are prepared for you to simply check your submission, and are by no means complete. We will introduce additional test cases to grade your submission. Getting full marks now on ZINC dose not mean you can get full marks in this assignment. Please read the PA1 description and consider all possible situations carefully when you do this assignment. 9 Change Log 1. Fri Feb 19 2021, typos in descriptions of Section 4.2 / Section 4.4 / Section 4.5 / Section 4.6: H seat Index is not in fe, LONGLONG_SIZE-1], rettim If seat Index is not in [o, numSeats -1], return -1. 2. Fri Feb 19 2021, typos in example code in Section 2.2: 0000 0000 0000 1100 | 2000 2000 2001 1001 - 0000 0000 0000 1101 - 20 (in decimal) 1/ 0000 0000 0000 1100 0000 0000 0001 1001 = 0000 0000 0001 1101 = 29 (in decimal) 2000 2000 2000 1100 = 1111 1111 1111 1100 = 65522 (in decimal) // - 0000 0000 0000 1100 = 1111 1111 1111 0011 = 65523 (in decimal) 3. Sat Feb 20 2021, mistakes in descriptions of Section 4.4 / Section 4.5: Caleulate and return the new bitmap Calculate and assign new value to bitmap, and return o 4. Sat Feb 20 2021, typo in Section 2.2: The ). 1. The & (bitwise AND) in C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Otherwise, it returns 0. 2. The l (bitwise OR) in C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if at least one of the two bits is 1. Otherwise, it returns 0. 3. The ^ (bitwise XOR) in C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. Otherwise, it returns 0. 4. The (bitwise NOT) in C++ takes one number and inverts all bits of it. a b a&b alb a^b wa 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 1 0 1 1 1 1 0 0 5. The > (right shift) in C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. When we shift any number to the right, the right-most bits are discarded, while the left-most bits are replaced by zeroes. 0 0 0 1 1 0 0 1 25 >> 1 = 12 Replacement bits 0 0 0 0 1 1 0 0 1 Discarded bits You can run the following example code to see how the bitwise operators take effect. #include  using namespace std; int main() { // 12 = 0000 0000 0000 1100 (in binary) // 25 = 0000 0000 0001 1001 (in binary) unsigned short a = 12, b = 25; unsigned short ri, r2, r3, r4, r5, r6; cout > 1 = 0000 0000 0000 1100 = 12 (in decimal) r5 = b > > 1; cout > 1 = " >) operators should not be used for negative numbers, since it results in undefined behavior. Also, if a number is shifted more than the size of it, the behavior is undefined. For the size of different variable types, please refer to lecture notes for details. 2.3 Common Usages of Bitwise Operation in C++ 1. Multiplying x by 2 can be done with x > 1 3. Getting the k-th bit from the right end of x can be done with x >> (k - 1) & 1 4. Setting the k-th bit from the right end of x to 1 can be done with x | 1  (k - 1) 5. Setting the k-th bit from the right end of x to can be done with x & -(1  using namespace std; const long long ONE = 1; int main() { int k=4; // 132 = 10000100 in biranry // size of unsigned long long is 64 bits unsigned long long x = 132; unsigned long long ri, r2, r3, r4, r5; cout > 1; cout > (k - 1) & 1; cout >c in a loop of numSeats rounds, where c is a char variable, to read the seat states one by one. 4.2 Task 2 10 Points Query the current state of a seat int getSeatState(int numSeats, unsigned long long bitmap, int seat Index)  Parameters: 0 numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat. Description: o If seatIndex is not in [0, numSeats - 1], return -1. o Else return the state of the seat at seat Index as oor 1. Example o numSeats =9, bitmap =010000100 (binary), seat Index =1 return : 1 . 4.3 Task 3 10 Points Print the current state of all seats ( bitmap ) to screen void printSeats(int numSeats, unsigned long long bitmap)  Parameters: numSeats : total number of seats in the row. o bitmap : bitmap that represents the states of all seats. Description: Print the state of all seats to the screen ( standard output ), with an ending endl Example numSeats =9, bitmap =010000100 (binary) Print 010000100 to screen with and ending endl .  Hints You can call the function getSeatState() you have implemented in Task 2. 4.4 Task 4 10 Points Change the state of a seat to OCCUPIED int setSeatOccupied (int numSeats, unsigned long long & bitmap, int seat Index)  Parameters: numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat.  Description: olf seat Index is not in [O, numSeats -1], return -1. o If the seat at seatIndex is already occupied, return -1 . Else change the state of the seat at seatIndex to 1. Calculate and assign new value to bitmap , and return o Example numSeats =9, bitmap =010000100 (binary), seatIndex =8  After function call, bitmap =010000101 (binary), return 0 4.5 Task 5 10 Points Change the state of a seat to EMPTY int setSeatEmpty(int numSeats, unsigned long long & bitmap, int seat Index)  Parameters: numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat. Description: o If seatIndex is not in [O, numSeats -1], return -1 . o If the seat at seatIndex is already empty, return -1. Else change the state of the seat at seatIndex to 0. Calculate and assign new value to bitmap , and return o Example numSeats =9, bitmap =010000100 (binary), seatIndex =6 After function call, bitmap =010000000 (binary), return 0 4.6 Task 6 25 Points Calculate the distance from a seat to its closest occupied seat ( minDist ) int getSeatDistance(int numSeats, unsigned long long bitmap, int seat Index) . Parameters: O numSeats : total number of seats in the row. bitmap : bitmap that represents the states of all seats. seatIndex : index of the target seat.  Description: o If seatIndex is not in [O, numSeats - 1], return -1. Else calculate and return the distance to its closest occupied seats mindist from the seat at seatIndex . Please refer to section 3.4 for details.  Example numSeats =9, bitmap =010000100 (binary), seat Index =8 return : 2 4.7 Task 7 15 Points Find an empty seat with maximum minDist int findSeat(int numSeats, unsigned long long bitmap)  Parameters: 0 numSeats : total number of seats in the row. o bitmap : bitmap that represents the states of all seats.  Description: o If all seats are occupied, return -1. o Else return the index of the seat with maximum minDist. Example o numSeats =9, bitmap =010000100 (binary) . return :3 5 Downloads  Skeleton Code Please download and complete the skeleton code, it includes a driver program for your testing. You can modify the given driver functions and write your own test functions for testing.  Sample Executables We have also prepared sample executables for your better understanding about this assignment. o Windows 10: download macOS: download o Linux: download 6 Submission You are required to submit your solution to our auto-grading system ZINC (ZINC). Please rename your solution file to "pa1.cpp", and submit it to ZINC. 7 Grading Your assignment will be automatically graded by our auto-grading system ZINC . . Please ensure your program can be compiled without any problem in ZINC by checking the report from ZINC. If your program cannot be compiled, you will receive 0 mark.  In the skeleton code, anything written below the line // DO NOT WRITE ANYTHING AFTER THIS LINE. ANYTHING AFTER THIS LINE WILL BE REPLACED. will be replaced during grading. Make sure you write all your solution codes above this line. . Your implementation will be graded by running multiple test cases, each contains a different input parameter. A test case should finish within 3 seconds. Failing to do so would result in O mark for that test case. . Your program should not crash during a test case. If it does, you will receive 0 marks for that test case.  The examples listed above are by no means complete. Additional test cases will be used during grading. You are encouraged to test your code using your own test cases.  For tasks that deal with standard output, please compare your output with those of sample executables and make sure they are EXACTLY the same. You should also check for the ZINC grading report. Be careful about endl.  For all tasks, please make sure there is no extra output in the task functions, e.g. extra endl or your own debugging messages. Extra output will also lead to O mark for that task. Only your latest submission will be graded. You are responsible to make sure the latest submission is correct. You should wait for ZINC to grade your work and then check the grading report carefully. 8 FAQ 1. Usage of string You are not allowed to either include any additional library or use the string class. Please refer to this post for further details. 2. How to use sample executables on Mac? Please refer to this post. 3. Test cases and Grading There are 15 test cases available to you on ZINC. Those test cases are prepared for you to simply check your submission, and are by no means complete. We will introduce additional test cases to grade your submission. Getting full marks now on ZINC dose not mean you can get full marks in this assignment. Please read the PA1 description and consider all possible situations carefully when you do this assignment. 9 Change Log 1. Fri Feb 19 2021, typos in descriptions of Section 4.2 / Section 4.4 / Section 4.5 / Section 4.6: H seat Index is not in fe, LONGLONG_SIZE-1], rettim If seat Index is not in [o, numSeats -1], return -1. 2. Fri Feb 19 2021, typos in example code in Section 2.2: 0000 0000 0000 1100 | 2000 2000 2001 1001 - 0000 0000 0000 1101 - 20 (in decimal) 1/ 0000 0000 0000 1100 0000 0000 0001 1001 = 0000 0000 0001 1101 = 29 (in decimal) 2000 2000 2000 1100 = 1111 1111 1111 1100 = 65522 (in decimal) // - 0000 0000 0000 1100 = 1111 1111 1111 0011 = 65523 (in decimal) 3. Sat Feb 20 2021, mistakes in descriptions of Section 4.4 / Section 4.5: Caleulate and return the new bitmap Calculate and assign new value to bitmap, and return o 4. Sat Feb 20 2021, typo in Section 2.2: The 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions