Advertisement
M4ritimeSeeker

PASS Week 13: File Streams and Input

Apr 18th, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. Team Competition
  2.  
  3. Here is a fun game as the semester starts to wrap up! Your team's job is to find out what a mystery program does.
  4. If a team thinks they have the answer, have one designated member notify Justin and explain to me what it does.
  5. For the sake of no other teams overhearing, Justin and the member will talk outside the lab for them to explain.
  6. If they get it correct, their team wins.
  7. If not, I will send them back to their group to keep working with their team to figure out the program.
  8.  
  9. What's the prize you might be asking? Bragging rights.
  10.  
  11. RULES
  12. 1) All teams have 15 minutes. If no one gets it in that time, then I explain what the program does and no one gets bragging rights,
  13. that sounds like no fun :(
  14.  
  15. 2) You must be able to explain to me in detail what the program does! Not just bits and pieces or a vague explanation.
  16. - A GOOD METRIC is if you can explain generally what the program is doing, along with what function1 and function2 do.
  17. - YOU DON'T HAVE TO EXPLAIN THE FLUSH INPUT FUNCTION
  18.  
  19. 3) Each team has 2 attempts to explain the program to me. If they use all 2 and don't get it on either, they are disqualified.
  20. - I won't pull any punches. If you're wrong I'm saying "Nah" and sending you back to the group. No Hints.
  21.  
  22. 4) I will send the pastebin with the mystery program right after debriefing on the game. Once you get the link, you may start.
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. PROBLEM #1: Argc and Argv
  43.  
  44. This problem involves using argc and argv for input and output. You will take
  45. several strings as input FROM THE COMMAND LINE, and print them in reverse.
  46. Here are the steps:
  47.  
  48. 1) Write the main function itself with parameters "int argc" and "char* argv[]".
  49. - This program will take a string as input AT THE COMMAND LINE when the program is started.
  50. - This means, in the command line, you will type "./main [word1] [word2]", and hit enter. NOT THE RUN BUTTON.
  51. 2) Print the words in the string in reverse order.
  52. - You will need to use argc and argv.
  53.  
  54. Note: Recall that char* argv[] is an ARRAY of STRINGS. So each whole string occupies
  55. a single slot in the array.
  56.  
  57.  
  58. SAMPLE OUTPUT:
  59.  
  60. > ./main here are my words [This is the command line. NOT your program output.]
  61.  
  62. words
  63. my
  64. are }- [These 5 lines are your program's output]
  65. here
  66. ./main
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. PROBLEM #2: Checking ISBN Numbers
  90.  
  91. The International Standard Book Number, ISBN, is used to uniquely identify a book.
  92. It is made of 10 digits. Write a function that tests an ISBN to see if it is valid.
  93.  
  94. For an ISBN number to be valid, the weighted sum of the 10 digits must be evenly divisible by 11.
  95.  
  96. To determine the weighted sum, the value of each position is multiplied by its
  97. relative position, starting from the right, and the sum of the product is determined.
  98. If (weighted sum) modulus 11 is 0, the ISBN number is valid.
  99.  
  100. Test your function with examples, use Amazon to get valid book ISBNs.
  101.  
  102. Example
  103. ISBN: 0-07-881809-5 (assume no dashes in your function)
  104.  
  105. Code * Weight = Value
  106. 0 10 0
  107. 0 9 0
  108. 7 8 56
  109. 8 7 56
  110. 8 6 48
  111. 1 5 5
  112. 8 4 32
  113. 0 3 0
  114. 9 2 18
  115. 5 1 5
  116.  
  117. Weighted Sum is 220
  118.  
  119. 220 % 11 = 0 (Valid ISBN)
  120.  
  121.  
  122.  
  123.  
  124. SAMPLE OUTPUT (ISBN is 0078818095)
  125.  
  126. Enter in 10 numbers one at a time: 0
  127. Enter in 10 numbers one at a time: 0
  128. Enter in 10 numbers one at a time: 7
  129. Enter in 10 numbers one at a time: 8
  130. Enter in 10 numbers one at a time: 8
  131. Enter in 10 numbers one at a time: 1
  132. Enter in 10 numbers one at a time: 8
  133. Enter in 10 numbers one at a time: 0
  134. Enter in 10 numbers one at a time: 9
  135. Enter in 10 numbers one at a time: 5
  136. Weighted Sum is: 220
  137. The ISBN is valid
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. PROBLEM #3: Checking If a File Exists by Using it's Name
  155.  
  156. This problem involves using File pointers! Here are the steps:
  157.  
  158. 1) Prompt the user for the name of a text file (example: input.txt)
  159. - Use fscanf() to put the user input in a string.
  160. 2) Check if the file exists. Print the result to the user.
  161.  
  162.  
  163. SAMPLE OUTPUT (If file exists) SAMPLE OUTPUT (If file doesn't exist)
  164.  
  165. Enter your file: file.txt Enter in your file: file.txt
  166. File exists File doesn't exist
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. BONUS PROBLEM: Storing Strings and converting them into Float
  187.  
  188. This problem involves taking strings as input and converting them into floating point values.
  189. You will use a TWO-DIMENSIONAL array to store the strings. Here are the steps:
  190.  
  191. 1) Create a two-dimensional array of characters that can hold 10 strings up to a length of 20
  192. characters each. (It will be formatted as char[][], place the 10 and 20 in the proper brackets).
  193. 2) Declare and initalize a double called "sum" and set it to 0.
  194. 3) Prompt the user to enter in 10 numbers and store them into an array of chars (strings) one at a time.
  195. TREAT NUMERICAL INPUT AS STRINGS and store them into the array
  196. - You DON'T need to reference the 2nd dimension, but this part of the program is really weird, so I will give you the scanf.
  197. - scanf("%s", ary[i][0]);
  198. 4) Use a loop to print out the strings to verify that the numbers were stored in the array successfully.
  199. 5) Now use a second loop through the array and use the atof() function to convert the string version of the numbers into a
  200. double, and then add it to a sum variable.
  201. 6) Print out the sum and end the program.
  202.  
  203. Note: scanf() will read in strings from standard in (stdin) until they encounter a
  204. space, end of file, or newline/carriage return. gets and fgets will include those items, which makes
  205. scanf a better choice here ( https://www.tutorialspoint.com/c_standard_library/c_function_atof.htm )
  206.  
  207.  
  208. SAMPLE OUTPUT:
  209.  
  210. Enter in a number: 1
  211. Enter in a number: 2
  212. Enter in a number: 3
  213. Enter in a number: 4
  214. Enter in a number: 5
  215. Enter in a number: 6
  216. Enter in a number: 7
  217. Enter in a number: 8
  218. Enter in a number: 9
  219. Enter in a number: 10
  220. 1
  221. 2
  222. 3
  223. 4
  224. 5
  225. 6
  226. 7
  227. 8
  228. 9
  229. 10
  230. Sum: 55.00
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement