SP 2020 CSE 2421 LAB 3 Assigned: Wednesday, January 29th
Early Due Date: Wednesday, February 5th by noon Due: Thursday, February 6th, by 11:30 p.m.
Objectives:
∙ Pointers
∙ Multiple Levels of indirection ∙ Dynamic memory allocation
∙ Functions
∙ Arrays (dynamically allocated) ∙ Character Strings
REMINDERS and GRADING CRITERIA:
This is an individual lab.
Effort has been made to insure that this lab description is complete and consistent. That does not mean that you will not find errors or inconsistency. If you do, please ask for clarification.
Every lab requires a Readme file (for this lab, it should be called lab3Readme – use this name. This file should include the following:
∙ Required Header:
BY SUBMITTING THIS FILE TO CARMEN, I CERTIFY THAT I STRICTLY ADHERED TO THE TENURES OF THE OHIO STATE UNIVERSITY’S ACADEMIC INTEGRITY POLICY.
THIS IS THE README FILE FOR LAB 3.
∙ Your name
∙ Total amount of time (effort) it took for you to complete the lab
∙ Short description of any concerns, interesting problems or discoveries encountered, or comments in general about the contents of the lab
∙ Describe, with 4-5 sentences, how you used gdb to find a bug in your program while debugging it. Or, if you had no bugs in your program, how you used gdb to verify that your program was working correctly. Include how you set breakpoints, variables you printed out, what values they had, what you found that enabled you to fix the bug.
You should aim to always hand an assignment in on time or early. If you are late (even by a minute – or heaven forbid, less than a minute late), you will receive 75% of your earned points for the designated grade as long as the assignment is submitted by 11:30 pm the following day, based on the due date given above. If you are more than 24 hours late, you will receive a zero for the assignment and your assignment will not be graded at all.
Any lab submitted that does not compile – without errors or warnings – and run WILL RECEIVE AN AUTOMATIC GRADE OF ZERO. No exceptions will be made for this rule – to achieve even a single point on a lab, your code must minimally build (compile to an executable without errors or warnings) on stdlinux and execute on stdlinux without crashing, the gcc command must use the –ansi and -pedantic options.
Since a Makefile is required for this lab, you must create the appropriate compile statements to create a lab3.o file, which would then create a lab3 executable. Graders will be downloading your lab3.zip file from Carmen, unzipping it, and then executing make from a linux command line prompt. Your program must compile –without errors or warnings – via commands within the Makefile. Given valid input, your program must also run without having a seg fault or other abnormal termination.
You are welcome to do more than what is required by the assignment as long as it is clear what you are doing and it does not interfere with the mandatory requirements.
LAB DESCRIPTION
BOOK LIST WITH FAVORITES (100%) Mandatory filename: lab3main.c readtitles.c
getfavorites.c
savedata.c Mandatory executable name: lab3
PROBLEM:
The user of your program will use it to create a list of books s/he had read, and then your program
will create a subset of this list as a favorites list. You will not know how many books the user wishes to include on the list until the program gets its first input. After the user has finished entering all of the books titles, you must prompt the user for a subset of the book titles to put on a “favorites” list. You will want to review Slide Deck 13, slides 46 through 50 to help you visualize how the data should be stored. You must use pointers within this project such that you have one array of char * that contains all of the book titles and an array of char ** for favorites. The third item this program does is ask the user whether or not s/he want to store a copy of their information in a file on disk. If so, you program will ask for a file name and store the information within the file in the format shown in the example below.
First, you should prompt the user to enter the number of books titles they plan to enter. The user willenteranintegergreaterthanorequalto1toindicatethenumberofbooktitles.(NOTE: Makea point to test your program to ensure entering just 1 book title works.)
You must then dynamically allocate enough memory to hold all books titles. You can assume that there will be no book titles with more than 60 characters. Remember that all character strings in C are null terminated and that you have to allocate space in your string for that null character, in addition to the length of the string you wish to have. Also realize that you won’t know how many of the 60 characters the book title will fill until after you read the title.
You should then prompt the user to enter each book title on a separate line. You must assume that each book title could contain more than one word and will be separated by a newline character from the next book title. You can assume that the user will enter the input in this format, so you do not need to check to make sure that the format of the input meets this description, and you do not need to reject input which is not properly formatted. You can also assume that the user’s input is correct. If you do not completely understand this description jump to the bottom of this file and check out the example data.
You can assume that there will be no book title duplicates.
After reading in all of the book titles, your program should print all of the book titles back
out with a number next to each title.
Then ask user to pick how many books s/he wishes to put on their favorites list. The favorites list will consist of a subset (up through all books on the titles list) of the books on the titles list.
Once you have this number, you will have to dynamically allocate enough space for an array of 8-byte addresses.
The user will then specify by title number which books should be included on the favorites list.
Your program should then print out the books on the favorite list.
Next, your program should ask whether or not the user wishes to store in an ASCII file the
information they have input. They indicate yes/no with 1 or 2.
If the user wishes to save the data, you must open a file and store the information in the file based on the format shown below, close the file, and then confirm to the user the data has been saved.
Prior to exiting the program, you must free() all dynamically allocated memory. You can determine whether you have managed to accomplish this with the valgrind tool.
CONSTRAINTS:
∙ Source code files (.c files) submitted to Carmen as a part of this program must include the following at the top of the file:
/* BY SUBMITTING THIS FILE TO CARMEN, I CERTIFY THAT I HAVE ** STRICTLY ADHERED TO THE TENURES OF THE
** OHIO STATE UNIVERSITY’S ACADEMIC INTEGRITY POLICY.
*/
If you choose not to put the above comment in your file, you will receive no points for the lab.
− Youmustcommentyourcode
You must create and submit a Makefile that will be used to create your .zip file and your executable, lab3. This Makefile must define at least each of the following targets: all, lab 3, lab3.zip, lab3main.o, readtitles.o, getfavorites.o, savedata.o, clean. Creating this file at the beginning of your development process and using it as you work, will allow you to test your Makefile for proper operation.
− Separateyourcodeintodifferentfunctionssothatyourmain()isunclutteredandeasytounderstand. Pointers to your two arrays should be declared in your main() program. You should have a different function that is called from main() that performs each of the following tasks:
1. populates the titles array in a file called readtitles.c
2. populates the favorites array in a file called getfavorites.c
3. saves data to a file in a file called savedata.c
− Youmustcreatealab3.hfilethatholdstheprototypestothesethreefunctions.
− Novariablescanbedeclaredoutsideofablock.Thatis,novariablescanbedeclaredas“global”.
− Youmayonlyusegetchar(),printf(),fprintf(),andscanf()foranyI/Oneedsyouhavewhilewritingthis program.
− Youmustusepointerswithinthisprojectsuchthatyouhaveanarrayofcharpointersthatcontains the addresses of all of the book title strings (the variable that holds the address to this array would have to be declared as char **) and an array for favorites that contains addresses from the titles array (the variable that holds the address to this array would have to be declared as char ***).
− Besureyourdirectionstotheuserareclearsotheyaresuretoentertheinput data correctly.
− Youcannotusestaticallydeclaredarraysinthislabtostoreanyoftheuserdata other than the filename in which to store user’s information.
− YourcodeshouldworkcorrectlyforANYNUMBERofbooktitles(includingjust1and up to the limits of available memory, of course), and these numbers are not known in advance.
− Youwillneedtousepointerstoaccessuserdata
− thevalgrindprogramshouldsaythatyourprogramhasnomemoryleaks
− Youmaynotaccessanyoftheallocatedstoragespaceusingindexes,asisusuallydone for a statically declared array, but only by using pointers and pointer arithmetic.
HINTS:
1. You will likely want to dynamically allocate an array of character pointers (one for each title) and a set of pointers to character pointers for the favorites list.
2. You will also likely want to dynamically allocate a character string for each book title. Since you can’t know how long the book title is for any particular book, you will have to allocate the maximum size for a book title for every book.
LAB SUBMISSION
Always be sure your linux prompt reflects the correct directory or folder where all of your files to be submitted reside. If you are not in the directory with your files, the following will not work correctly.
You must submit all your lab assignments electronically to Carmen in .zip file format. The format of zip command is as follows:
[jones.5684@fl1 lab3] $ zip
where
For lab3, the zip command would look like this:
[jones.5684@fl1 lab3] $ zip lab3 lab3main.c readtitles.c getfavorites.c savedata.c lab3.h Makefile lab3Readme
You will need to put this command in your Makefile.
NOTE:
• Your programs MUST be submitted in source code form. Make sure that you zip all the required .c files for the current lab (and .h files when necessary), and any other files specified in the assignment description. Do NOT submit the object files (.o) and/or the executable. The grader will not use executables that you submit anyway. She or he will always build/compile your code using your Makefile after inspecting it to insure it compiles using gcc -ansi – pedantic and then test the executable generated by that command.
• It is YOUR responsibility to make sure your code can compile and run on CSE department server stdlinux.cse.ohio-state.edu, using gcc -ansi –pedantic without generating any errors or warnings or segmentation faults, etc. Any program that generates errors or warnings when compiled or does not run without system errors will receive 0 points. No exceptions!
See below for a sample input to the program. Your program should work for all valid input not just this one. Be creative with your test data!!! ☺
Sample Screen Input/Output for this lab: (user provided input is in green.)
How many library book titles do you plan to enter? 9
Enter the 9 book titles one to a line: Harry Potter and the Order of the Phoenix David Copperfield
3001: The Final Odyssey
Uriel’s Machine
2001: A Space Odyssey
War and Peace
Great Expectations
The Rubaiyat
Adventures of Tom Sawyer
You’ve entered:
1. Harry Potter and the Order of the Phoenix
2. David Copperfield
3. 3001: The Final Odyssey
4. Uriel’s Machine
5. 2001: A Space Odyssey
6. War and Peace
7. Great Expectations
8. The Rubaiyat
9. Adventures of Tom Sawyer
Of those 9 books, how many do you plan to put on your favorites list? 5
Enter the number next to each book title you want on your favorites list: 1 3 5 7 9
The books on your favorites list are:
1. Harry Potter and the Order of the Phoenix
2. 3001: The Final Odyssey
3. 2001: A Space Odyssey
4. Great Expectations
5. Adventures of Tom Sawyer
Do you want to save them (1=yes, 2=no): 1
What file name do you want to use? booksIveRead
Your booklist and favorites have been saved to the file booksIveRead.
Sample File Output for this lab:
Books I’ve Read:
Harry Potter and the Order of the Phoenix David Copperfield
3001: The Final Odyssey
Uriel’s Machine
2001: A Space Odyssey
War and Peace
Great Expectations
The Rubaiyat
Adventures of Tom Sawyer
My Favorites are:
Harry Potter and the Order of the Phoenix 3001: The Final Odyssey
2001: A Space Odyssey
Great Expectations
Adventures of Tom Sawyer