Advertisement
M4ritimeSeeker

PASS Week 13: Malloc, Calloc, and More Files

Apr 26th, 2022
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. Program Demo (You don't have to code anything):
  2. The following link leads you to a program that utilizes argc, argv, multiple user defined functions,
  3. and header files: https://replit.com/@LarrySnedden1/SeparateSources
  4.  
  5. NOTE: It will not compile using the scripted compiler commands in Repl.it. You must go to the
  6. console window and compile the code manually. Type the following “gcc -o program main.c” and
  7. then type “./program 10” to launch the executable named “program” and to give it input at startup.
  8. In this case the program should count by 2 from 0 ten times.
  9.  
  10. Takeaways:
  11. 1. Note the MyFunctions.c source code and how the function descriptions advise you, the
  12. programmer on how to use them.
  13. 2. Note that the function headers in the header file match the function headers in the function
  14. source code file.
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. PROBLEM #1: Using Calloc to create Arrays
  33.  
  34. Create a program that declares an integer pointer that is initialized to NULL. Prompt the user
  35. for the amount of integers (elements) the user will enter, and store this in a normal integer variable.
  36. Then using the calloc() function, allocate an array of type integer to hold the number of elements the user provided.
  37. (Set the integer pointer you declared earlier equal to the calloc).
  38. Now, using scanf() read in the elements using a for loop and load the elements in the new
  39. array. Finally use a for loop to print the values back to screen, proving it worked.
  40.  
  41. Example: Using calloc to get memory of x number of type int elements: (int*) calloc(x, sizeof(int));
  42.  
  43. If you want more info on why calloc() or malloc() are better for declaring arrays, this post has some insight:
  44. https://www.quora.com/How-do-I-free-an-array-in-C
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. PROBLEM #2: Using Malloc
  66.  
  67. This problem involves initalizing strings by explicitly allocating the memory using malloc(). Here are the steps:
  68.  
  69. 1) Declare an uninitialized string using pointer notation.
  70. -Example: char *str;
  71. 2) Use malloc() to allocate an array of size 15.
  72. -Example shown below.
  73. 3) Initialize the string to a word or phrase under 15 characters.
  74. 4) Print the string, and the address its stored in.
  75. -Use the "%p" specifier for printing the address.
  76. 5) Use realloc() to up the size of the array to 30.
  77. 6) Repeat steps 3 and 4 with a larger string.
  78.  
  79.  
  80. EXAMPLE: Allocating a string of size 40.
  81.  
  82. (char*) malloc(40);
  83.  
  84.  
  85. SAMPLE OUTPUT
  86.  
  87. String = Hereismystring; Address = 0x1ac7260
  88. String = Hereismystring, its larger now; Address = 0x1ac7690
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. PROBLEM #3: Using Argc, Argv, User-functions, and Calloc:
  113.  
  114. Just a warning, this one is a bit long, mainly cause of the amount of typing involved.
  115. This program will take two numbers as input from the command line and produce the following output:
  116.  
  117. ./main 8 4
  118. 4 8 12 16 20 24 28 32
  119.  
  120. The first number is the amount of numbers you will have printed total, and the second number is the interval
  121. each number will be incremented by.
  122. Your functions will need to pass a single dimensional array and the size of the array, or number of
  123. items.
  124.  
  125. Construct a program that takes in two numbers from the command line using argc and argv.
  126. The program will use the first integer value to determine how large of an integer
  127. array to allocate using calloc. Use a user defined function to create the array and return it to the
  128. calling function. Make certain to test that the user has entered in the correct number of values at
  129. the program start. Test that your function works. Now create a second user function to populate
  130. the array using the second value entered. Your function will fill the array by the value entered
  131. increased by itself each loop. If the first value entered was 8 second value entered was 4, the array
  132. would be filled with 4, 8, 12, 16, 20, 24, 28, 32. Finally, construct a user function to print out the
  133. values stored in the array.
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. PROBLEM #4: Using malloc with ragged arrays
  160.  
  161. You are tasked with creating a program that will prompt a user to input 5 words(strings) and then
  162. your program will store the words in what is referred to as a ragged array. A ragged array is a
  163. 2-dimensional array where the length of the items stored in each row varies in size or length. In
  164. this case you will be storing strings in your array. Use malloc to allocate the memory to hold the
  165. string as you store them by using strlen() to determine the size you need. Since you know the
  166. array will be 5 strings, you can declare and define the array as an array of 5 pointers to type char.
  167.  
  168. SAMPLE OUTPUT
  169.  
  170. Enter in a series of 5 strings:
  171. these
  172. are
  173. some
  174. strings
  175. guacamole
  176.  
  177. The value is these
  178. The value is are
  179. The value is some
  180. The value is strings
  181. The value is guacamole
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. PROBLEM #5: Determine How Many Items are in a File
  197.  
  198. You are tasked with writing a program that uses a user defined function to take an existing file name, open the
  199. file, examine the file and return the number of records (lines) in the file as well as the maximum length of
  200. the records (amount of characters on each line). For test input, create a text file and in that
  201. text file type in a few simple sentences, even perhaps a short poem. It is important that your
  202. function denotes that a line or record ends with a newline character. The fgets() function stops
  203. reading on newline characters or if a preset maximum number of items is read. Make the largest
  204. record 1024 characters.
  205.  
  206.  
  207. For this problem, you will need 2 files. One being the program, and the other being a plain text file.
  208. Make sure if you are on Replit, these files both exist in the same directory.
  209. For this example, assume there is a file called "test" with the following content inside it:
  210.  
  211. this
  212. is
  213. a
  214. test
  215. file
  216. with
  217. text
  218. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  219.  
  220.  
  221. SAMPLE OUTPUT:
  222. Enter the file name: test
  223. The file has 8 records and the largest is 40 characters in length
  224.  
  225.  
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement