Project 1: MSVS 2005, C++, Pointers, Memory Allocation

ITCS x120 Computer Graphics Fall 2008 Revision 1.4 Professor Zachary Wartell

Last Modified: 1/19/2018 12:56:59 PM

Copyright August 2006, Zachary Wartell @ University of North Carolina at Charlotte All Rights Reserved.

Due Date: Part I: Wednesday September 24, 2008 11:59PM. Part II: Wednesday October 1, 2008 11:59PM.

1. Overview

Project 1 involves:  Working through a tutorial on SVN  Working through a tutorial on Microsoft Visual Studio 2005 (MSVS 2005)  Working through a tutorial on one of two memory leak detection tools  Writing a C++ program

The overall purpose is for students to familiarize themselves with MSVS 2005 as much as possible and to acquaint themselves with C++ and programming with pointers (including function pointers), dynamic memory allocation, etc.

Review the class syllabus regarding the basic requirements for getting full and partial credit for your programming assignments [1].

2. Part I: “Hello World”

From the class website [1] get the file:

SVN for ITCS 4120-5120.doc and read the listed chapters in the SVN manual and the tutorial. There is no deliverables for this tutorial.

From the class website [1] get the file:

“ C++ and MSVS 2005--A Tutorial and Guide to further Reading.doc” The above document has multiple deliverables in addition to reading. By and large these deliverables are about making modifications to C++ code and turning those modifications and turning answers to various questions.

3. Part II: Simple File Parser

For Part II you will write a (non-graphical) program in C++. Skeleton code for this project is provided in your SVN repository directory under:

‘Project 1/ITCS 4120 – Tree’

Use this skeleton code. Also you must link with the VLD library discussed in the tutorial above. The distributed compilation scripts already do this for you.

For Part II you will write a (non-graphical) program that reads in a text file describing a tree data structure and then prints out the tree using a pre-order traversal of the tree and performs some computational operation as each node in the tree is visited. (If necessary, brush up on tree traversal [3] from your data structures and/or algorithms course. Recall, ‘pre-order’ means you visit/operate on the current node before visiting/operating on its children). The program will construct, traverse, and destruct the tree using recursive function calls. The program will dynamically allocate and deallocate nodes in the tree using C++ new and delete. Each node in the tree can have a different number of child nodes.

Your program should include the following classes and functions:

/** \brief 'Tree' is a simple Tree class */ class Tree { public: /** NodeVisitFunction is a function pointer type */ typedef void (*NodeVisitFunction) (Tree*);

public: Tree(); ~Tree();

void traversePreOrder (NodeVisitFunction nvf); void load(std::fstream& file);

/** a text name for this node */ std::string name;

… further details removed, see Tree.h … };

/** \brief prints the message "Hello ", where is the name of the Tree node 't' */ void SayHello(Tree*t);

/** \brief prints the message "Goodbye ", where is the name of the Tree node 't' */ void SayGoodbye(Tree*t); Your program should assume the text file that describes the tree(s) is called “Tree.txt”. “Tree.txt” may describe one or more trees. Your program should allocate, create, print and then deallocate each of the trees described in the Tree.txt file. If Tree.txt contains multiple trees each tree should be processed separately and the Tree data structure should be completely deallocated after each tree is processed. The contents of each tree should be printed twice by the pre-order traversal of the tree nodes. The first traversal should print the message “Hello ” on a separate line for each node in the tree, where is the name of the tree node. The second traversal should print the message “Goodbye ” on a separate line for each node in the tree. These traversals and printouts should be implemented using the ‘traversePreOrder’ member function which takes a pointer to the ‘SayHello’ or ‘SayGoodbye’ function. These latter two functions just print the appropriate message for a single node. The purpose here is to implement and use a simple function pointer callback mechanism.

The input file format grammar is as follows. In this grammar, a | separates different options; * denotes 0 or more repetitions of a terminal or non-terminal symbol.

FILE := TREE* TREE := { STRING_NAME TREE* } STRING_NAME := this is a string made of only letters, numbers or the _ character

For example the following is a valid file (The SVN checkout includes other examples as well). Note, all whitespace (space, tabs, newlines) are treated the same in the grammer as in any normal programming language.

The example below contains 4 trees:

{ ROOT } { ROOT { FRED } } { ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } } { ROOT { FRED { FRED_JUNIOR1} { FRED_JUNIOR2} } { BARNEY { BARNEY_JUNIOR1} { BARNEY_JUNIOR2} } }

Hints:  Useful C++ functions for parsing: isspace, isalnum, etc., fstream.get(),fstream.peek()  The simplest way to parse this file’s grammar is using a recursive descent parser. Hopefully stuff you’ve seen in programming languages, or compilers or automata theory courses or see [4] (Note, the example in [4] is 10 times more complicated than what you need here).

4. Citations

[1] http://www.cs.uncc.edu/~zwartell/ITCS 4120-5120/current/web page/ITCS 4120 - Wartell.html . ITCS 4120/5120, Zachary Wartell. [2] Zachary Wartell, “C++ and MSVS 2005--A Tutorial and Guide to further Reading.doc” [3] Tree Traversal http://en.wikipedia.org/wiki/Tree_traversal [4] Recursive descent parser. http://en.wikipedia.org/wiki/Recursive_descent_parser

5. Appendix I: Grading Instructions

As always, see syllabus regarding partial credit.

Total is out of 100 pts.

Part I: Sub-total is 25% (.25) of Project Grade Each subpart is 0.50 of the Part 1’s subgrade

Part II: Sub-total is 75% (0.75) of Project 1 Grade.

Then further break down is as follows [.xx] means the associate portion of the assignment is worth xx percent of the total sub-grade.

1) [.15] File Parsing – program reads the specified input file format 2) [.42] Tree Data Structure a. [.42*.33] tree structure uses pointers for parent & child nodes and supports varying number of child nodes per node b. [.42*.33] tree structure is dynamically allocated when built from input file c. [.42*.34] tree structure is deallocated after print out and before reading next tree. 3) [.43] Print out a. [.43*.20] tree is printed out appropriately b. [.43*.40] callbacks i. traversal uses 2 different callback functions ii. traversal function executes callback function as it visits each node c. [.43*.40] traversal is done recursively

6. Appendix II: History

Revision 1.1 – -Fixed typo in ‘Tree** children’ data member (Tree* children made no sense). -clarified that each node in the tree can have a different number of child nodes. -removed pile of junk accidentally pasted into this document Revision 1.4 - too tedious to maintain this… see SVN log