Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Demo (You don't have to code anything):
- The following link leads you to a program that utilizes argc, argv, multiple user defined functions,
- and header files: https://replit.com/@LarrySnedden1/SeparateSources
- NOTE: It will not compile using the scripted compiler commands in Repl.it. You must go to the
- console window and compile the code manually. Type the following “gcc -o program main.c” and
- then type “./program 10” to launch the executable named “program” and to give it input at startup.
- In this case the program should count by 2 from 0 ten times.
- Takeaways:
- 1. Note the MyFunctions.c source code and how the function descriptions advise you, the
- programmer on how to use them.
- 2. Note that the function headers in the header file match the function headers in the function
- source code file.
- PROBLEM #1: Using Calloc to create Arrays
- Create a program that declares an integer pointer that is initialized to NULL. Prompt the user
- for the amount of integers (elements) the user will enter, and store this in a normal integer variable.
- Then using the calloc() function, allocate an array of type integer to hold the number of elements the user provided.
- (Set the integer pointer you declared earlier equal to the calloc).
- Now, using scanf() read in the elements using a for loop and load the elements in the new
- array. Finally use a for loop to print the values back to screen, proving it worked.
- Example: Using calloc to get memory of x number of type int elements: (int*) calloc(x, sizeof(int));
- If you want more info on why calloc() or malloc() are better for declaring arrays, this post has some insight:
- https://www.quora.com/How-do-I-free-an-array-in-C
- PROBLEM #2: Using Malloc
- This problem involves initalizing strings by explicitly allocating the memory using malloc(). Here are the steps:
- 1) Declare an uninitialized string using pointer notation.
- -Example: char *str;
- 2) Use malloc() to allocate an array of size 15.
- -Example shown below.
- 3) Initialize the string to a word or phrase under 15 characters.
- 4) Print the string, and the address its stored in.
- -Use the "%p" specifier for printing the address.
- 5) Use realloc() to up the size of the array to 30.
- 6) Repeat steps 3 and 4 with a larger string.
- EXAMPLE: Allocating a string of size 40.
- (char*) malloc(40);
- SAMPLE OUTPUT
- String = Hereismystring; Address = 0x1ac7260
- String = Hereismystring, its larger now; Address = 0x1ac7690
- PROBLEM #3: Using Argc, Argv, User-functions, and Calloc:
- Just a warning, this one is a bit long, mainly cause of the amount of typing involved.
- This program will take two numbers as input from the command line and produce the following output:
- ./main 8 4
- 4 8 12 16 20 24 28 32
- The first number is the amount of numbers you will have printed total, and the second number is the interval
- each number will be incremented by.
- Your functions will need to pass a single dimensional array and the size of the array, or number of
- items.
- Construct a program that takes in two numbers from the command line using argc and argv.
- The program will use the first integer value to determine how large of an integer
- array to allocate using calloc. Use a user defined function to create the array and return it to the
- calling function. Make certain to test that the user has entered in the correct number of values at
- the program start. Test that your function works. Now create a second user function to populate
- the array using the second value entered. Your function will fill the array by the value entered
- increased by itself each loop. If the first value entered was 8 second value entered was 4, the array
- would be filled with 4, 8, 12, 16, 20, 24, 28, 32. Finally, construct a user function to print out the
- values stored in the array.
- PROBLEM #4: Using malloc with ragged arrays
- You are tasked with creating a program that will prompt a user to input 5 words(strings) and then
- your program will store the words in what is referred to as a ragged array. A ragged array is a
- 2-dimensional array where the length of the items stored in each row varies in size or length. In
- this case you will be storing strings in your array. Use malloc to allocate the memory to hold the
- string as you store them by using strlen() to determine the size you need. Since you know the
- array will be 5 strings, you can declare and define the array as an array of 5 pointers to type char.
- SAMPLE OUTPUT
- Enter in a series of 5 strings:
- these
- are
- some
- strings
- guacamole
- The value is these
- The value is are
- The value is some
- The value is strings
- The value is guacamole
- PROBLEM #5: Determine How Many Items are in a File
- You are tasked with writing a program that uses a user defined function to take an existing file name, open the
- file, examine the file and return the number of records (lines) in the file as well as the maximum length of
- the records (amount of characters on each line). For test input, create a text file and in that
- text file type in a few simple sentences, even perhaps a short poem. It is important that your
- function denotes that a line or record ends with a newline character. The fgets() function stops
- reading on newline characters or if a preset maximum number of items is read. Make the largest
- record 1024 characters.
- For this problem, you will need 2 files. One being the program, and the other being a plain text file.
- Make sure if you are on Replit, these files both exist in the same directory.
- For this example, assume there is a file called "test" with the following content inside it:
- this
- is
- a
- test
- file
- with
- text
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- SAMPLE OUTPUT:
- Enter the file name: test
- The file has 8 records and the largest is 40 characters in length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement