Question
In C++ Could really use come help with adding a loop to find the array location of each source node. Hint: Use get_value() to compare
In C++
Could really use come help with adding a loop to find the array location of each source node. Hint: Use get_value() to compare a node's value to source_name and If the node does not exist, add it to the array using the set_value() function and incrementing n.Save index.
Comment out the output lines in the file input loop in main().1.Test program to verify that your array has been populated:
Sample Run
Enter file name: Data.txt
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Note that Saturday is not shown
Node.h
// Node Declarations
#define ERR-1
#define NODE_MAX20
#define EDGE_MAX4
// Node class
class node
{ public:
node();// Constructor
void set_value(string);// Set string value
string get_value();// Return string value
void connect(node *);// Connect this node to another
void put(ostream &);// Output node and neighbors
private:
string value;// Node value
node *edge[EDGE_MAX];// Edges array
};
Node.cpp
#include
#include
using namespace std;
#include "Node.h"
/******************************
* Null constructor
******************************/
node::node()
{ int i;
value = "";
for(i=0;i edge[i] = NULL; } /****************************** * set_value() ******************************/ void node::set_value(string arg) { value = arg; } /****************************** * get_value() ******************************/ string node::get_value() { return value; } Main.cpp #include #include #include #include using namespace std; #include "Node.h" /****************************** * main() ******************************/ void main() { int i,n; int source_index,target_index,distance; string fname,source_name,target_name; fstream in; node map[NODE_MAX]; // Initialize n = 0; cout << left; // Get file name cout << "Enter file name: "; cin >> fname; // Open file in.open(fname,ios::in); // Loop through file while(!in.eof()) { in >> source_name >> target_name >> distance; // add loop here // Add to array if(in.good()) { cout << setw(12) << source_name; cout << setw(12) << target_name; cout << setw(4) << distance; cout << endl; }; }; // Close file in.close(); // Display array for(i=0;i cout << map[i].get_value() << endl; } In the data file each line contains the name of the source node, target node, and distance.The program reads from each line a source node, target node, and distance. Data.txt SundayMonday10 SundayTuesday20 MondayThursday30
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started