Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in C++ Create a menu driven application which will provide the following o

ID: 3694642 • Letter: W

Question

Write in C++

Create a menu driven application which will provide the following options to the users:

1. Insert: This option will allow users to add an edge to the existing graph model. To add an edge, take two vertices (the edge is from first to second) and the edge weight as input from the user, update the adjacency matrix using that two vertices as index. If an edge already exists, notify the user by saying ”edge already exists!”

2. Delete: This option will allow users to delete an edge to the existing graph model. To delete an edge, take two vertices as input from the user, update the adjacency matrix using that two vertices as index. If there is no existing edge, notify the user by saying ”no edge available!”

3. Show total number of edges: Show total number of edges using the adjacency matrix.

4. Show the matrix: Show the updated adjacency matrix in the console.

5. Exit

Explanation / Answer

#include <iostream>
using namespace std;
int getInt(char c) {
   switch (c) {
       case 'A': return 0;
       case 'B': return 1;
       case 'C': return 2;
       case 'D': return 3;
       case 'L': return 4;
       case 'M': return 5;
       case 'N': return 6;
       case 'S': return 7;
   }
}

int main() {
   int matrix[8][8] =
   {
       {0, 0, 606, 0, 0, 0, 0, 0},
       {0, 0, 860, 0, 0, 0, 0, 0},
       {0, 0, 0, 0, 0, 0, 0, 0},
       {0, 0, 908, 0, 0, 0, 0, 0},
       {0, 0, 0, 834, 0, 0, 0, 349},
       {595, 0, 0, 0, 0, 0, 1090, 0},
       {760, 191, 722, 0, 2451, 0, 0, 2534},
       {0, 0, 1855, 957, 349, 0, 0, 0}
   };
   int choice, numOfEdges = 15;
   char a, b;
   int distance;
  
   do {
       cout << "1 - Insert" << endl
   << "2 - Delete" << endl
   << "3 - Show total number of edges" << endl
   << "4 - Show the matrix" << endl
   << "5 - Exit" << endl;

  
   cin >> choice;
       switch (choice) {
       case 1:
       {cout << "Enter origin city ";
               cin >> a;
               cout << "Enter destination city ";
               cin >> b;
               if (matrix[getInt(a)][getInt(b)] != 0)
                   cout << "Edge already exits!";
               else {
                   cout << "Enter distance ";
                   cin >> distance;
                   matrix[getInt(a)][getInt(b)] = distance;
                   numOfEdges++;
               }
               break;}

       case 2:
       {cout << "Enter origin city ";
               cin >> a;
               cout << "Enter destination city ";
               cin >> b;
               if (matrix[getInt(a)][getInt(b)] == 0)
                   cout << "No edge available!";
               else {
                   matrix[getInt(a)][getInt(b)] = 0;
                   numOfEdges--;
               }
               break;}

       case 3:
       {cout << "Number of edges = " << numOfEdges << endl;
               break;}

       case 4:
       {for (int i = 0; i < 8; i++) {
                   for (int j = 0; j < 8; j++) {
                       cout << matrix[i][j] << " ";
                   }
                   cout << endl;
               }
               break;}
   }
} while ( choice != 5);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote