Lab 3 - History


Prerequisite Skills for This Lab


  • Able to read characters in from keyboard one line at a time.
  • C Skills
    • Able to create and use arrays.
    • Able to pass arrays to functions.
    • Able to manage pointers and data in an array.
    • Able to free dynamically allocated memory.
    • Able to compile, run, and debug C programs.


Your task is...


In a file name lab3.c, write a program that does the following:

  • Read in user input​ from the keyboard line by line; each input can include spaces or other characters.
  • Stores the last 5 lines of user inputs.
  • When the user enters the command print (no arguments), then print the last 5 lines of user input to the screen.
    • print would then be the most recent user input, and be shown as such in the output below.

Example output

Enter input: Hello world!
Enter input: yes
Enter input: print
Hello world!
yes
print
Enter input: And this is
Enter input: Just another input
Enter input: line
Enter input: here
Enter input: print
And this is
Just another input
line
here
print
Enter input:
Enter input: that was blank!
Enter input: see it!
Enter input: print
print

that was blank!
see it!
print
Enter input: ^C

(Ended with Ctrl+c)


1. Design (10 mins)


Design your solution.

  • Plan your solution using pseudocode.
  • Start by identifying what data your program need to store.
    • Hint - Data structure: What data structure would be best for storing 5 things of the same data type? Don't use 5 individual variables!
    • Hint - Pointer vs String: Input from the user will be a dynamically allocated string (from getline()). Would it be easier to have the history store a pointer for each user input, or to copy each input into pre-allocated strings (null-terminated arrays of characters)? Is there a maximum length for a user input?
    • Hint - Local vs Global: Will your data be stored in local or global variables? When in doubt, try making everything local and passing it between functions.
  • Use functions to break out non-trivial operations such as managing the input history.
    • Hint - Readability: Try to make main() read like a book: don't put complex code in main(), move it into well named functions.
  • How are you handling dynamically allocated memory?
    • Remember that getline() can allocate space for the strings. When do you free any dynamically allocated memory?
  • Discuss with your partner, or someone else, how your design will work.
  • Try not to Google it; you need these skills. Labs are for figuring it out, not for coping.


2. Implement it!


  • Write a couple lines of code then compile, run, debug. Repeat!
  • Do simplest thing first (read input and output it?). Then add functionality bit-by-bit.
  • Help others around you!


3. Reviewing (15 mins)


  • During the last 15 minutes of the in-person lab, TAs will show a sample solution.
  • TAs will talk through how the solution works and discuss its implementation.
  • Discussion Points
    • How does it actually work?
      • What syscalls / library functions does it use? How do those work? Use man page as needed.
    • How does it handle errors?
    • How could the design and/or code be improved? Look at data storage, functions, etc.
  • Now that you have seen the solution:
    • Finish implementing your solution: don't just copy the solution, but it is OK if you use the ideas you saw to help you finish.
    • Revise your solution to improve it based on what you learned.


4. Optional Challenges


  1. Optional: Refactor (cleanup) your code to improve its readability.
    • Review your main() function and look for ways to make it clearer.
    • Consider if any of your code could be made into functions that are easier to understand or call.
    • Consider how you are storing data. Can you think of any ways to make the code clearer?
    • Review another student's solution:
      • Suggest one or two thing you like about their code.
      • Suggest one or two things they could improve in their code.
      • Note: It is fine to show your lab code to another student, and it is OK for them to use ideas they learned from your code and your feedback.
    • Change your code using these ideas.
  2. Optional: Change your program to ignore blank lines of user input.
    • When the user enters a blank line, don't add it to the history.
  3. Optional: Add a clear command that will erase the current history.


Submission


Submit your lab3.c C code to CourSys; the file name must be an exact match to what CourSys is expecting, otherwise it won't accept it.

Submissions will be marked for completion. It must be valid C code that runs (however we are unlikely to actually compile and run the code). You do not need to complete any optional steps.