M4ritimeSeeker

PASS Week 12: Managing Files and Strings

Nov 14th, 2021
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 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. What's the prize you might be thinking? Bragging rights.
  9.  
  10. RULES
  11. 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,
  12. that sounds like no fun :(
  13.  
  14. 2) You must be able to explain to me in detail what the program does! Not just bits and pieces or a vague explanation.
  15. - You must also explain what the functions do. I can't have you just RUN the program and get off easy.
  16.  
  17. 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.
  18. - I won't pull any punches. If you're wrong I'm saying "Nah" and sending you back to the group. No Hints.
  19.  
  20. 4) I will send the pastebin with the mystery program right after debriefing on the game. Once you get the link, you may start.
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. PROBLEM #1: Argc and Argv
  41.  
  42. This problem involves using argc and argv for input and output. You will take
  43. several strings as input from the command line, and print them in reverse.
  44. Here are the steps:
  45.  
  46. 1) Write your main function with parameters "int argc" and "char* argv[]".
  47. - This program will take a string as input AT THE COMMAND LINE when the program is started.
  48. 2) Print the words in the string in reverse order.
  49. - You will need to use argc and argv.
  50.  
  51. Note: Recall that char* argv[] is an ARRAY of STRINGS. So each input string occupies
  52. a single slot in the array.
  53.  
  54.  
  55. SAMPLE OUTPUT:
  56.  
  57. > ./main here are my words [This is the command line. NOT your program output.]
  58.  
  59. words
  60. my
  61. are }- [These 5 lines are your program's output]
  62. here
  63. ./main
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. PROBLEM #2: Storing Strings and converting them into Float
  85.  
  86. This problem involves taking strings as input and converting them into floating point values.
  87. You will use a TWO-DIMENSIONAL array to store the strings. Here are the steps:
  88.  
  89. 1) Create a two-dimensional array of characters that can hold 10 strings up to a length of 20
  90. characters each. (It will be formatted as char[][], place the 10 and 20 in the proper brackets).
  91. 2) Declare and initalize a double called "sum" and set it to 0.
  92. 3) Prompt the user to enter in 10 numbers and store them into an array of chars (strings) one at a time.
  93. TREAT NUMERICAL INPUT AS STRINGS and store them into the array (You DON'T need to reference the 2nd dimension).
  94. 4) Use a loop to print out the strings to verify that the numbers were stored in the array successfully.
  95. 5) Now use a second loop through the array and use the atof() function to convert the string version of the numbers into a
  96. double, and then add it to a sum variable.
  97. 6) Print out the sum and end the program.
  98.  
  99. Note: scanf() will read in strings from standard in (stdin) until they encounter a
  100. space, end of file, or newline/carriage return. gets and fgets will include those items, which makes
  101. scanf a better choice here ( https://www.tutorialspoint.com/c_standard_library/c_function_atof.htm )
  102.  
  103.  
  104. SAMPLE OUTPUT:
  105.  
  106. Enter in a number: 1
  107. Enter in a number: 2
  108. Enter in a number: 3
  109. Enter in a number: 4
  110. Enter in a number: 5
  111. Enter in a number: 6
  112. Enter in a number: 7
  113. Enter in a number: 8
  114. Enter in a number: 9
  115. Enter in a number: 10
  116. 1
  117. 2
  118. 3
  119. 4
  120. 5
  121. 6
  122. 7
  123. 8
  124. 9
  125. 10
  126. Sum: 55.00
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. PROBLEM #3: Checking If a File Exists by Using it's Name
  144.  
  145. This problem involves using File pointers! Here are the steps:
  146.  
  147. 1) Prompt the user for the name of a text file (example: input.txt)
  148. - Use fscanf() to put the user input in a string.
  149. 2) Check if the file exists. Print the result to the user.
  150.  
  151.  
  152. SAMPLE OUTPUT (If file exists) SAMPLE OUTPUT (If file doesn't exist)
  153.  
  154. Enter your file: file.txt Enter in your file: file.txt
  155. File exists File doesn't exist
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. BONUS PROBLEM
  172.  
  173. This problem involves initalizing strings by explicitly allocating the memory using malloc(). Here are the steps:
  174.  
  175. 1) Declare an uninitialized string using pointer notation.
  176. -Example: char *str;
  177. 2) Use malloc() to allocate an array of size 15.
  178. -Example shown below.
  179. 3) Initialize the string to a word or phrase under 15 characters.
  180. 4) Print the string, and the address its stored in.
  181. -Use the "%u" specifier for printing the address.
  182. 5) Use realloc() to up the size of the array to 30.
  183. 6) Repeat steps 3 and 4 with a larger string.
  184.  
  185.  
  186. EXAMPLE: Allocating a string of size 40.
  187.  
  188. (char*)malloc(40);
  189.  
  190.  
  191. SAMPLE OUTPUT
  192.  
  193. String = Hereismystring; Address = 6709856
  194. String = Hereismystring, its larger now; Address = 6710928
Advertisement
Add Comment
Please, Sign In to add comment