Question: Edit the c + + code so that the output looks like this: Sample Run: Nodes [ 2 - 2 6 ] : 7 Edges:

Edit the c++ code so that the output looks like this:
Sample Run:
Nodes [2-26]: 7
Edges: 8
Edge values: HC GH AB BF BC FG CD DA
The network:
A--B-------F
|||
D--C--H---G
Run Again (Y/N): n
#include
#include
#include
#include
#include
using namespace std;
// Function to validate and parse the edge input
bool parseEdges(int n, map>& adjList){
string edge;
for (int i =0; i < n; ++i){
cin >> edge;
if (edge.length()!=2|| edge[0]== edge[1]|| edge[0]<'A'|| edge[0]>'Z'|| edge[1]<'A'|| edge[1]>'Z'){
cout << "Invalid edge input: "<< edge << endl;
return false;
}
// Assuming undirected graph, add edge in both directions
adjList[edge[0]].insert(edge[1]);
adjList[edge[1]].insert(edge[0]);
}
return true;
}
// Function to draw the network based on the adjacency list
void drawNetwork(const map>& adjList){
vector> grid(2*('Z'-'A')+3, vector(4*('Z'-'A')+5,''));
// Draw horizontal edges
for (auto& node : adjList){
char currentNode = node.first;
for (char connectedNode : node.second){
if (currentNode < connectedNode){
int x =1*(currentNode -'A')+0;
int y =2*(connectedNode -'A')+1;
grid[x][y]='-';
grid[x][y +1]='-';
}
}
}
// Draw vertical edges
for (auto& node : adjList){
char currentNode = node.first;
for (char connectedNode : node.second){
if (currentNode < connectedNode){
int x =2*(currentNode -'A')+2;
int y =4*(connectedNode -'A')+1;
grid[x][y]='|';
}
}
}
// Add corner labels
grid[0][1]='A';
grid[0][2*('Z'-'A')+2]='B';
grid[('Z'-'A')+1][1*('Z'-'A')+0]='C';
grid[1*('Z'-'A')+1][0]='D';
grid[0][1*('Z'-'A')+2]='E';
grid[0][1*('Z'-'A')+2]='F';
grid[1*('Z'-'A')+0][1*('Z'-'A')]='G';
grid[1*('Z'-'A')+0][1]='H';
// Print the grid
for (int i =0; i < grid.size(); ++i){
for (int j =0; j < grid[i].size(); ++j){
cout << grid[i][j];
}
cout << endl;
}
}
// Main function
int main(){
char runAgain ='Y';
do {
int m, n;
cout << "Nodes [2-26]: ";
cin >> m;
if (m <2|| m >26){
cout << "Invalid number of nodes. Please enter a value between 2 and 26.";
continue;
}
cout << "Edges: ";
cin >> n;
if (n <=0){
cout << "Invalid number of edges. Please enter a positive number.";
continue;
}
map> adjList; // Adjacency list to store the graph
cout << "Edge values: ";
if (!parseEdges(n, adjList)){
cout << "Error parsing edges. Please try again.";
continue;
}
// Drawing the network
cout <<"
The network:
";
drawNetwork(adjList);
// Clear the input buffer
cin.ignore(numeric_limits::max(),'
');
// Asking the user if they want to run the program again
cout << "Run Again (Y/N): ";
cin >> runAgain;
runAgain = toupper(runAgain);
} while (runAgain =='Y');
return 0;
}

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