Question
This program is written in c++, I need help correcting my code so that i get the desired output. below are the instructions along with
This program is written in c++, I need help correcting my code so that i get the desired output. below are the instructions along with the code I have written for the project based on said instructions, the output from my program, and the desired output I need. PLEASE HELP, THANKYOU!!
this is my code:
#include
#include
#include
#include
class LinkedNodes {
private:
int val;
std::vector
public:
LinkedNodes(int value) : val(value) {}
// Function to establish a two-way connection between nodes
void setNext(LinkedNodes* node) {
connections.push_back(node);
node->connections.push_back(this);
}
// Recursive function to find a path from the current node to the target node
std::string findPathTo(int targetVal, std::vector
if (val == targetVal) {
return std::to_string(val);
}
visited.push_back(val);
for (auto& conn : connections) {
if (std::find(visited.begin(), visited.end(), conn->getVal()) == visited.end()) {
std::string path = conn->findPathTo(targetVal, visited);
if (path != "No path") {
return std::to_string(val) + "->" + path;
}
}
}
visited.pop_back();
return "No path";
}
// Wrapper function to find a path without pre-existing visited nodes
std::string findPathTo(int targetVal) {
std::vector
return findPathTo(targetVal, visited);
}
// Function to get the value of the current node
int getVal() const {
return val;
}
};
int main() {
const int NUM_NODES = 9;
std::vector
// Create nodes
for (int i = 1; i
nodes.emplace_back(i);
}
// Establish connections between nodes to reflect the required paths
nodes[0].setNext(&nodes[1]);
nodes[0].setNext(&nodes[2]);
nodes[0].setNext(&nodes[3]);
nodes[0].setNext(&nodes[4]);
nodes[1].setNext(&nodes[5]);
nodes[1].setNext(&nodes[6]);
nodes[2].setNext(&nodes[7]);
nodes[2].setNext(&nodes[8]);
// Print the paths between nodes
for (int i = 0; i
for (int j = 0; j
std::cout
if (j
std::cout
}
}
std::cout
}
return 0;
}
here is the output from my code:
this is the desired output:
value is one. (See the example below) Your Answer: \begin{tabular}{lllllllllll} 1 & 1>2 & 1>3 & 1>4 & 1>5 & 1>2>6 & 1>2>7 & 1>3>8 & 1>3>9 & \\ 2>1 & 2 & 2>1>3 & 2>1>4 & 2>1>5 & 2>6 & 2>7 & 2>1>3>8 & 2>1>3>9 & 3>9 \\ 3>1 & 3>1>2 & 3 & 3>1>4 &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& 9>4 & 9>1>5 & 9>2>6 & 9>3>7 & 9>4>8 \end{tabular}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