Advertisement
M4ritimeSeeker

PASS Week 13: Managing Memory and More Files

Nov 30th, 2021 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. Program Demo:
  2. The following link leads you to a program that utilizes argc, argv, multiple user defined functions,
  3. and header files. You can use this program to help out with any of the problems.
  4.  
  5. https://replit.com/@LarrySnedden1/SeparateSources
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. PROBLEM #1: Using Calloc to create Arrays
  22.  
  23. Create a program that declares an integer pointer that is initialized to NULL. Prompt the user
  24. for the number of numbers (elements) the user will enter and store this in a normal integer.
  25. Then using the calloc() function, allocate an array of type integer to hold the number of elements the user provided.
  26. (Set the integer pointer you declared earlier equal to the calloc).
  27. Now, using scanf() or fscanf() read in the elements using a for loop and load the elements in the new
  28. array. Finally use a for loop to print the values back to screen, proving it worked.
  29.  
  30. Example: Using calloc to get memory of x number of type int elements: (int*) calloc(x, sizeof(int));
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. PROBLEM #2: Using Argc, Argv, User-functions, and Calloc:
  47.  
  48. Just a warning, this one is long. This program will take two numbers as input from the command line
  49. and produce the following output:
  50.  
  51. ./main 8 4
  52. 4 8 12 16 20 24 28 32
  53.  
  54. Remember this is the COMMAND LINE. If on Replit, you will need to click the green "Run" button to compile
  55. the code, but it will most likely return an error, a segmentation fault, or just nothing. After clicking
  56. the green "Run" button, you need to execute the command "./main [input 1] [input 2]" on the command line.
  57. As well, your program file must be named "main.c" for this to work.
  58.  
  59. Now, here are the STEPS:
  60. 1) Construct a program that takes in TWO numbers as input from the command line using argc and argv.
  61. > The FIRST integer value to determine THE SIZE of an array.
  62. > The SECOND integer is a special value that will be used in step 3.
  63. 2) Use a user defined function to create the array using calloc.
  64. > Declare an initialize the array USING CALLOC.
  65. > Return the array itself.
  66. 3) Now create a second user function to populate the array using the second value entered.
  67. > This function will fill the array by the value entered increased by itself each loop.
  68. > If the first value entered was 8 second value entered was 4, the array
  69. would be filled with 4, 8, 12, 16, 20, 24, 28, 32. (See sample output above)
  70. > Return void.
  71. 4) Finally, construct a final user function to print out the values stored in the array.
  72. > Return void.
  73.  
  74. Your functions will need to pass a single dimensional array and the size of the array, or number of
  75. items.
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. PROBLEM #3: Using Malloc
  88.  
  89. This problem involves initalizing strings by explicitly allocating the memory using malloc(). Here are the steps:
  90.  
  91. 1) Declare an uninitialized string using pointer notation.
  92. -Example: char *str;
  93. 2) Use malloc() to allocate an array of size 15.
  94. -Example shown below.
  95. 3) Initialize the string to a word or phrase under 15 characters.
  96. 4) Print the string, and the address its stored in.
  97. -Use the "%p" specifier for printing the address.
  98. 5) Use realloc() to up the size of the array to 30.
  99. 6) Repeat steps 3 and 4 with a larger string.
  100.  
  101.  
  102. EXAMPLE: Allocating a string of size 40.
  103.  
  104. (char*)malloc(40);
  105.  
  106.  
  107. SAMPLE OUTPUT
  108.  
  109. String = Hereismystring; Address = 6709856
  110. String = Hereismystring, its larger now; Address = 6710928
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. PROBLEM #4: Using malloc with ragged arrays
  129.  
  130. You are tasked with creating a program that will prompt a user to input 5 words(strings) and then
  131. your program will store the sentences in what is referred to as a ragged array. A ragged array is a
  132. 2-dimensional array where the length of the items stored in each row varies in size or length. In
  133. this case you will be storing strings in your array. Use malloc to allocate the memory to hold the
  134. string as you store them by using strlen() to determine the size you need. Since you know the
  135. array will be 5 strings, you can declare and define the array as an array of 5 pointers to type char.
  136.  
  137. SAMPLE OUTPUT
  138.  
  139. Enter in a series of 5 strings:
  140. these
  141. are
  142. some
  143. strings
  144. guacamole
  145.  
  146. The value is these
  147. The value is are
  148. The value is some
  149. The value is strings
  150. The value is guacamole
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. PROBLEM #5: Determine How Many Items are in a File
  166.  
  167. You are tasked with writing a program that uses a user defined function to take an existing file name, open the
  168. file, examine the file and return the number of records (lines) in the file as well as the maximum length of
  169. the records (amount of characters on each line). For test input, create a text file and in that
  170. text file type in a few simple sentences, even perhaps a short poem. It is important that your
  171. function denotes that a line or record ends with a newline character. The fgets() function stops
  172. reading on newline characters or if a preset maximum number of items is read. Make the largest
  173. record 1024 characters.
  174.  
  175.  
  176. For this problem, you will need 2 files. One being the program, and the other being a plain text file.
  177. Make sure if you are on Replit, these files both exist.
  178. For this example, assume there is a file called "test" with the following content inside it:
  179.  
  180. this
  181. is
  182. a
  183. test
  184. file
  185. with
  186. text
  187. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  188.  
  189.  
  190. SAMPLE OUTPUT:
  191. Enter the file name: test
  192. The file has 8 records and the largest is 40 characters in length
  193.  
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement