Question: IN C + + PLEASE I need help outputting the code correctly as seen in the image. The code should read two node IDs from

IN C++ PLEASE I need help outputting the code correctly as seen in the image. The code should read two node IDs from the console (one per line). Then, it must list the IDs of the resistors that are directly connected to the two nodes. For example, if the input is
N1
N2
then the output should be
R1
If the nodes are not adjacent (and therefore no resistors are connected to both nodes), a message should be printed as in the following example. Input:
N1
N5
Output:
No resistors found.
My Code:
#include
#include
#include
using namespace std;
struct Resistor {
string ID;
double resistance;
};
struct Node {
string ID;
bool ground;
vector connections; // Change vector type to Resistor*
};
void find_resistors(string n1, string n2, vector& nodes){
bool found = false;
for (const auto& node : nodes){
if (node.ID == n1|| node.ID == n2){
for (const auto& connection : node.connections){
for (const auto& other_node : nodes){
if (other_node.ID == n1|| other_node.ID == n2){
if (other_node.ID != node.ID){
for (const auto& other_connection : other_node.connections){
if (connection == other_connection){
cout connection->ID "";
found = true;
}
}
}
}
}
}
}
}
if (!found){
cout "No resistors found.";
}
cout endl;
}
int main(){
string n1, n2;
// Read two node IDs from cin
cin >> n1>> n2;
// Resistors
Resistor r1={"R1",2200.0};
Resistor r2={"R2",4700.0};
Resistor r3={"R3",10000.0};
Resistor r4={"R4",3300.0};
Resistor r5={"R5",4700.0};
Resistor r6={"R6",6800.0};
// Nodes
vector nodes(5);
nodes[0].ID ="N1";
nodes[0].ground = true;
nodes[0].connections.push_back(&r1);
nodes[0].connections.push_back(&r3);
nodes[1].ID ="N2";
nodes[1].connections.push_back(&r1);
nodes[1].connections.push_back(&r2);
nodes[2].ID ="N3";
nodes[2].connections.push_back(&r2);
nodes[2].connections.push_back(&r4);
nodes[2].connections.push_back(&r5);
nodes[3].ID ="N4";
nodes[3].connections.push_back(&r3);
nodes[3].connections.push_back(&r4);
nodes[3].connections.push_back(&r6);
nodes[4].ID ="N5";
nodes[4].connections.push_back(&r5);
nodes[4].connections.push_back(&r6);
// Call find_resistors
find_resistors(n1,n2, nodes);
return 0;
}
{:-=I:11=
x-TIn=grad||1||
11-=
II.?bar(3)
IN C + + PLEASE I need help outputting the code

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 Accounting Questions!