Advertisement
M4ritimeSeeker

PASS Week 4/14: Files and Memory

Apr 11th, 2021 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. Team Competition
  2.  
  3. Here is a fun game for our last session. 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 leave their breakout room and explain to me what it does.
  5. If they get it correct, their team wins, I break all of the rooms and the winning team gets bragging rights. If not, I
  6. will send them back into their room to keep working with their team to figure out the program.
  7.  
  8. RULES
  9. 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,
  10. that sounds like no fun :(
  11.  
  12. 2) You must be able to explain to me in detail what the program does! Not just bits and pieces or a vague explanation.
  13. - You must also explain what the functions do. I can't have you just RUN the program and get off easy.
  14.  
  15. 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 lose.
  16. - I won't pull any punches. If you're wrong I'm saying "Nah" and sending you back to the breakout room. No Hints.
  17.  
  18. 4) I will send the pastebin with the mystery program right after debriefing on the game. Enter your breakout room as soon as you
  19. get the pastebin link.
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. Problem #1
  30.  
  31. This problem involves using argc and argv for input and output. Program the following:
  32.  
  33. 1) Write your main function with parameters "int argc" and "char* argv[]".
  34. - This program should take a string as input AT THE COMMAND LINE when the program is started.
  35. 2) Print the words in the string in reverse order.
  36. - You will need to use argc and argv.
  37.  
  38. Note: Recall that char* argv[] is an array of STRINGS. So each input string occupies
  39. a single slot in the array.
  40.  
  41.  
  42. SAMPLE OUTPUT:
  43.  
  44. > ./main here are my words [This is the command line. NOT your program output.]
  45.  
  46. words -------\
  47. my \
  48. are - [These lines are your output, from "words" to "./main"]
  49. here /
  50. ./main -------/
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. Problem #2
  65.  
  66. This problem involves using File pointers! Program the following:
  67.  
  68. 1) Prompt the user for the name of a text file (example: input.txt)
  69. - Use fscanf() to put the user input in a string.
  70. 2) Check if the file exists. Print the result to the user.
  71.  
  72.  
  73. SAMPLE OUTPUT (If file exists) SAMPLE OUTPUT (If file doesn't exist)
  74.  
  75. Enter your file: file.txt Enter in your file: file.txt
  76. File exists File doesn't exist
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. Problem #3
  92.  
  93. This problem involves creating arrays by explicitly allocating the memory using calloc(). Program the following:
  94.  
  95. 1) Declare a pointer of type int and initialize it to NULL.
  96. 2) Prompt the user for the size of the array.
  97. - Doesn't matter the size as long as you don't put in a huge number (Cause you are gonna manually enter the numbers later).
  98. 3) Use calloc() and allocate the array based on the user input for the size.
  99. - Example code for how to do this provided below.
  100. 4) Fill the array with numbers supplied by the user. Do this one at a time.
  101. 5) Print the values to the screen to prove it worked.
  102. - Don't forget to use free() at the end.
  103.  
  104. Example: Using calloc to get memory of x number of type int elements.
  105.  
  106. (int*)calloc(x, sizeof(int));
  107.  
  108.  
  109.  
  110. SAMPLE OUTPUT
  111.  
  112. Enter the number of items: 5
  113. Enter values to put in the array:
  114. 1
  115. 2
  116. 3
  117. 4
  118. 5
  119. Here are the values in the array:
  120. 1 2 3 4 5
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. BONUS PROBLEM
  130.  
  131. This problem is similar to problem 3, but using malloc() instead. Program the following:
  132.  
  133. 1) Declare an uninitialized string using pointer notation.
  134. -Example: char *str;
  135. 2) Use malloc() to allocate an array of size 15.
  136. -Example shown below.
  137. 3) Initialize the string to a word or phrase under 15 characters.
  138. 4) Print the string, and the address its stored in.
  139. -Use the "%u" specifier for printing the address.
  140. 5) Use realloc() to up the size of the array to 30.
  141. 6) Repeat steps 3 and 4 with a larger string.
  142.  
  143. Example: Allocate a string of size 40.
  144.  
  145. (char*)malloc(40);
  146.  
  147.  
  148. SAMPLE OUTPUT
  149.  
  150. String = Hereismystring; Address = 6709856
  151. String = Hereismystring, its larger now; Address = 6710928
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement