Project 1_ CSCI 2021 (010) Machine Architecture and Organization (Spring 2024)

pdf

School

University of Minnesota-Twin Cities *

*We aren’t endorsed by this school

Course

2021

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

17

Uploaded by ryansmith_0527

Report
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 1/17 Project 1 Quiz Due Date: 11:59pm, 02/02/2024 (Complete on Canvas) Final Submission Due Date: 11:59pm, 02/12/2024 (Submit to Gradescope) 8.75% of Course Grade Projects are individual work. No collaboration with other students is allowed. Starter Code: proj1-code.zip (https://csci2021-sp24.s3.amazonaws.com/proj1-code.zip) Project Quiz is here (https://canvas.umn.edu/courses/413046/quizzes/866563) . How to Succeed on this Project 1. Read the entirety of this specification in detail. It contains a lot of information that will make the project go more smoothly if you take the time to understand it before starting the project. 2. Know the purpose of each file in the starter code. Read the provided header file(s). You will only need to modify two files to complete this project, but you should read this specification to understand why the other files are present. In particular, understand which files you are or are not allowed to modify to avoid autograder issues. 3. Know how to run your code independently of the Makefile we provide. The results you see from make test will not be enough to tell you what is going wrong with your code. You will need to be able to test out your code manually to figure out precisely where it may be producing undesirable results. 4. Expect to get stuck, and exercise patience and persistence. Often, the act of writing a sophisticated computer program will take time and critical thinking. Do not expect solutions to bugs to be immediately clear to you. Getting stuck like this is a normal part of the learning process in STEM fields. We are happy to help in office hours but will not give out answers. We can only point you in the right direction or suggest helpful examples to consider. 5. Start early. Give yourself time to get stuck, discover insights about what is going wrong, and overcome obstacles. It can be hard to think creatively about solutions to programming issues when you feel the pressure of an imminent deadline. 6. If you have questions, your best option is to ask in a public Piazza post. This gets your question in front of as many people as quickly as possible and also lets others benefit by seeing the answer to your question. 7. Familiarize yourself with the late submission policy detailed in the syllabus (https://canvas.umn.edu/courses/413046/assignments/syllabus) so you are not caught off guard. No submissions are accepted more than 48 hours after the deadline. Introduction
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 2/17 Basic application programming in C is an essential step downward toward the lower levels of computing. This project explores several fundamental aspects of getting work done in C: Dynamic memory management with malloc() and free() Reading and writing files in both text and binary formats Displaying information to the screen Reading commands from the user in interactive programs Building data structures with the C struct primitive and pointers You will implement a simple Contacts Log Program (i.e., a system for recording contact information about a group of people) based on a hash table data structure. You will also implement a command- line interface that allows a user to interact with this data structure by adding new contacts, searching for existing contact information, printing all contacts, and reading/writing contact data to/from both text and binary files. Grading Criteria Credit for this assignment will be based on the following categories: Project Quiz (10%): A quiz on this assignment specification and the project starter code has been posted on Canvas. Complete the quiz by the due date. The quiz is to be completed individually but multiple attempts are allowed. Automated Testing (50%): We have provided several tests along with instructions on how to run these tests from the command line. To pass the tests, you will need to ensure that your code compiles and runs according to the given descriptions. In particular, the output from your program must exactly match the expected output, so make sure to verify that your code works as expected. Manual Inspection (40%): Graders will be manually reviewing your code to check for certain features – mainly that you have properly implemented the expected solution and that your code adheres to a reasonable structure and style. Getting Started Take the following steps to get started on this project: 1. Read the rest of this document carefully and thoroughly. It explains what you should do and how your code is expected to behave. Reading this will help you write correct code earlier rather than later, saving you time on debugging. 2. Download the starter code for this project, linked at the top of this page. Unzip it and examine the provided code. Pay particular attention to the comments we have included. These describe important aspects of the starter code and also include some guidance on how to approach the project. 3. Complete the project quiz on Canvas by the deadline.
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 3/17 4. Get coding. Don’t wait too long before starting as this will only make the project more stressful for you and may result in a late submission. 5. Ask questions. If it’s not clear how to proceed, post to Piazza or visit office hours. Makefile A Makefile is provided as part of this project, much like what you have seen in the lab assignments. Building programs in C can be tedious, and most people use build systems, of which make is the oldest. The instructions and dependencies to create programs are written in a Makefile which is then interpreted by the make program. This program invokes gcc and other commands on your behalf. The Makefile for this project supports the following commands: make : Compile all code, produce an executable contacts_main program make clean : Remove all compiled items, useful if you want to recompile everything from scratch make clean-tests : Removes all files created during execution of the tests make zip : Create a zip file for submission to Gradescope make test : Run all test cases make test testnum=5 : Run test case #5 only make test testnum=4-8 : Run test cases #4 through #8 (inclusive) only make test testnum="4,8,15" : Run test cases #4, #8, and #15 only. Note the enclosing double quotes. Automated Tests Automated tests are included with the project’s starter code. These tests are known to work on lab machines only but in most cases they should run identically in x86-64 Linux environments such as the Windows Subsystem for Linux or a virtual machine. When debugging, you are encouraged to make use of the testnum argument as shown in the make command above to run only a specific test case you’re having trouble with, both to limit the amount of output you have to dig through and to reduce the time it takes to produce test results. Running all of the tests every time will be slow and painful. Manually Running Your Code The tests can help guide you to a correct solution, but they are not enough for productive debugging. Avoid the habit of spamming make test without understanding how your program works and thinking critically about what you are changing in your code. This usually causes you to take more time to complete the project overall. Once your code is compiled (e.g, by running make ), you can run your program from the command line like so:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 4/17 jhkolb@login01:~/csci2021-sp24/proj1-code$ ./contacts_main CSCI 2021 Contact Log System Commands: create <name>: creates a new log with specified name log: shows the name of the active contacts log add <name> <phone> <zip>: adds a new contact lookup <name>: searches for a phone number by contact name clear: resets current contacts log print: shows all contacts in active log write_text: saves all contacts to text file read_text <file_name>: loads contacts from text file write_bin: saves all contacts to binary file read_bin <file_name>: loads contacts from binary file exit: exits the program contacts> ... The contacts_main program always starts by printing out a list of available commands (most of which you will have to implement to complete the project), printing out a prompt ( contacts> ) to notify the user that they should type in a command, and then reading the user’s input from the terminal once it has been entered. At this point, you are free to type in whatever sequence of commands you like. Testing out a scenario where you devise input for your program, predict what you expect your program to do, and then observe how its actual behavior compares to your prediction is an important part of the debugging process. Manual Grading Criteria Overall 1 point: Reasonably short functions (Nothing over 50 lines is typically needed) 3 points: Follows some consistent style and indentation scheme, no ugly regions of code with closing or opening braces at same indentation level dict_free 2 points: Properly frees all contacts log contents and the contacts log structure itself add_contact 2 points: Checks for invalid phone number and returns an error if needed 2 points: Checks for invalid zip code and returns an error if needed 1 point: Properly uses provided hash function to determine bucket for new contact based on name 1 point: Properly adds new node to end of the relevant linked list 1 point: Properly initializes data values for new linked list node find_phone_number 1 point: Properly uses provided hash function to determine bucket for provided name 1 point : Traverses relevant linked list to find matching contact name
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 5/17 1 point: Correct string handling to find matching name 1 point: Returns correct value whether or not contact is found print_contacts_log 1 point : Properly traverses all hash table buckets 1 point: Properly traverses each bucket’s linked list 1 point: Prints out contact information in expected format read_contacts_log_from_text 1 point: Proper function call and arguments for opening file 1 point: Handles errors when opening file 1 point: Sets new contacts log’s log_name based on file name 1 point: Valid logic for reading in contacts log size 2 points: Valid logic for reading in contacts log contents 1 point: Correctly creates new contacts log and initializes its contents 1 point: Closes file when done write_contacts_log_to_binary 1 point: Proper function call and arguments for opening file 1 point: Determines destination file name from log’s log_name 1 point: Handles errors when opening file 1 point: Valid logic for writing out contacts log size 2 point: Valid logic for writing out log contents 1 point: Closes file when done read_contacts_log_from_binary 1 point: Proper function call and arguments for opening file 1 point: Handles errors when opening file 1 point: Correctly sets new log’s log_name based on file name 1 point: Valid logic for reading in log size 2 points: Valid logic for reading in contacts log contents 1 point: Properly initializes memory to avoid valgrind errors 1 point: Correctly creates new contacts log and initializes its entries 1 point: Closes file when done contacts_main 1 point: Clear structure to main loop, selecting a command to execute, then executing that command 1 point: Clear effort to free contacts log after end of loop and when currently active contacts log is cleared 2 points: Command-line arguments. Supports reading in contacts log from specified text or binary file.
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 6/17 1 point: Command-line arguments. Prints out error if unknown file extension is encountered as input. Starter Code Download the zip file linked at the top of this page to obtain the starter code for this project. You should see the following files: File Purpose Notes Makefile Build Build file to compile code and run test cases contacts.h Provided Header file for the contacts log data structure. Make sure to read over this, as there is lots of helpful information in the comments. contacts.c Edit Implementations of the contacts log functions. We have given you a starting point, but you will need to fill in most of this file. contacts_main.c Edit The command-line interface for interacting with a contacts log. We have given you a starting point, but you will need to fill in most of this file. test_cases/ Testing Directory containing the specification of all tests cases, their inputs, and the expected outputs for your program testius Testing Script to run test cases University.txt Testing Sample file used for testing your contacts log Directory.bin Testing Sample file used for testing your contacts log You are only allowed to modify the contacts.c and contacts_main.c files. The Gradescope autograder will use unaltered versions of the remaining files when testing your code for grading purposes. Note that the starter code should successfully compile, but it will not yet pass the automatic tests. Implementing the Contacts Log Note: You are welcome (and encouraged) to use any of the functions you’d like from the string.h library. Completing the Hash Table Implementation First, you will need to complete the implementation of the hash table data structure by filling in the functions in contacts.c . The contacts log stores the following information for each contact:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 7/17 A name (e.g., Louise ) A phone number (e.g., 6126254002 ) A zip code (e.g., 55455 ) Note that for phone numbers we store only the digits of the number and not any other symbols sometimes used in written representations of phone numbers. For example, we store 6126254002 and not 612-625-4002 . This allows use to use a numerical data type for each phone number rather than a string. Assume for this project that no phone numbers start with 0 . Zip codes are stored similarly, as a numerical data type and not as a string. Assume for this project that no zip codes start with 0 . The use of a hash table allows one to search the contacts log efficiently for an existing contact (by name) and allows efficient addition of new contacts to the log. Make sure you review how a hash table data structure is designed and used if you need a refresher on these topics. Perhaps the most important concept to recall is that a hash table relies on a hash function to map a data value (in our case, a contact’s name) to a storage location (or bucket ) in the table (in our case, an array index). Start in contacts.h and read it carefully. This contains the C struct definition for both the contacts log and each node stored in the hash table. The contacts log stores a log_name string and an array of hash table buckets . The number of buckets in the table (and thus the number of entries in this array) is determined by the NUM_BUCKETS constant defined near the top of contacts.h . Finally, the contacts log also keeps track of the total number of entries it contains with the size struct field. We will be implementing a hash table with chaining , a technique to deal with collisions in the hash function (i.e., the situation where two different contact names hash to the same bucket). What this means is that each element in our hash table’s underlying array can store multiple values. We will do this by making each bucket in our hash table a linked list, capable of storing arbitrarily many table entries. Therefore, you could view the hash table as an array of linked lists, where each element in the array is a pointer to the head of a linked list as we have seen in Lab 2. When the hash table is first created, all of its buckets (i.e., all of the internal linked lists) are empty. This means each element of the buckets array is NULL to indicate an empty list, and size is 0 . Each time a new item is added to a bucket in the hash table, it is appended to the end of the bucket's linked list. Below is a diagram of a contacts log with the name Directory which holds no contact information, meaning its internal hash table is empty.
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 8/17 As a second illustrative example, say we have a very small hash table with only 3 buckets and we add the following contacts to our hash table. Note that each contact’s placement in the hash table depends on the hash value of its name. We have already provided a hash function for you to use in contacts.h . Make sure to use our provided hash function. Otherwise, you may get inconsistent results that will cause you to fail the automated tests. Name Phone Number Zip Code hash(Name) Louise 6126254002 55455 2 Desmond 9163777000 95051 0 Hurley 6516358273 55113 1 Sun 5413450354 97401 2 In particular, note that both Louise and Sun both happened to hash to 2 , meaning buckets[2] will be a 2-element linked list to deal with the collision. More precisely, buckets[2] is a pointer to the head (first node) of this linked list (so you can think of buckets as an array of pointers, each pointing to the head node of that bucket’s linked list, or NULL if the list is empty).
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 9/17 The diagram below focuses only on the buckets field of the contacts log struct. The other fields are still present, they are just not shown in the picture. Note that Sun is at the end of the list for buckets[2] because the contact information for Sun was added to the hash table after the contact information for Louise was previously added. You may assume that contacts log names do not contain spaces. You may also assume that each contact in the log has a unique name, also without spaces. The contacts.h file declares the following functions. Make sure to read over the file itself for full details. create_contacts_log : Create a new contacts log. Already completed for you. get_contacts_log_name : Retrieve the log’s assigned name add_contact : Add a new contact to the log find_phone_number : Search for a phone number based on a contact’s name print_contacts_log : Display all contacts stored in the log on the screen free_contacts_log : Deallocate any memory used for the contacts log write_contacts_log_to_text : Save the contacts log to a text file. Already implemented for you. read_contacts_log_from_text : Load a contacts log from a text file write_contacts_log_to_binary : Save the contacts log to a binary file read_contacts_log_from_binary : Load a contacts log from a binary file You will perform your work for this part of the project in the contacts.c file, which contains all definitions for the functions declared in contacts.h . We have already provided the implementation of create_contacts_log for you. The remaining functions are marked as unimplemented and are left for you to complete.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 10/17 You are welcome to add your own helper functions to contacts.c to use in the required functions listed above. Make sure you add any helper functions to contacts.c , not contacts.h (which you are not allowed to modify) to avoid compiler errors and autograder issues. Remember that, unlike in Java, C requires you to declare a function above wherever it is called in your code. Contacts Log File Formats You will be expected to adhere to specific formats for your contacts log text and binary files. We will test this by checking how your program behaves when given pre-made text and binary contacts log files that we have included with the starter code. We will not expect you to deal with incorrectly formatted files in this project. You can assume that all files follow the rules given below, including the files we have provided. Text Files 1. The first line of the contacts log file should contain an unsigned integer representing the size of the log (the total number of contacts the log contains). 2. Each of the following lines should contain a string representing a contact: the contact’s name, the contact’s phone number, and the contact’s zip code each separated by a single space. The contacts log shown in the second diagram above would be stored in a text file with the following contents: 4 Louise 6126254002 55455 Desmond 9163777000 95051 Hurley 6516358273 55113 Sun 5413450354 97401 Binary Files Remember, binary files do not have “words” or “lines” – they are simply a sequence of bytes. 1. A contacts log binary file should start with a single unsigned integer representing the size of the contacts log (the total number of contacts the log contains). 2. Next, the file contains a sequence of bytes for each contact as follows: 1. The first bytes represent an unsigned with the length of the contact’s name (not counting the null terminator). 2. The next bytes are the characters in the contact’s name. The minimum number of bytes is used – there is no null terminator or any extra space. For example, "Sun" occupies 3 bytes of space, while "Louise" occupies 6 bytes of space in the file. 3. Next are bytes representing an unsigned long for the contact’s phone number. 4. Finally, there are bytes representing an unsigned value, the contact’s zip code.
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 11/17 The contacts log shown in the second diagram above would be stored in a binary file with the following contents (replacing each item in angle brackets with its actual binary representation): <size (4)> <name length 1 (6)> <name 1 ("Louise")> <phone number 1 (6126254002)> <zip code 1 (55455)> <name length 2 (7)> <name 2 ("Desmond")> <phone number 2 (9163777000)> <zip code 2 (95051)> <name length 3 (6)> <name 3 ("Hurley")> <phone number 3 (6516358273)> <zip code 3 (55113)> <name length 4 (3)> <name 4 ("Sun")> <phone number 4 (5413450354>) <zip code 4 (97401)> Note: You will need to be particularly attentive to memory when reading from binary files. You will need to manually null-terminate the string that is read in from the file (since the null terminator is not stored in the file), and you will need to initialize all elements in the character array you use to store a contact’s name so that valgrind does not report an error. Completing the Command Line Interface The contacts log features a command-line interface that is similar to the application you explored in Lab 2. A good way to prepare for this portion of the project is to review the code in Lab 2. There is one important change for this project, however: the command-line interface always tracks a single active contacts log, and the user is allowed to change from one active contacts log to another during a single execution of the contacts_main program. When the application is first started, there is no active contacts log, so the user is not allowed to perform operations like adding a new contact, searching for a phone number, or writing contacts to a file. The user can either create a new active (and empty) contacts log with the create command or load in a contacts log from a text or binary file. Once an active contacts log is set, the user is free to perform operations against it by typing in commands. Similarly, if there is a currently active contacts log instance, the user cannot switch to a new instance without first removing the old instance via the clear command. Here are the details on each command your application is expected to support. create <name> Creates a new active (and empty) contacts log instance with the specified name. For example: contacts> create School log Prints the name of the currently active contacts log on the screen. For example: contacts> log School If there is no currently active contacts log, then an error message is shown on the screen as follows:
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 12/17 contacts> log Error: You must create or load a contacts log first add <name> <phone> <zip> Adds a new contact to the contacts log. You can assume that all contacts added to the log have unique names (i.e., you do not need to worry about duplicate names). For example: contacts> add Louise 6126254002 55455 If the phone number provided is outside of the valid range (between 1000000000 and 9999999999 ), then an error message is shown on the screen as follows: contacts> add Louise 0 55455 Error: Invalid phone number and/or zip code If the zip code provided is outside of the valid range (between 10000 and 99999 ), then an error message is shown on the screen as follows: contacts> add Louise 6126254002 0 Error: Invalid phone number and/or zip code If there is no currently active contacts log, then an error message is shown on the screen as follows: contacts> add Louise 6126254002 55455 Error: You must create or load a contacts log first lookup <name> Searches the active contacts log for a specific contact, based on the contact’s name. If a contact with a matching name is found, their phone number is displayed on the screen like so: contacts> lookup Louise Louise: 6126254002 If no contact under the specific name is found, a message is displayed on the screen as follows: contacts> lookup Bueller No phone number for 'Bueller' found If there is no currently active contacts log, then an error message is shown on the screen as follows: contacts> lookup Louise Error: You must create or load a contacts log first clear
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 13/17 Discards the currently active contacts log (meaning there will be no active contacts log after this command) and frees all memory associated with the currently active contacts log. If there is no active contacts log, then an error message is shown on the screen as follows: contacts> clear Error: No contacts log to clear print Displays all contacts stored in the contacts log to the screen. You can just iterate through each bucket in the hash table, and for each bucket iterate though the nodes in its linked list. For example: contacts> print All contacts in Work: Name: Hurley, Phone Number: 6516358273, Zip Code: 55113 Name: Sun, Phone Number: 8553719460, Zip Code: 90210 Name: Louise, Phone Number: 6126254002, Zip Code: 55455 Name: Jin, Phone Number: 5413450354, Zip Code: 97401 Name: Desmond, Phone Number: 9163777000, Zip Code: 95051 If there is no currently active contacts log, then an error message is shown on the screen as follows: contacts> print Error: You must create or load a contacts log first write_text Writes all contacts stored in the contacts log to a text file. The name of this file is based on the contacts log’s name. For example, a contacts log with the name "School" is saved in the file School.txt in the current working directory. Upon success, a message is printed to the screen as follows. contacts> write_text Contacts log successfully written to School.txt If the write to the file fails, an error message is printed to the screen as follows: contacts> write_text Failed to write contacts log to text file If there is no currently active contacts log, then an error message is shown on the screen as follows: contacts> write_text Error: You must create or load a contacts log first read_text <file_name> Reads in contacts information from a text file with the specified name. The file’s name will determine the log_name of the new contacts_log_t instance. For example, if contacts are read from the file
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 14/17 People.txt , then the log_name will be "People" . Notice that the file extension ( .txt ) does not become part of the log’s name. There must not already be a currently active contacts log when this command is executed. Upon success, a message is printed to the screen as follows: contacts> read_text People.txt Contacts log loaded from text file If an error occurs when trying to read the specified file, an error message is shown on the screen as follows: contacts> read_text People.txt Failed to read contacts log from text file If there is already an active contacts log, then an error message is shown on the screen as follows: contacts> read_text People.txt Error: You must clear current contacts log first write_bin Writes all contacts stored in the contacts log to a binary file. The name of this file is based on the contacts log’s name. For example, a contacts log with the name "School" is saved in the file School.bin in the current working directory. Upon success, a message is printed to the screen as follows. contacts> write_bin Contacts log successfully written to School.bin If the write to the file fails, an error message is printed to the screen as follows. contacts> write_bin Failed to write contacts log to binary file If there is no currently active contacts log, then an error message is shown on the screen as follows. contacts> write_bin Error: You must create or load a contacts log first read_bin <file_name> This operation involves some tricky memory management. Be sure to read the note above in the discussion of the required binary file format. Reads in contacts information from a binary file with the specified name. The file’s name will determine the log_name of the new contacts_log_t instance. For example, if contacts are read from
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 15/17 the file People.bin , then the log_name will be "People" . Notice that the file extension ( .bin ) does not become part of the log’s name. There must not already be a currently active contacts log when this command is executed. Upon success, a message is printed to the screen as follows: contacts> read_bin People.bin Contacts log loaded from binary file If an error occurs when trying to read the specified file, an error message is shown on the screen as follows: contacts> read_bin People.bin Failed to read contacts log from binary file If there is already an active contacts log, then an error message is shown on the screen as follows: contacts> read_bin People.bin Error: You must clear current contacts log first exit This command is already implemented for you. It exits the contacts_main program. Supporting Command Line Arguments Many programs take in additional information specified as additional arguments when they are launched from the command line. Your last task is to add this feature to the contacts_main program. You should support the following cases for command-line arguments. contacts_main <file_name> If a file name is passed in as a command line argument, then you should attempt to immediately load contacts log data from this file when contacts_main starts. You will also need to do some string handling to deal with different file extensions. If the file’s name ends in .txt , assume the file is a text file and use the read_contacts_log_from_text function. If the read succeeds, your program’s output should appear as follows: ./contacts_main People.txt Contacts log loaded from text file If a failure occurs, then you should print an error message to the screen as in the following example. ./contacts_main badFile.txt Failed to read contacts log from text file
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 16/17 If the file’s name ends in .bin , assume the file is a binary file and use the read_contacts_log_from_binary function. If the read succeeds, your program’s output should appear as follows: ./contacts_main People.bin Contacts log loaded from binary file If a failure occurs, then you should print an error message to the screen as in the following example: ./contacts_main badFile.bin Failed to read contacts log from binary file Otherwise, if the file’s name does not end in either .txt or .bin , you should print an error message to the screen as follows: ./contacts_main People.pdf Error: Unknown contacts log file extension In any of these situations (text or binary file, success or read failure, unknown file type), your program should then proceed with the normal welcome message and command prompt. One example execution of your program might be: ./contacts_main People.txt Contacts log loaded from text file CSCI 2021 Contact Log System Commands: create <name>: creates a new log with specified name log: shows the name of the active contacts log add <name> <phone> <zip>: adds a new contact lookup <name>: searches for a phone number by contact name clear: resets current contacts log print: shows all contacts in active log write_text: saves all contacts to text file read_text <file_name>: loads contacts from text file write_bin: saves all contacts to binary file read_bin <file_name>: loads contacts from binary file exit: exits the program Debugging Advice Start by studying the starter code (including the comments in contacts.h ) and this specification carefully. You can’t pass the tests if you don’t understand the expectations of your implementation and what we are testing for. When gcc produces error messages, it’s usually better to start at the top of its output and work your way down. Often there are a few messages coming from gcc that stem from the same initial problem upstream. Debug systematically, not by guessing. Be sure you know how to run the contacts_main program yourself, then run it and enter commands yourself to gain an understanding of which events are causing the problems with your code.
2/11/24, 11:05 AM Project 1: CSCI 2021 (010) Machine Architecture and Organization (Spring 2024) https://canvas.umn.edu/courses/413046/pages/project-1 17/17 Try writing an implementation for a single command and immediately test that command instead of trying to complete the entire solution all at once. Use valgrind . Spend time familiarizing yourself with this tool and the output it produces. It can catch a variety of problems in your code such as where a segmentation fault is coming from, where you have forgotten to free memory, or where you read or write to invalid memory addresses. Study the test cases. They contain all the input we will provide to your program and all of the output we expect from your program. They often involve a specific sequence of steps to check that you are handling both normal situations and error cases properly. Project Submission You must submit your code to Gradescope to receive credit for this project. You can run the make zip command to conveniently create a zip archive suitable for uploading to Gradescope. Submissions should be submitted to the Project 1 assignment listed in our course’s Gradescope page. See Lab 1 (https://canvas.umn.edu/courses/413046/pages/lab-1) if you need further guidance on submitting your code and viewing the autograder’s test results. You may also wish to review the late submission policy detailed in the course syllabus to understand what score you will receive if submitting your work after the deadline. No project submissions are accepted more than 48 hours after the deadline.