::::: Homework Assignment #5 ::::: Due Friday, December 18, 2009 by midnight! +5 extra credit if submitted on or before Wednesday, December 16. +3 extra credit if submitted on Thursday, December 17. *** All work has to be done individually! *** 1 programming question inside. Programming question #1: Calorie tracker Intro: You decide to make a program that tracks calorie consumption. The program will make a new file for each person, and the person can record his/her breakfast, lunch, and dinner calories. Upon request, it also calculates numbers like average calories per day. The program is menu-based interactive program. It has the following menus: 1. Add new data 2. Calculate average calories per day 3. Save and quit program "Add new data" adds (date, breakfast cal, lunch cal, dinner cal) to the person's record. "Calculate average calories per day" calculates the average all calories/day to date. "Save and quit program" saves all data, including the new data, to file. Instructions: Implement the program using three things learned in class: 1. Use command line argument to open a file for the person For example, $ ./calorietracker Stan opens a file for Stan named Stan.txt. 2. Use linked list to read data from file The file contains 4 columns per line. For example, 2009-11-20 125 250 350 First is date, second is breakfast, third is lunch, fourth is dinner. If there's data already in the person's file, then read all into a linked list. Each line in the file corresponds to one node in the linked list. Use the following node structure. struct calorie_day { char date[11]; // format is 2009-11-20 int breakfast; // calories for breakfast int lunch; // calories for lunch int dinner; // calories for dinner struct calories *next; // pointer to next day }; If there's no data in file, the linked list should be empty. 3. Save data to file When program exits, save all data in linked list to the person's file. The file should contain all 4 columns per line. It should be sorted by date. 2009-11-20 125 250 350 2009-11-21 130 150 400 2009-11-22 0 330 240 Hints: A lot of code fragments that are useful for this homework were covered in class and in homework 4. Review linecount_args.c and calories.c. You can reuse code in homework 4 or code given in class. Attack this homework part by part. 1. Start with a very small and simple program that opens a file with the person's name as command line argument. 2. Then write code that reads the file line by line stores data into ordinary variables like we did in class with calories.c. You may also want to print the value of these variables to see if they're correct. Make a test file and test your code against your test file. fscanf and printf is all you need at this point. 3. Now, start thinking about structures and linked lists. One line in a file is one node in a linked list. You need to be able to make a new node, assign data into the node, and add it into a linked list. To save time, reuse code written for homework 4. 4. Write code to save data in a linked list to file. It can be implemented in one function which goes through the linked list and writes one line per node to a file. Menu items 2 is easy once you get the linked list correct. Grading: Your program will be graded out of 100 with the following breakdown: - Correctness (70 points) And it should compile!!!!! We will not accept any program that does not compile in CUNIX. A program that compiles but is less complete is better than one that does not compile. - Elegance (10 points) Simple code is better than complex code. Sometimes, complex code comes from the programmer not thinking clearly about the problem that the program is trying to solve. Write simple, elegant code. - Format (10 points) Use indentation so that your code is readable. Your code should look neat. - Comments (10 points) Write comments liberally at the top of your code, and also inside the code. What to submit: 1) README file README file is a text file that contains your name, UNI, name and explanation of files that you're submitting, and your comments on your program. 2) Your source code file This is the .c and/or .h file(s) you submit as the programming part of your homework. How to submit: 1) make a directory called homework5 2) copy or move your README, written answer file, source code file to homework4. 3) 'cd homework5' to get into that directory make sure you check that you're in homework5 by issuing a 'pwd' Unix command. 4) Type '~jk2520/submit hwk5' in shell prompt. For example, this is what it'd look like with the CUNIX shell prompt, $ ~jk2520/submit hwk5 The submit script will ask some questions and let you submit homework. You will receive a confirmation email if the homework was submitted properly.