Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Demo:
- The following link leads you to a program that utilizes argc, argv, multiple user defined functions,
- and header files. You can use this program to help out with any of the problems.
- https://replit.com/@LarrySnedden1/SeparateSources
- 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 number of numbers (elements) the user will enter and store this in a normal integer.
- 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() or fscanf() 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));
- PROBLEM #2: Using Argc, Argv, User-functions, and Calloc:
- Just a warning, this one is long. 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
- Remember this is the COMMAND LINE. If on Replit, you will need to click the green "Run" button to compile
- the code, but it will most likely return an error, a segmentation fault, or just nothing. After clicking
- the green "Run" button, you need to execute the command "./main [input 1] [input 2]" on the command line.
- As well, your program file must be named "main.c" for this to work.
- Now, here are the STEPS:
- 1) Construct a program that takes in TWO numbers as input from the command line using argc and argv.
- > The FIRST integer value to determine THE SIZE of an array.
- > The SECOND integer is a special value that will be used in step 3.
- 2) Use a user defined function to create the array using calloc.
- > Declare an initialize the array USING CALLOC.
- > Return the array itself.
- 3) Now create a second user function to populate the array using the second value entered.
- > This 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. (See sample output above)
- > Return void.
- 4) Finally, construct a final user function to print out the values stored in the array.
- > Return void.
- Your functions will need to pass a single dimensional array and the size of the array, or number of
- items.
- PROBLEM #3: 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 = 6709856
- String = Hereismystring, its larger now; Address = 6710928
- 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 sentences 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.
- 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