Question: C + + program to draw a network as a two dimensional diagram giving bad output, please help. Code: #include #include #include #include #include using

C++ program to draw a network as a two dimensional diagram giving bad output, please help.
Code:
#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')+1, vector(2*('Z'-'A')+1,''));
// Draw horizontal edges
for (auto& node : adjList){
char currentNode = node.first;
for (char connectedNode : node.second){
if (currentNode connectedNode){
int x =2*(currentNode -'A');
int y =2*(connectedNode -'A');
grid[x][min(y, y +1)]='-';
}
}
}
// Draw vertical edges
for (char c ='A'; c ='Z'; ++c){
if (adjList.count(c)>0){
int x =2*(c -'A');
int y =0;
for (char d ='A'; d ='Z'; ++d){
if (c d && adjList.at(c).count(d)>0){
grid[min(x, x +1)][y]='|';
}
y +=2;
}
}
}
// Print the grid
for (int i =0; i grid.size(); ++i){
for (int j =0; j grid.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." endl;
continue;
}
cout "Edges: ";
cin >> n;
if (n =0){
cout "Invalid number of edges. Please enter a positive number." endl;
continue;
}
map> adjList; // Adjacency list to store the graph
cout "Edge values: ";
if (!parseEdges(n, adjList)){
cout "Error parsing edges. Please try again." endl;
continue;
}
// Drawing the network
cout "
The network:
";
drawNetwork(adjList);
// 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;
}
C + + program to draw a network as a two

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!