Deliverable - Farmer Wolf Goat Cabbage
Short Description
Source code for classic college programming exercise...
Description
Page |6 COVER/TITLE PAGE Farmer, Wolf, Goat, Cabbage.
A C++ Programming Project Submitted to the Faculty Of COP1000-70T Eastern Florida State College
By
ME In Partial Fulfillment of the Requirements For the Course of COP1000-70T Principles of Programming May 2014
Titusville, Florida
Page |7
Table of Contents:
.
.
.
.
.
.
.
1
.
.
.
.
.
.
.
.
2
.
.
.
.
.
.
.
.
3
Program’s Problem and Solution
.
.
.
.
.
.
4
Flow Chart .
.
.
.
.
.
.
6
.
.
.
.
.
.
8
List of Preprocessor Commands/Header Files, and Libraries used . . . . .
.
.
.
8
List of Variables .
.
.
.
.
.
.
.
.
8
Source Code
.
.
.
.
.
.
.
.
9
Time Log .
.
Abstract of Program
.
.
List of Functions .
.
.
.
Page |8
Screen Shot of Executed Program .
.
.
.
.
.
14
Page |9
Time Log The project “Farmer Wolf Goat Cabbage” was started on February 3rd, 2014. During this initial process, I came up with Algorithms and Flowcharts for the program. After flow charting and writing some pseudo code, the programming of the C++ programming commenced. On April 22nd, the source code and commenting was completed, and the final build of the project was completed on May 2nd, 2014. The program was exhibited on April 30th to Professor Canton-Sanchez, M. for review. Professor Canton-Sanchez asked for the code to be commented further, these modifications were implemented on the evening of April 30th. During this process I made myself available to help other students along with their projects. The total number of man hours spent on this was around 16hrs.
P a g e | 10 Abstract The program “Farmer, Wolf, Goat, Cabbage” is a classic computer program assigned to begging programmers to get them used to thinking logically and to expose the students to how really dumb a computer is and how it is necessary to give them every single instruction.
This program does not take any user input and starts with the boat the farmer, wolf , goat cabbage sitting on the West shore of a river. The program is run from a while loop in the main function where it will run until all items are moved from the West shore of the river to the East shore of the river. The criteria set up for this trip across the river were that, the wolf and the goat could not be left alone together, and the goat and the cabbage could not be left alone. The program verifies the coasts are safe before taking the next item across the river.
P a g e | 11 Problem and Solution Question:
How to move items safely across the river and only allow one item to be moved at a time?
This is done by moving the goat to the East side of the river first, leaving the wolf and the cabbage alone, since the wolf won’t eat the cabbage the shoreline is considered safe. The farmer then rows the boat back alone to the west shore to get the cabbage and rows back to the East shore where he must pick up the goat as not to leave the goat alone with the cabbage because the goat will eat the cabbage. After picking up the goat from the East shore the farmer rows the boat back to the east shore to drop off the goat and pickup the wolf, then row it back to the East shore to drop the wolf Off alone with the cabbage. The farmer rows back to the West shore alone to pick up the cabbage and row it back to the East shore completing the task.
The Pseudo code is as follows: Load the boat with goat at the West shore. Cross the river. Unload the goat from the boat
P a g e | 12 Cross the river. Load the boat with the cabbage Cross the River. Unload the cabbage and load the goat. Cross the river with goat. Unload the goat and load the wolf into the boat. Cross the river. Unload the wolf from the boat. Cross River alone. Load the boat with the goat. Cross River Unload the goat. End Loop End Program.
P a g e | 13
While Loop
No
Is West shore safe?
Load Boat
Yes
Cross River
No
Unload Boat
Is the East shore safe? Cross River
End While
End Program
Yes
Page |8 Lists: Functions: int main() int int int int
loadboat(void);// Loads the boat crossriver(void);// crosses river unloadboat(void);// unloads the boat iscompatable(void);// determines if the food chain is safe
Header Files and Libraries: #include "stdafx.h" #include #include #include #include
using namespace std;
Variables: ///// Defines the food chain ///// #define cabbage 1 #define goat 2 #define wolf 3 #define farmer 4 //////// Defines the load unload locations #define boatwest 1 #define boateast 2 int boat[2];// Holds the farmer and the cargo int westshore[4];// holds the cargo on the west shore int eastshore[4];// holds the cargo on the east shore int boatlocation;// holds the east west location of the boat std::string cargo; int good = 0; int lastitem = boat[0];
Page |9
// FarmerWolfGoatCabbage.cpp : Defines the entry point for the console application. // Program Name: Farmer, Wolf, Goat, Cabbage // Description: // This program solves the classic Farmer, Wolf, Goat, Cabbage problem. // The program will load the boat then determine if the shorelone // is safe, before embarking on it's journey to the other side of the river // the program will then unload the boat and ensure the shoreline is safe. // The main loop contains only the logic required to ensure the boat // initiates a loading, crossing and unloading function and exiting // when all the items are moved safely accross the river. // // // // // //
Developer: Date: Development Environment: food chain: goat eats cabbage wolf eats goat
Anthony Olive 5/4/14 Microsoft Visual Studio 2013
#include "stdafx.h" #include #include #include #include ///// Defines the food chain ///// #define cabbage 1 #define goat 2 #define wolf 3 #define farmer 4 //////// Defines the load unload locations #define boatwest 1 #define boateast 2 int int int int
loadboat(void);// Loads the boat crossriver(void);// crosses river unloadboat(void);// unloads the boat iscompatable(void);// determines if the food chain is safe
int int int int
boat[2];// Holds the farmer and the cargo westshore[4];// holds the cargo on the west shore eastshore[4];// holds the cargo on the east shore boatlocation;// holds the east west location of the boat
using namespace std; void main() { boat[1] = farmer;
// Place the farmer in the boat
boatlocation = boatwest;
// starts the boat on the west bank
westshore[0] = cabbage;
// starts the cabbage on the west bank
P a g e | 10 westshore[1] = goat; westshore[2] = wolf;
// starts the goat on the west bank // starts the wolf on the west bank
// sets the east bank to nothing eastshore[0] = 0; eastshore[1] = 0; eastshore[2] = 0; // Main loop runs until the west shore is empty while (westshore[0] != 0 || westshore[1] != 0 || westshore[2] != 0) { boat[0] = loadboat(); boatlocation = crossriver(); boat[0] = unloadboat(); boatlocation = crossriver(); } cout
View more...
Comments