Programming Report Meals Manager Project
STEPHANE LO-PRESTI 2009
Mohammed Elsouri 0707773 Mario Siqueira 0719547 Nikolas Salpingidis 0710714 Andrew Bailey 0722532
PROGRAMMING REPORT
Background Knowledge
Andrew
2-3
Introduction
Group Communication
Nick/Andrew
4
Problem identification
Mohammed
5-6
Solution Proposals
Nick
6
Final Solution
Mario/Andrew
7
Software Design
Mohammed
7-8
System Flowchart
All
9-10
Pseudo Code
All
11-12
Andrew/Mohammed
13
Design
Implementation
Programming Report | 4/2/2009
Testing Strategy
1
Program Development and Documentation Mohammed
14-16
Testing procedure
Mario
17-22
Evaluation
All
23
References
All
24
Background Knowledge Any computer software or application requires some form of programming to run effectively and perform a desired task. From simple word processing to managing bank accounts, programming has become a very versatile and powerful tool in the modern world. C programming first appeared in 1972 having been developed by Dennis Ritchie, though programming techniques and languages had been around for a lot longer. 1978 proved to be breakthrough year when Brian Kernighan and Dennis Ritchie published “The C Programming language”. This book became an informal specification for the C programming language, though it referred to the early versions known as “K&R C” named after the authors’ initials. Standardisation and definition of the language was established by the American National Standards Institute in 1983 and from there the C language has become one of the most popular and versatile tools used by programmers. So, we have seen some of the history behind the development of the C language; what follows is an explanation of what the language is how it works. We start with a compiler which is a computer program or series of programs that convert a source code or series of instructions into computer code known as object code. The aim of this is to create an executable program that can perform tasks effectively.
Programming Report | 4/2/2009
There are several layers to the programming environment and a compiler is used to convert high level code to low level language such as object code. In our case it is C language that is used and it is a procedural systems implementation language. In simple terms this means that the language specifies the steps the program must take to reach the desired state. The system implementation part refers to the production of software which provides services to the computer hardware. The C language was designed to be read by a relatively straightforward compiler and this gave several characteristics, namely: language constructs that map effectively to computer machine instructions and minimal run-time support requirements.
2
As with any language, programming requires syntax to work properly. Syntax is a set of rules that define the combination and construction of symbols and characters that validate written code; just as in English the construction of a sentence determines whether it can be understood when read. As a group our first exposure to C programming was the simple yet famous “hello world!” program. The following construct illustrates this simple program and shows some of the syntax used.
#include <stdio.h>
Programming Report | 4/2/2009
int main(void) { printf("hello world\n"); return 0; }
3
As a language, C is relatively easy to learn though it is more cryptic and example above is broken down into the following steps: the #include is a pre-processor directive. The preprocessor is the first tool to read the source code and it is instructed to substitute the entire line with the <stdio.h>. The next line indicates that a function named “main” is being defined. The “main” function serves a special purpose in C programming; it is used to begin program execution. The “int” is a type specifier and its purpose is to indicate that the value returned to the invoker as a result of reading the main function, is an integer. The keyword “void” as a parameter list indicates that the main function takes no arguments. The program then follows with the execution line that calls the function named “printf” which was defined in <stdio.h>. The “printf” function is given the address of the first character in the string of words “hello world\n”. This string of letter is known as an array and it ends with “\n” to denote the end of the array that translates to a new line in C language. The compiler needs to know this in order to reach the end state. This simple example illustrates the construction of source coding and shows a clear need for correct syntax for a program to run effectively. Throughout any programming project, it is syntactic errors that slow the development of the code and in some cases result in a failed program.
Introduction The aim of this project was to create an interactive program to solve a chosen problem. This project gave useful exposure to the C Programming language. This was a group project and another objective was to work effectively as a group and manage all the tasks effectively. The project was done as a combined group initiative. The building process started with a team brainstorming session to find a problem that could be solved as well as the possible solutions. This was achieved in a short time period as the whole project was set to tight deadlines. The next objective was the choice of the most appropriate solution. The solution needed depth so that all four group members could explore the C programming language and its capabilities.
Programming Report | 4/2/2009
In terms of workload the division of the tasks, one of the most challenging tasks was ensuring uniformity of the coding language used and defining variables. Once the problem had been chosen the next stage was to create a pseudo code; a series of instructions and parameters to be followed for the desired result to be achieved. This would also serve as a guideline once the building of source code commenced. Testing and evaluation of the code would form the final stages of the development though it was testing of small sections of code was routinely done to indentify errors early on.
4
Problem Identification The group members were asked to look at many problems that could potentially be face in our daily life. There are many problems that humans face every day, things like missing appointments, so solutions such as memos, alarms, and organizers are implemented. The group had several meetings to discuss what problems can we identify in our lives, and how would a computer program solve it. The problems we have looked at were varied and very open ended. One problem was the variation of interfaces that we use to check many things. For example we use the computers to check emails, phones to check messages, and reminding letters to check bills, and post-it notes to check appointment dates. Why can’t we integrate all these possibilities into one piece of software that can do all these things for you, and also organize those using automated algorithms where it places appointments in their time slots, and reminds you of bills a week before they are due, and displays your important emails the day they are received. Another problem was the fact that all of our files are displaced on our laptops, and are completely forgotten about, and when it comes to the time you need it, you cannot find it. We have thought thoroughly about problems with taking this scenario and finding a solution for it, but it was too complex to implement given the knowledge we have about C and the time we have to do it in.
Programming Report | 4/2/2009
Many ideas were thought about. A very interesting one is a piece of software than can calculate really complex formulas for you by just inputting the values into the formula. The problem that this solution came from is very common amongst us designers. Sometimes we want to design something but we need to work out the stress of the object, or something similar, and we end up writing a lot of pages and the final answer is not found. So from this proposal of software design, we can simply enter the dimensions of the shape, the forces acting on it, and what we need to find from it (in this example the Tension of the surfaces and the stress in them)
5
The most interesting problem was the chosen final problem. It is the problem we face with the food in our fridges. Food goes out of date all the time, and we don’t realize that until we want to use it, sometimes we don’t use it because we don’t know what dishes we can combine with many other ingredients in our fridges. This is a problem that a lot of people daily. They simply buy food and place it in their fridges and never plan when to use it or what it can be used for. Our solution is to fix this problem through a program in C language. Once we have had a look at the problem, we have managed to figure out many sub problems that came to us. The major problem we aim to solve is the disorganization of food, and the times where we don’t know what to cook because we are not so sure about what ingredients we have, whether it was expired or not, and how much of it is left. The basic solution statement is the following:
To organize all the food in our fridge, and being able to generate food recipes whenever we ask it to, and to have automatically detect the food that is nearly expired and use it to generate the recipes.
Solution proposals The desirable software would be something that would be able to download recipes from the internet depending on the preferences of the user; scan the new ingredients added to the fridge and remove ingredients automatically from the list when used. It should also be able to calculate the removal of ingredients like milk, sugar, bread and generally ingredients with different types of measurements. Having that ideal software in mind we tried to generate ideas that would have the same principals but would be easier to program with the available recourses and time. The initial idea was to create a program that would check the existing ingredients and after having checked the expiring dates it would generate some recipes from a database of stored recipes. The problem with that idea was the fact that it wouldn’t take into account the user’s preferences or any potential allergies. Another section we thought of was the fact that sometimes there might be visitors to the house.
Programming Report | 4/2/2009
Having these scenarios in mind we came out with a basic solution which we used as a platform when thinking about the best solution. We divided the software into three main sections, profiles, ingredients and recipes and started building the whole software idea on that platform.
6
Final Solution The eventual desire was to have a fully working and very dynamic program. The program would have a user profile manager where an individual’s details like, name, age, eating preferences and allergies. These would be stored in a database that could be edited and even have an option for entering an additional profile for a guest who came to eat. The second aspect of the program would be a recipes library where a range of meal ideas could be stored as well as edited by the user. The aim would be for the user to be able to select a meal based on personal preference and availability of ingredients. Such a database would need to be varied to satisfy the tastes of those in the household, therefore some flexibility would be necessary and an option to edit the contents be provided. An ingredients manager would follow on and the objective was to allow the user to keep a working record of the produce in their fridge in terms of when it was purchased, the quantity and its expiry date. Obviously to enter the information the user would need some form of interface like a console.
Programming Report | 4/2/2009
Software Design The initial software idea we had, was found to be more complicated than we had thought. The original idea was to have 3 tables in one single database, than can link the users to their food preferences, allergies, and other features related to each user. The recipes table links individual recipes to its specific ingredients. The ingredients are the only standalone table that does not link onto a table, but is rather linked to. The diagram below explains this concept.
7
Profiles Table
Recipes Table
Ingredients Table
Profile Name*
Ingredient
Ingredient*
Allergies
Recipe*
Nutritional Values
Recipe
* is primary key
Quantity
* is primary key
* is primary key
The way the tables we initialed wanted to work looked like the ones above.
However we could not integrate this idea well in C programming as much more advanced degree of skills is needed. So we decided to concentrate and work on one part of the software and the table handling is described below. There will be only two tables One table will be the ingredients table, where our program can add, modify, review the ingredients The other table will be the Recipes table, where our program is able to call the ingredients specific to individual recipes. There are many aspects of programming to integrate however these aspects are very complicated, many are validation rules and programs These aspects are briefly listed below:
•
•
Programming a function that will only accept letters as you enter them, and will not accept number entries into the add function of the program as all ingredients are words and don’t have letters Programming a function that can detect if the entry has all ready been entered in the table and rather than adding the entry to the end of the ingredients file it will add a number to the quantity of the ingredient Programming a function that can search for a specific ingredient (if user wants to know quantity of this ingredient in his fridge) rather than display all the content of the ingredients table on the screen (as in option 3)
Programming Report | 4/2/2009
•
8
9
Programming Report | 4/2/2009
10
Programming Report | 4/2/2009
Pseudo Code This an exact pseudo code that tries to describe the way the program works For the ingredients menu page, the detailed pseudo code is this – Display Welcome Message Display content of menu (options) Ask user what he would like to do
If (user chooses to add ingredient to table) then Open table for editing Ask user what the ingredient he is adding is Copy the new ingredient at the end of the open file Exit and close file Otherwise
If (user chooses to subtract ingredient from table) then Ask user what the ingredient he wishes to delete is Store the entry into an allocated temporary memory Open table for editing Copy content of table into a new temporary file Search the word that the user entered (now in allocated temporary memory)
If the word is found then Copy all the content of the temporary file APART from the word found Into the original ingredients file
Programming Report | 4/2/2009
Close both files
11
Delete Temporary files and clear word from temporary memory Otherwise (if the word is not found) then prompt user
If (user chooses to just review ingredients from table) then Open ingredients file Copy content of file Display the content on the console screen Otherwise tell user that file not found
For the recipes menu page, the program should run as shown below
Opens ingredients.txt Opens fishandchips.txt Available_recipes.txt Reads the ingredients text file and places the available ingredients into an array Reads the fish and chips text file and places The ingredients needed into an array with the use of an "if" and "while" commands it loops until it finds the ingredients that are needed or reaches the end of the ingredients array.
Programming Report | 4/2/2009
At the same time it increases the value of the sum so at the end if the sum reaches the needed value for the user to be able to cook the recipe, it inserts on the available recipe folder the name of the recipe
12
Testing Strategy It was identified early on that testing and evaluation of the source code would determine a successful program. Each group member was responsible for checking their code construction so that the final compilation could run smoothly. Our program would no doubt contain variables. These would be where the user could input required information which would be stored on file. Clearly one of the first testing stages would be verifying the variables by inputting them into the program. Successful execution would be determined by the output result from the program. Such a test would need to be carried out for each variable.
Programming Report | 4/2/2009
It was decided to have three interfacing sections where information could be added, edited or viewed. The first section would be where the user could input information and it would be saved to the file. In terms of testing this section, the strategy was to run the program and input information when prompted. A successful test would result if the information was stored to the file. Editing the stored information formed the second part of the program and the aim would be to allow the user to view and alter the stored information. Testing this would require the table to be viewed and enter a piece of data and once the updated information was complete it also had to be stored effectively. Adding data was one option but deleting data would also be required and an appropriate test to delete and entry and have the changes saved was another test worth doing. The final part of the program was a viewing section; the purpose of this being to allow the user to view the information stored on the file. It was decided to limit the operation of this to “read-only” meaning that the stored information could be read but not altered. This could involve a simple test of trying to input data and seeing if any changes occurred.
13
Validation of the user input would form part of our strategy. The desire was to limit what the user could input, for example entering a number character to determine an ingredient quantity instead of a letter character. There would need to be some fail-safe against entering false information. Basic testing was of the code would need to be done frequently in a strategy of “check as we go”. A debugger would be a quick and easy way to do this and it was decided that routine checks would be carried out by each group member as they developed code. It was hoped that the final result would be a smooth assembly of all the code components built by each person.
Program Development and Documentation The program was initially a few comments with what we intentionally wanted to do. We worked slowly to develop it but a lot of difficulties were faced. The programming language was difficult to comprehend and understand, so as we tried to apply it without much knowledge of it problems were faced. Even though previous exercises were practiced, the entire group was facing difficulties producing something. Each one then tried to understand what each code does and how it can be used. As a result of this, there were several versions of the program. At the start, all the versions were incomplete and do not work. Each one was assigned a part of the program to develop. Mario was assigned the profiles management part of the program Mohammed was assigned the ingredients management part of the program Andrew was assigned the recipes management part of the program And Nick was assigned the part of the program that links all these aspects
Programming Report | 4/2/2009
After it proved to be too difficult and complex for each one to develop his part of the program, people were still empty handed and had nothing.
14
The ingredients management however was one program that was developed over Easter.
Programming Report | 4/2/2009
The first version of it was the following:
15
printf ("Welcome To The Ingredients Management Main Menu\n"); printf ("\n"); printf ("\n"); printf ("\n"); printf ("\n"); printf ("Please select an option from the following:\n"); printf ("\n"); printf ("1. Add Ingredient\n"); printf ("2. Remove Ingredient\n"); printf ("3. Review Ingredient\n"); printf ("\n"); printf ("\n"); int option = 0; // this creates a variable "option" where the amount of charecters it can enter is 1 of type "numbers only" scanf_s("%d", option); printf ("\n"); char ingredient [25] // this will confirm the selection of the user, and will ask user to wait to connect to table and initialise page // if the user selects option 1, he will be able to add and ingredient // otherwise if he selects option 2 then he can remove it // and option 3 allows him to review it ingredients ;if (option=1) printf("You chose to Add Ingredient. Please wait..."); {file *f1; f1=fopen(“Ingredients.txt”,”w”); /*Open the file Ingredient*/ printf ("Please enter the name of the new ingredient"); scanf ("%s", &ingredient);s; while((c=getchar())!=EOF) /*get a character from key board*/ putc(c,f1); /*write a character to ingredients*/ fclose(f1); /*close the file ingredients*/ printf(ingredient, "You Entered: "); f1=fopen("INPUT","r"); /*Reopen the file ingredient*/ while((c=getc(f1))!=EOF) printf("%c",c); fclose(f1); } else if (option=2) printf("You chose to Remove Ingredient. Please wait..."); else Comments were still being if (option=3) integrated and written at {printf("You chose to Review Ingredient. Please wait..."); this point but the structure fp=fopen("Ingredients.txt", "a"); started coming to life if(fp==NULL) { printf("file not found!\n"); else f1=fopen("Ingredients.txt","w"); /*Open the file Input*/ exit(0);}; } else printf ("\n"); return 0;
The program had many errors and did not run at all, so during the debugging process, the program started to become functional slowly. The group decided at this point that only this part should be developed, time was running out and something must be done, and perhaps if there was more time in the end, we could think about programming the other parts of the program.
Each group member started from the afore mentioned initial point and working together to get the code working. The add option started to take shape, as well as the subtract option. A few errors occurred, many attempts were tried to fix these errors and make the code work perfectly. Each one of us engaged in debugging. Towards the end, everything started coming together. The add function finally worked though it required lengthy research into code and working out how the program needed to be manipulated. The subtract ingredient function also became operational. To complete the construction, the review operation was built. From there, we could now develop the program and add the initial ideas we wanted to have in the program. One of these initial ideas was the best one to develop to prove how the principle of the “meal generator” works.
Programming Report | 4/2/2009
The new addition works by having the new function that suggests a meal idea. What it did is it stored the ingredients and the recipe files into arrays, and then it searched for the needed ingredients from the ingredients file by matching the ingredients to the recipe file. If the ingredients needed were available then it will produce a new file where it mentions all possible recipes.
16
Testing procedure
Programming Report | 4/2/2009
The following screen shots illustrate some of the testing that was carried out to validate the program and check that it worked.
17
18
Programming Report | 4/2/2009
19
Programming Report | 4/2/2009
20
Programming Report | 4/2/2009
21
Programming Report | 4/2/2009
Programming Report | 4/2/2009
For additional notes on the testing, please refer to the readme.txt file on the accompanying project CD.
22
Evaluation: As a group it was discovered that C programming was a versatile tool. Though it proved to be challenging to use and manipulate; as with any language understanding the basics is essential before any further progress can be made. During the course of this project, the division of tasks and project work proved to be the best way to get tasks done. Meetings were held regularly to maintain progress and each group member to feed off the others in terms of sharing knowledge and problem solving. It also revealed the difficulties in maintaining uniformity of coding variables and syntactic correctness. Having completed the project a great deal was learned about the complexity and intricacy of designing, building and testing a working code. It was not possible to achieve the desired end product and in evaluating our progress the following was observed. The initial division of coding was done in a way that led to the creation of four sizeable parts of code which did not work properly at first and there problems encountered with different variables and text files. When these problems were encountered it was decided that it was best to start from the beginning with just a few small parts of code that worked. A new plan was set and small parts of code were developed and as soon as they were built and run they were put together with other component parts. This approach proved better because the problems were solved as soon as they were discovered. This helped with the smooth compilation of the final code.
Programming Report | 4/2/2009
Time management was crucial for this project. With the desire to create a working program and with the limitations of time, it was decided to create a simpler version that would work but still prove the principle and solve the problem.
23
REFERENCES: Websites sourced for research: • http://en.wikipedia.org/wiki/C_programming Website visited on 20/04/2009 • http://en.wikipedia.org/wiki/Syntax_of_programming_languages Website visited on 18/04/2009 • http://www.howstuffworks.com/c.htm Website visited between 18/04/09 to 20/04/09 • http://www.cprogramming.com/ Website visited between 15/03/09 to 29/04/09
Programming Report | 4/2/2009
Electronics Programming and Interfacing resources • U-link web tutorials covering C Programming. • MSDN library – for coding language, syntax and guidelines.
24