Programming Assignment 3Monday, October 28
Statement of Work
|
|||
1.0 Overview | |||
Many simulations rely on implementing a finite state machine in the form of a graph. A finite state machine is defined as a representation of some event or phenomena in which the system can exist in only a limited (finite) number of states. For example: A coin is a finite state machine when flipped. It can exist in only one of three states: (1) heads, (2) tails, or in very rare circumstances (3) on edge. When a finite state machine is represented as a graph it is frequently necessary to traverse the graph, visiting each node in the graph to perform some action. In this programming assignment you are to implement a graph and perform a "depth first traversal" of the graph. | |||
2.0 Requirements | |||
The student shall define, develop, document, prototype,
test, and modify as required the software system. |
|||
2.1 Functional Requirements | |||
2.1.1 The software shall be capable of representing internally
a graph in the form of an adjacency matrix. The graph shall be implemented
in a class called Prog3Graph which shall consist of the source files
called Prog3Graph.h and Prog3Graph.cpp. The graph class shall
contain the following functions. |
|||
2.1.1.1 bool buildGraph(char *fileName) This function opens the given
text file, reads all data from the file, and implements the graph as an
adjacency matrix. |
|||
2.1.1.2 void printGraph() This function shall print all data in the
graph nodes, including the links for each node. The primary purpose of this
function is to check the contents of the graph after calling buildGraph(). |
|||
2.1.1.3 void depthFirstTraversal() This function shall shall perform a
Depth First Traversal of the graph meeting the following criteria:.
|
|||
2.1.1.4 bool getNextLine(char *line, int lineLen) this function shall read lines
from the data file from the ifstream object that is a private member variable of the class. This function
shall be provided by the instructor. |
|||
2.1.1.5 Any additional functions and/or variables which will be needed to
implement the list of nodes for the traversal shall be defined as private members
in the Prog3Graph class. |
|||
2.1.2 There will be 10 nodes in the graph. Nodes will be implemented by a class
called GraphNode which shall consist of the source files
called GraphNode.h and GraphNode.cpp. |
|||
2.1.2.1 The following private variables shall be in the GraphNode
class:
|
|||
2.1.2.2 The following public functions shall be in the GraphNode class:
|
|||
2.1.3 A sample data file called graph.txt will be provided for use
during development and testing of the program. |
|||
2.1.3 For grading, the provided source code files will be compiled
by the instructor with a
test driver file containing a main() function. When run, the testing program will call
the build graph function in the Prog3Graph and check the boolean return value
to verify that all data was read and the graph built. It will then call the
depth first traversal function and verify the results of the traversal. |
|||
3.0 Deliverables | |||
These products shall be delivered
electronically via e-mail as specified below to the instructor. 3.1 Software Design Document -- The student shall provide a software design document for instructor approval NLT (Not Later Than) Monday, November 11. 3.2 Software Test Plan -- The student shall provide a test plan for complete verification and validation of the software for instructor approval NLT Monday, November 11. 3.3 Source files -- The student shall provide fully tested copies of the .cpp and .h files. These files must be submitted to the instructor via e-mail. The files shall be delivered NLT Monday, November 18. |
|||
4.0 Period of Performance | |||
The period of performance of this assignment is 21 days from the date of assignment. No deliveries will be accepted after the DDD for this assignment. |
Excerpts from
|
||
2.1 System Organization | ||
2.1.1 All data shall be stored internally as a graph which will be
implemented as an Adjacency Matrix. |
||
2.1.2 ... |
||
2.1.3 The format of the data file shall be as follows:# Lines beginning with a '#' character will be ignored as comments # blank lines will also be ignored # It is assumed that the file will be valid and contain data on # 10 nodes for the graph. # Data on each node consists of the following on separate lines # NodeID -- an integer # NodeData -- a character array of up to 10 characters # Number of links -- an integer giving the number of links to other nodes # The next N lines will list the NodeIDs of the links 1234 FSM-State1 2 2345 3456 # etc. for the remaining nodes |
||
2.1.5 The class definition (header) file for the graph class may be
patterned after the following sample.//--------------------------------------------------------------- // File: Prog3Graph.h // Purpose: Header file for a graph class // Programming Language: C++ //--------------------------------------------------------------- #ifndef PROG3GRAPH_H #define PROG3GRAPH_H #include <iostream> #include <fstream> #include <string.h> #include <stdlib.h> #include <GraphNode.h> using namespace std; #define NUMNODES 10 class Prog3Graph { private: ifstream inFile; // File stream to read from int AdjMatrix[NUMNODES][NUMNODES]; GraphNode Nodes[NUMNODES]; public: Prog3Graph(); // Class constructor ~Prog3Graph(); // Class destructor bool buildGraph(char *filename); // Read graph file, build graph void printGraph(); // Print all data in graph void depthFirstTraversal(); // Perform a depth first traversal private: bool getNextLine(char *line, int lineLen); // Read next line from graph file }; #endif |
Additional hints |
Opening a file for reading// Assume filename is a character array passed to this function // containing the name of the file to open. ifstream inFile; // Define a reference to an input file // Try to open the file with the graph data inFile.open(filename, ifstream::in); if(!inFile.is_open()) { // inFile.is_open() returns false if the file could not be found or // if for some other reason the open failed. cout << "Unable to open file " << filename << ". \nProgram terminating...\n"; return 0; } |
Calling the function getNextLine()getNextLine(line, 80); // Assumes a character array char line[81]; |
The function getNextLine()//------------------------------------------- // GetNextLine() // Read the next line from the file. // It is assumed the ifstream object, inFile, // is a private class member and that the // file has already been successfully opened. //------------------------------------------- bool getNextLine(char *line, int lineLen) { int done = false; while(!done) { inFile.getline(line, lineLen); if(inFile.good()) // If a line was successfully read { if(strlen(line) == 0) // Skip any blank lines continue; else if(line[0] == '#') // Skip any comment lines continue; else done = true; // Got a valid data line so return with this line } else { strcpy(line, ""); return false; // Flag end of file with null string and return false } } // end while return true; // Flag successful read } |