Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.21 KB | None | 0 0
  1. Skip to main content
  2. eClass
  3. HELP
  4. EMAIL
  5. Laith Haddadin
  6. CMPUT 201 Wi20 - PRACTICAL PROG METHODOLOGY Combined LAB LEC Wi20
  7. Dashboard
  8. My courses
  9. CMPUT 201 (Winter 2020 LAB LEC)
  10. Week 3: January 20 to 24
  11. Assignment 2 Description
  12. Assignment 2 Description
  13. CMPUT201 Assignment 2: Expressions, Operators, Loops, Conditionals, Switch
  14. The goal of this assignment is to have you demonstrate knowledge of expressions, operators, booleans, and if/switch/break/goto/do...while/for.
  15.  
  16. You may not use code from anyone else! Online resources and collaborators are for concepts only.
  17.  
  18. README.md
  19. Remember, your submission needs to include a README.md file like:
  20.  
  21. # CMPUT201 Assignment 2: Expressions, Operators, Loops, Conditionals, Switch
  22.  
  23. * By: __YOUR_NAME_HERE__
  24. * CCID: __YOUR_CCID_HERE__
  25. * Student Number: __YOUR_STUDENT_NUMBER_HERE__
  26.  
  27. # Sources
  28.  
  29. Tell us what online resources you used and who you collaborated with:
  30.  
  31. * __COLLABORATOR_1__
  32. * __StackOverflow_Link__
  33.  
  34. # License
  35.  
  36. This software is NOT free software. Any derivatives and relevant shared files are under the following license:
  37.  
  38. Copyright 2020 Abram Hindle, Hazel Campbell
  39.  
  40. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, and submit for grading and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  41.  
  42. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  43. * You may not publish, distribute, sublicense, and/or sell copies of the Software.
  44. * You may not share derivatives with anyone except UAlberta staff.
  45. * You may not pay anyone to implement this assignment and relevant code.
  46. * Paid tutors who work on this code owe the Department of Computing Science at the University of Alberta $10000 CAD per derivative.
  47. * By publishing this code publicly said publisher owes the Department of Computing Science at the University of Alberta $10000 CAD.
  48. * You must not engage in plagiarism
  49.  
  50. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  51. Code Quality Standards
  52. Your code must meet the code quality standards. If you've taken CMPUT 174 before these should be familiar to you.
  53.  
  54. Use readable indentation.
  55. Blocks should be indented (everything between { and })
  56. One line should not have more than one statement on it. However, a long statement should be split into multiple lines.
  57. Use only idiomatic for loops.
  58. Use descriptive variable names. It should be obvious to the person reading (and marking your code) what each variable does.
  59. Every switch case should have a break.
  60. Never use goto.
  61. Never use control flow without curly braces (if, else, do, while, for, etc.)
  62. Use <stdbool.h>, bool, true, and false to represent boolean values.
  63. Never compare with true, e.g. never == true.
  64. Do not leave commented-out code in your code.
  65. Provide comments for anything that's not totally and completely obvious.
  66. Always check to see if I/O functions were actually sucessful.
  67. On an unexpected error, print out a useful error message and exit the program.
  68. For invalid input from the user you should handle it by asking the user to try again or by exiting the program with exit(1), exit(2), etc. or returning 1 or 2 etc. from main.
  69. For unexpected errors, such as fgets failing to read anything, consider abort().
  70. Main should only return 0 if the program was successful.
  71. Do not use magic literals (magic numbers or magic strings).
  72. If a value has a particular meaning, give a meaningful name with #define or by declaring a constant with const.
  73. Values other than 0 and 1 with the same meaning should not appear more than once.
  74. 0 or 1 with a meaning other than the immediately obvious should also be given a name.
  75. String literals should not appear more than once.
  76. This includes magic numbers that appear in strings!
  77. Program should compile without warnings with gcc -std=c99 -pedantic -Wall -Wextra -ftrapv -ggdb3.
  78. For more info, see "Code & Compilation Requirements" on eClass.
  79. Note: The next assignment will have more Code Quality requirements.
  80.  
  81. Testing your Program
  82. Correct input-output examples are provided. For example, q1a-test1-input.txt is the input to your ./question1 program. If your program is correct, its output will match q1a-test1-expected-output.txt.
  83.  
  84. You can tell if your output matches exactly by saving the output of your program to a file with bash's > redirection operator. For example, ./question1 >my-output-1.txt will save the output of your question1 program into the file named my-output-1.txt instead of showing it on the screen. Be warned! It will overwrite the file, deleting anything that used to be in my-output-1.txt.
  85.  
  86. Similarly, you can give input to your program from a file instead of typing it by using bash's < redirection operator. For example, ./question1 <q1a-test1-input.txt will run your program with the contents of q1a-test1-input.txt instead of being typed out.
  87.  
  88. These two can be combined. For example,
  89.  
  90. ./question1 <q1a-test1-input.txt >my-output-1.txt
  91. will use the contents of q1a-test1-input.txt as input and save the output of your program in my-outputr-1.txt.
  92.  
  93. When you want to check if your output is correct, you can then use the diff command from bash to compare two files. For example,
  94.  
  95. diff -b my-output-1.txt q1a-test1-expected-output.txt
  96. will compare the two files my-output-1.txt and q1a-test1-expected-output.txt and show you any differences. -b tells diff to ignore extra spaces and tabs.
  97.  
  98. diff will only show you something if there's a difference between the two files. If diff doesn't show you anything, that means the two files were the same!
  99.  
  100. So, putting it all together, to check if your program handles one example input correctly, you can run:
  101.  
  102. ./question1 <q1a-test1-input.txt >my-output-1.txt
  103. diff -b my-output-1.txt q1a-test1-expected-output.txt
  104. If diff doesn't show you anything, that means the two files were the same, so your output is correct.
  105.  
  106. This is what the included scripts (test-q1a.sh, etc.) do.
  107.  
  108. However, the examples are just that: examples. If your code doesn't produce the correct output for other inputs it will still be marked wrong.
  109.  
  110. Questions
  111. Question 1
  112. Overview
  113. Write a C program that simulates the Monty Hall game. Monty Hall is a scenario where you imagine being on a game show and you're given a chance to win a car by selecting one of the three doors. Behind one door is a car while behind the other two doors are goats. The car and the goats were placed randomly behind the doors before the show. Monty Hall works according to following rules:
  114.  
  115. The player choose one of the three doors.
  116. Monty chooses another door and opens it revealing a goat. (Monty never chooses the door with the car behind it!)
  117. Monty asks the player if they want to switch the door choice or keep the original choice.
  118. The user may switch by pressing Y or y. Alternatively, the user may choose to not switch by pressing N or n.
  119. The door selected by the player is opened and the player wins or loses. The message is displayed accordingly.
  120. Monty asks the player if they want to play again.
  121. Additional Requirements
  122. Put your C code for this question in question1.c
  123. You should compile the program as ./question1
  124. You must make a ./question1.sh to compile and run your question1.c program
  125. You must demonstrate the proper use of a switch statement in this question.
  126. stdlib.h has a random function that you need to use to generate a number randomly, rand().
  127. To get help on using rand() try man 3 rand at your bash prompt!
  128. Do not use srand(), or the car will be behind a different door and your output will be wrong!
  129. Wait for the user to press enter after the yes-or-no questions. If the user enters more than one letter, ignore the other letters. Only consider the first letter.
  130. For example, Yes should count as yes, no should count as no, Ninetails should also count as no. This is just to keep things simple for you.
  131. If you need a string buffer, use a buffer big enough to hold a string 1024 chars long.
  132. Represent the doors as integers 0, 1, 2.
  133. The int returned by rand() is a random integer that can be very large. Divide it by three and take the remainder to get a random integer 0, 1, or 2.
  134. At the beginning of a game, take 1 random number from rand() to decide which door the car is behind.
  135. If the player's first guess is the door with the car, Monty can choose to reveal either goat. For this assignment, have monty choose to reveal the goat behind the door with the lowest number.
  136. Example: Car is behind #1. Player's initial guess is door #1. Monty reveals the goat behind door #2.
  137. If the player's first guess is a door with a goat, Monty must reveal the other goat.
  138. The car can only move between games.
  139. You should only need to modify one number and no strings in your code to change the number of doors.
  140. Example Runs
  141. Following is an example run of a program in which the car was behind door 2.
  142.  
  143. Choose a door from 1 to 3: 1
  144. You chose door #1.
  145. Monty opens door #3, revealing a goat!
  146. Would you like to switch to door #2? Y
  147. You chose door #2.
  148. Monty opens door #2, revealing a car! You win!
  149. You've won 1 games out of 1 games.
  150. Would you like to play again? n
  151. Following is an example of putting in invalid inputs:
  152.  
  153. Choose a door from 1 to 3: I don't know
  154. Choose a door from 1 to 3: There's only 3?
  155. Choose a door from 1 to 3: OKay um...
  156. Choose a door from 1 to 3:
  157. Choose a door from 1 to 3: 4
  158. Choose a door from 1 to 3: Door 2
  159. Choose a door from 1 to 3: 1
  160. You chose door #1.
  161. Monty opens door #3, revealing a goat!
  162. Would you like to switch to door #2? Why?
  163. Error: please enter Y or N: What's wrong with Door #1?
  164. Error: please enter Y or N:
  165. Error: please enter Y or N: Maybe.
  166. Error: please enter Y or N:
  167. Error: please enter Y or N: Yes
  168. Monty opens door #2, revealing a car! You win!
  169. Would you like to play again? Not really
  170. Marking
  171. 1 Point Program output is correct for a valid input. (Examples: test-q1a.sh)
  172. 1 Point Program output is correct for an invalid input. (Examples: test-q1b.sh)
  173. 1 Point Program uses switch statements (and uses them correctly).
  174. 1 Point Quality of question1.c meets the quality standards, listed above.
  175. 1 Point question1.sh meets the requirements above.
  176. Hints
  177. If you are left with an extra newline after doing scanf, try adding %*c to the scanf. This will eat 1 character (the newline character) but you do not need to provide an argument to scanf and it won't count towards the return value of scanf. See man 3 scanf.
  178. It's probably easier to use fgets to read all user responses. If you use fgets, you can convert the string it reads into its first argument to an int using the function atoi. See man 3 atoi.
  179. Question 2
  180. Write a C program question2.c that takes a number as input 1 to 9 and prints a number pattern as follows. If the user enters a number that's not 1 to 9 or not a number the program returns 1 and prints "Invalid input\n" to the terminal.
  181.  
  182. Additional Requirements
  183. Put your C code for this question in question2.c
  184. You should compile the program as ./question2
  185. You must make ./question2.sh to compile and run your question2.c program
  186. Program input and output should match the examples.
  187. Example runs
  188. Enter a number between 1 and 9: 0
  189. Invalid input
  190. Enter a number between 1 and 9: 1
  191. 11
  192. Enter a number between 1 and 9: 2
  193. _1_212
  194. 212_1_
  195. Enter a number between 1 and 9: 3
  196. __1__32123
  197. _212__212_
  198. 32123__1__
  199. Enter a number between 1 and 9: 4
  200. ___1___4321234
  201. __212___32123_
  202. _32123___212__
  203. 4321234___1___
  204. Enter a number between 1 and 9: 5
  205. ____1____543212345
  206. ___212____4321234_
  207. __32123____32123__
  208. _4321234____212___
  209. 543212345____1____
  210. Enter a number between 1 and 9: 6
  211. _____1_____65432123456
  212. ____212_____543212345_
  213. ___32123_____4321234__
  214. __4321234_____32123___
  215. _543212345_____212____
  216. 65432123456_____1_____
  217. Enter a number between 1 and 9: 7
  218. ______1______7654321234567
  219. _____212______65432123456_
  220. ____32123______543212345__
  221. ___4321234______4321234___
  222. __543212345______32123____
  223. _65432123456______212_____
  224. 7654321234567______1______
  225. Enter a number between 1 and 9: 8
  226. _______1_______876543212345678
  227. ______212_______7654321234567_
  228. _____32123_______65432123456__
  229. ____4321234_______543212345___
  230. ___543212345_______4321234____
  231. __65432123456_______32123_____
  232. _7654321234567_______212______
  233. 876543212345678_______1_______
  234. Enter a number between 1 and 9: 9
  235. ________1________98765432123456789
  236. _______212________876543212345678_
  237. ______32123________7654321234567__
  238. _____4321234________65432123456___
  239. ____543212345________543212345____
  240. ___65432123456________4321234_____
  241. __7654321234567________32123______
  242. _876543212345678________212_______
  243. 98765432123456789________1________
  244. Enter a number between 1 and 9: 10
  245. Invalid input
  246. To run the tests implement question2.sh to compile and run your question2.c.
  247.  
  248. Run test-Q2A.sh from the assignment directory:
  249.  
  250. bash test-Q2A.sh
  251. Run test-Q2A.sh from the assignment directory
  252.  
  253. bash test-Q2B.sh
  254. The test cases should produce no output whatsoever.
  255.  
  256. Marking
  257. 1 Point Program output is correct for a valid input. (Examples: test-q2a.sh)
  258. 1 Point Program output is correct for an invalid input. (Examples: test-q2b.sh)
  259. 1 Point Program uses idiomatic for loops to print the number triangles
  260. 1 Point Quality of question2.c meets the quality standards, listed above.
  261. 1 Point question2.sh meets the requirements above.
  262. Hints
  263. To check the return value of your program you can do echo $? in bash immediately after your program.
  264.  
  265. $ ./question2
  266. Enter a number between 1 and 9:
  267. lasjkdf
  268. Invalid input
  269. $ echo $?
  270. 1
  271. Your program returns whatever it returns from main. If you're not in main, you can have your program exit with a specific return value by using the exit function from stdlib.h. For example, exit(1) will cause the program to exit and return 1. return 1 in main will do the same thing.
  272.  
  273. Submission
  274. Test your program!
  275. Always test your code on the VM or a Lab computer before submitting!
  276.  
  277. Make a 1 line (excluding the comments and header) shell script for each question that will compile and run the C program for that question. Name the scripts question1.sh and question2.sh respectively. Make sure the program successfully compiles the program and then runs it. If the program doesn't compile it should not run the executable. The shell program should use 1 operator to achieve this and it should all fit on the same line. You can assume the shell script is run in the directory that contains both the source code and the executable. Run the test-Q1A.sh script for question1. Run the test-Q1B.sh script for question1. Run the test-Q2A.sh script for question2. Run the test-Q2B.sh script for question2. The scripts should produce no output.
  278.  
  279. Tar it up!
  280. Make a tar ball of your assignment. It should not be compressed. The tar name is __YOUR__CCID__-assignment1.tar
  281.  
  282. the tar ball should contain:
  283.  
  284. __YOUR__CCID__-assignment2/ # the directory
  285. __YOUR__CCID__-assignment2/README.md # this README filled out with your name, CCID, ID #, collaborators and sources.
  286. __YOUR__CCID__-assignment2/question1.c # C program
  287. __YOUR__CCID__-assignment2/question2.c # C program
  288. __YOUR__CCID__-assignment2/question1 # executable
  289. __YOUR__CCID__-assignment2/question2 # executable
  290. __YOUR__CCID__-assignment2/question1.sh # shell script
  291. __YOUR__CCID__-assignment2/question2.sh # shell script
  292. Dr. Hindle's assignment2 tar would be called hindle1-assignment2.tar and it will contain:
  293.  
  294. hindle1-assignment2/ # the directory
  295. hindle1-assignment2/README.md # this README filled out with Dr. Hindle's name, CCID, ID #, collaborators and sources.
  296. hindle1-assignment2/question1.c # C program
  297. hindle1-assignment2/question2.c # C program
  298. hindle1-assignment2/question1 # executable
  299. hindle1-assignment2/question2 # executable
  300. hindle1-assignment2/question1.sh # shell script
  301. hindle1-assignment2/question2.sh # shell script
  302. Submit it!
  303. Upload to eClass! Be sure to submit it to the correct section.
  304.  
  305. Marking
  306. This is a 10-point assignment. It will be scaled to 4 marks. (4% of your final grade in the course: A 10/10 is 100% is 4 marks.) Partial marks may be given at the TA's discretion.
  307.  
  308. You will lose all marks if not a tar (a .tar file that can be unpacked using tar -xf)
  309. You will lose all marks if files not named correctly and inside a correctly named directory (folder)
  310. You will lose all marks if README.md does not contain the correct information!
  311. Markdown format (use README.md in the example as a template)
  312. Name, CCID, ID #
  313. Your sources
  314. Who you consulted with
  315. The license statement below
  316. License
  317. This software is NOT free software. Any derivatives and relevant shared files are under the following license:
  318.  
  319. Copyright 2020 Abram Hindle, Hazel Campbell
  320.  
  321. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, and submit for grading and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  322.  
  323. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  324. You may not publish, distribute, sublicense, and/or sell copies of the Software.
  325. You may not share derivatives with anyone except UAlberta staff.
  326. You may not pay anyone to implement this assignment and relevant code.
  327. Paid tutors who work on this code owe the Department of Computing Science at the University of Alberta $10000 CAD per derivative.
  328. By publishing this code publicly said publisher owes the Department of Computing Science at the University of Alberta $10000 CAD.
  329. You must not engage in plagiarism
  330. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  331.  
  332.  
  333. Last modified: Friday, 17 January 2020, 7:27 PM
  334. Jump to...
  335. CMPUT 201 (Winter 2020 LAB LEC)
  336. Participants
  337. Grades
  338. General
  339. Guides and FAQs
  340. Week 1: January 6 to 10
  341. Week 2: January 13 to 17
  342. Week 3: January 20 to 24
  343. Week 4: January 27-31
  344. Week 5: February 3-7
  345. Week 6: February 10-14
  346. Reading Week: February 17-21
  347. Week 7: February 24-28
  348. Week 8: Mar 2-6
  349. Week 9: Mar 9-13
  350. Week 10: Mar 16-20
  351. Week 11: Mar 23-27
  352. Week 12: March 30-April 3
  353. Week 13: April 6-8
  354. Final Exam
  355. Dashboard
  356. Site home
  357. Calendar
  358. My courses
  359. WRS 102 (SEM 850 Wi20)
  360. PSYCO 105 (LEC B3 Wi20)
  361. MATH 125 (Winter 2020 LEC Q1 R1 S1)
  362. CMPUT 201 (Winter 2020 LAB LEC)
  363. CMPUT 175 (Fall 2019 LEC A1 A2)
  364. You are logged in as Laith Haddadin (Log out)
  365. CMPUT 201 (Winter 2020 LAB LEC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement