Question: must be in C++ Week 1: Class Creation In the first week you will make a MyNode class and hook up the nodes in a

must be in C++

Week 1: Class Creation In the first week you will make a MyNode class and hook up the nodes in a similar way to what you did in A1. The difference is the list of connected nodes will be pointers to other nodes (like our LinkedList example from class). We wont do that ID lookup thing again. You will also now be using classes instead of a structure. 0) We will continue to use the graph reading code from A1 BUT we will use constructors to make each node. 1) The calls to MyNode functions will cause an error. Your job is to define MyNode.cpp (prototypes are in MyNode.h) to make sure those functions work. 2) The provided text file CAN now have errors in it. In the case of an error, we want to elegantly handle the issue by outputting a meaningful message AND by not breaking the graph. An assert to alert the programmer would also be acceptable. For example, if node -2 is supposedly connected to node 1, our system would stop working in A1. Now we can have node 1 output an error message but not attempt to add node -2. What should happen if the same node is added as a neighbor more than once? The provided GraphFormat.txt (to add) file is not currently broken but when we test your code, it may be. 3) Write your first initial and name last name (each person) at the top of your CPP with the main method.I have my reasons. You will lose 10% if you dont. 4) You will print out the graph to the console (so outputting text). Again, each node should know how to print information related to itself (in this case, a line of text indicating what nodes are connected to it and its own ID). 5) To test your code, you should read in the text file, make the array of MyNode objects and then output to the console what the graph looks like.

use the provided code:

#include "MyNode.h" using namespace std; /////////////////// // Prototypes unsigned int tabCountLine(char* p_nextField); unsigned int readNumLine(char* toRead, unsigned int* dstNumList, unsigned int dstSize); unsigned int readNumNodes(char* firstLine, unsigned int* dstNodeList, unsigned int dstSize); MyNode* getNode(MyNode** nodeList, unsigned int numNodes, unsigned int srcId); int connectNodes(MyNode** nodeList, unsigned int numNodes, unsigned int srcId, unsigned int* nearIdxList, int numEdges); MyNode** readGraphFile(const char* fileName, int* numNodesOut); IdList* findPath(MyNode* src, MyNode* dst); int main() { int numNodes = -1; MyNode** myNodes = readGraphFile("GraphFormat.txt", &numNodes); std::cout printPath(); delete p_path62; } else { cout printNode(); } // TODO: Now release the dynamic array // You need to delete each of the nodes // one by one. return 0; } // Function that purely counts the number of tabs on the line // (it does not get alter the text) // This function means we don't need to assume a maximum graph size. // If you add 1 the size should be bigger than any graph you find. unsigned int tabCountLine(char* p_nextField) { unsigned int numNodes = 0; // While there are tabs to be found, keep finding them and removing them p_nextField = strchr(p_nextField, '\t'); while (p_nextField) { // Remove the tab and shift the pointer one over. p_nextField++; numNodes++; p_nextField = strchr(p_nextField, '\t'); } return numNodes; } // Helper function (you can use) // Read in a series of numbers separated by tabs. unsigned int readNumLine(char* toRead, unsigned int* dstNumList, unsigned int dstSize) { assert(toRead); // Remove the first character if it is a tab. while (toRead[0] == '\t') { toRead++; } char* p_nextField = toRead; unsigned int numNodes = 0; int numChars = strlen(toRead); // While there are tabs to be found, keep finding them and removing them p_nextField = strchr(p_nextField, '\t'); while (p_nextField) { // Remove the tab and shift the pointer one over. p_nextField[0] = 0; p_nextField++; //Extract the field dstNumList[numNodes] = atoi(toRead); numNodes++; // Update the start and end point of the next field. toRead = p_nextField; p_nextField = strchr(p_nextField, '\t'); } dstNumList[numNodes] = atoi(toRead); numNodes++; return numNodes; } // readNumNodes(char* firstLine) // takes first line of text is in the format "Nodes: # # # # # #" // where the numbers are the node IDs (not in a particular order) // and separated by one or more spaces. the destination array and array size // are also passed as parameters. The destination array is then filled // readNumNodes returns the number of nodes in the array. unsigned int readNumNodes(char* firstLine, unsigned int* dstNodeList, unsigned int dstSize) { char* cPtr = strchr(firstLine, (int)':'); cPtr += 1; return readNumLine(cPtr, dstNodeList, dstSize); } // TODO: Write a helper function to find a particular node based on the id. // This is just to make your life easier when you try to find a particular node. MyNode* getNode(MyNode** nodeList, unsigned int numNodes, unsigned int srcId) { // FIX THIS CODE return NULL; } // GIVEN: A helper function to read each line of edges // This is MUCH easier to read than sticking everthing in a for loop // Functions let us separate concepts int connectNodes(MyNode** nodeList, unsigned int numNodes, unsigned int srcId, unsigned int* nearIdxList, int numEdges) { // Get the node to add edges to MyNode* srcNode = getNode(nodeList, numNodes, srcId); // TODO: Make sure srcNode is actually one of the nodes // If not, return an message to the user and don't change anything. //Sanity check. You shouldn't have the same node have multiple lines in the text file. //Therefore it starts at 0 edges. assert(srcNode->getNumEdges() == 0); //TODO: Now, get the node associated with each id in nearIdxList and ADD it // (not just the ID) to the source node's list of NEIGHBORs // If that id doesn't correspond with a node you can handle it by ignoring the issue, // causing an assert failure, or outputting a message to the user. // ...... return srcNode->getNumEdges(); } /* GIVEN: This function (from A1) reads the information from GraphFormat.txt We no longer guarantee the text file is properly formatted...and we may test your code by changing GraphFormat a bit. You can (and should) make your own text file to test your code. */ MyNode** readGraphFile(const char* fileName, int* numNodesOut) { const int TXT_BUFF_SIZE = 100; char txtBuffer[TXT_BUFF_SIZE]; FILE* filePtr; fopen_s(&filePtr, fileName, "r"); if (filePtr == NULL) { printf("File to read in does not exist it seems."); return NULL; } unsigned int numLines = 0; // fgets reads text until newline. Repeat until the end of the file // Notice there are many ways one can do this. fscanf_s also works and is useful // to just get a sub-part of a line (readNumLine could use scanf_s) char* textPtr = fgets(txtBuffer, TXT_BUFF_SIZE, filePtr); numLines++; assert(textPtr); // Find out the number of tabs to get approximately num nodes. unsigned int numNodes = tabCountLine(txtBuffer); unsigned int* nodeIdxList = new unsigned int[numNodes]; // Read the first line to determine the nodes readNumNodes(txtBuffer, nodeIdxList, numNodes); //TODO: Make your array of nodes (don't connect them). // You SHOULD use new[]/delete[] this time. MyNode** nodeList = NULL;// FIX THIS //GIVEN: Read each remaining line in the text file (you can assert that the number of lines // will be  1); connectNodes(nodeList, (int)numNodes, nodeIdxList[0], nodeIdxList + 1, numEdges - 1); numLines++; // Get the next line in the file textPtr = fgets(txtBuffer, TXT_BUFF_SIZE, filePtr); } assert((int)numLines getPath(dst, visited); delete visited; return path; }

also please use:

must be in C++ Week 1: Class Creation In the first week

also please use:

// MyNode includes ALL the header information #include "MyNode.h" /////////////////////////////////////////////////////////////////////// // Notice: you do not need to look at this file at all...seriously. // Just make a new IdList, add Ids, and check if an ID is in the list // You don't need to know anything else....although addID might be useful // for demonstrating how to dynamically increase the size of an array. IdList::IdList() { idArray = new unsigned int[START_ID_ARRAY_SIZE]; numIDs = 0; maxNumIDs = START_ID_ARRAY_SIZE; } IdList::~IdList() { delete[] idArray; numIDs = 0; maxNumIDs = 0; } int IdList::addID(unsigned int id) { if (hasID(id)) { return numIDs; } // If not in the list, then add it to the end assert(numIDs  

also please use:

#pragma once /* IdList is a class to store a set of node IDs without duplicates. It is designed so you can add an id (without worrying about running out of space) and you can check if the id is already in the set (hasID). */ class IdList { private: unsigned int* idArray; unsigned int numIDs; int maxNumIDs; const int START_ID_ARRAY_SIZE = 10; public: //IdList Constructor (called when you call new) IdList(); //IdList Destructor (called when you call delete) ~IdList(); // Add the given ID to the set if and only if it is not // already in the set int addID(unsigned int id); // hasID returns true if the given id is already in the set // of ID and false otherwise. bool hasID(unsigned int id); // Simple function for printing the IDs in the set to the console. void printPath(); }; 

also please use:

#pragma once // One way to ensure this .h file is included only once in a project // All the libraries and .h files you need for this project in one place // Now get all .cpp files to include "MyNode.h" instead of figuring out // what to include on a file-by-file basis. #include "IdList.h" #include  #include  #include  #include  class MyNode { private: //TODO: Add private functions and member variables *as needed* // I am giving the expected variables required to simplify your work. unsigned int id; // The number of edges / connections you currently have unsigned int numEdges; // The size of the dynamic array int maxConnections; // The dynamic array of MyNode pointers. Use this to go to another node. MyNode** connNodes; const int DEFAULT_MAX_CONNECTIONS = 20; // Helper Function: duplicate the array of connections // so the array is double the size. Notice this is a PRIVATE function int dupConnNodes(); public: //TODO: Your job is to implement all the functions below in MyNode.cpp // Constructor MyNode(unsigned int id); // Destructor ~MyNode(); // Get the ID of the node. unsigned int getID(); // Unused but good example: set the ID for the node // ensuring the ID is appropriate void setID(int id); // Connect the current node with the node toAdd so // they are now neighbors. There is no upper limit on the number of neighbors. int addNeighbor(MyNode* toAdd); // Return the pointer for a neighboring node given its ID. // If no node with that ID is connected to the current node, return NULL MyNode* getNeighbor(int id); // Return the number of edges and thus // the number of nodes connected to this node int getNumEdges(); // visited should be initialized (ie make a new one) BEFORE the first call to // getPath is made // First we check to see if the destination is directly connected to the current node. // Next we will figure out if there is a path between each neightbour and the destination // by calling getPath on each neighbor. The first time a path is found, we then add // the current node to the path and return the path. // If a path is not found between the current node and dst then return NULL. // For example, in the given file the path from ID 3 to ID 7 is done by // finding the path from 1 to 7 (1 is the only neighbour) and then adding // 3 to the path. If there was no path between 1 (p_node1) and 7 then // p_node1->getPath(dst, visited) would return NULL....and no neighbor // has a path to the destination, what can you conclude? // Note: we are keeping a list of visited nodes around so we don't go in circles. // If already visited, then don't re-visit(ie call getPath) on the node. // Notice that an IdList is great for keeping the nodes already visited // and returning the path to the destination. IdList* getPath(MyNode* dst, IdList* p_visited); // Print the node information to the console. // This should be a simple line of text like "Node 4: 3 5 6" void printNode(); };

c++ please; do as much as possible

Node IDs: _4 _5 _6 7 _ 67 Node IDs: _4 _5 _6 7 _ 67

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!