Skip to content

BlinkFTW/CS211_Lab4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

CS211_Lab4

Lab assignment FOUR from Programming for Engineers I in C

Setup

Create a new directory and copy skeleton.c, into it. Rename skeleton.c to lab4.c. Then, write a Makefile which will compile, assemble and link this file into an executable called "lab4". Do not forget to add "-Wall" to each gcc command. Then, run "make" from within that directory and be sure your Makefile works.

Part A

Create a function which can read in whole lines of input from the user, of any length (this means you MUST be able to break these up among multiple string buffers with scanf's "%[]" or read it in one character at a time with "%c" and build the string yourself). This function must use a temporary dynamic array to store this full line as a single string, and must actually return the string in a perfectly-allocated dynamic string. Lines will be separated (only) by newline characters. All other characters should be read and added into the string.

Write a new function that will use the previous function to read in any number of lines from the user until they enter the special line of a "." by itself. You should also use a temporary dynamic array of pointers for this, and also should perfectly size this array to point to just these individual strings, and terminate this array with a NULL. These lines should include all lines entered by the user, up to (but not including) the line which had just ".".

Add to your main some basic output explaining to the user how to enter lines, and then call the second function to get them. Then print out all of the lines, separated by newline characters.

Note: Be sure you don't reallocate your dynamic arrays too often. Each time one needs to be resised, you should double its capacity.

Note: Be careful not to have ANY memory leaks or dangling pointers!

Part B

Copy your lab4.c file to a new file called getline.c. Edit getline.c and remove all code from it except for the first function described in part A, and any utility functions it needs to call. Be sure this one function is now the only function defined in this code file which is not declared as "static". Add a header file called getline.h which declares just this one function.

Copy your lab4.c file to a new file called multiline.c. Edit multiline.c and remove all code from it except for the second function described in part A, and any utility functions it needs to call. Be sure this one function is now the only function defined in this code file which is not declared as "static". Add a header file called multiline.h which declares just this one function.

Remove the two functions from Part A from your lab4.c file (which should now only have the main function). Now use "#include" to include the two header files you just created. Update your Makefile to build object files from each of these three files, and link all three together to build the lab4 executable. Be sure you Makefile also takes into account the header files (which should be dependencies, but not included in the gcc command lines).

Use your updated Makefile to build your program again and be sure everything still works with each component now in a separate file.

Part C

Create another new set of C code and header files which provides three new functions, which work on data in the format generated by the second function from part A (arrays of lines). These functions should each do the following:

  1. Ask the user for an integer from 1 to the number of lines in the array of lines. If that line number exists (numbered from 1, not from 0), then remove that entire line from the array (making the array 1 line shorter).

  2. Allow the user to enter a new array of lines (you should use the second function you wrote in part A for this) and add these to the end of the current array of lines (making the array any number of lines longer).

  3. Get a single line of input from the user (you should use the first function you wrote in part A for this) and search for the text entered in an array of lines. Return as an "int" the line number (again, numbered from 1, not from 0) this entered sub-string is first found on, or 0 if it is not found at all. Note this is not just asking you to match a line, but perhaps match a part of a line. For example, if the a line in the array of lines contains "Mary had a little lamb", and the user asked to search for "a little", it would be found on that line.

Link these files into your program by updating your main function and Makefile. Also add a few tests of each function to your main function to confirm each is working correctly. These tests should all use the line data input by the user for part A.

About

Lab assignment FOUR from Programming for Engineers I in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages