Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. int main(void)
  7. {
  8. char* arrayPointer = NULL;
  9. int dynamicSize, point1, point2;
  10. float input = 0;
  11. string key;
  12.  
  13. cout << "How many vertices? ";
  14. cin >> dynamicSize;
  15. cout << endl;
  16.  
  17. arrayPointer = new char[dynamicSize];
  18. int** graph = new int*[dynamicSize];
  19.  
  20. for (int i = 0; i < dynamicSize; i++) {
  21. graph[i] = new int[dynamicSize];
  22. }
  23.  
  24. for (int i = 0; i < dynamicSize; i++)
  25. {
  26. for (int j = 0; j < dynamicSize; j++)
  27. {
  28. graph[i][j] = 0;
  29. }
  30. }
  31.  
  32. for (int i = 0; i < dynamicSize; i++) {
  33. cout << "What is the label for vertex " << i+1 << "? ";
  34. cin >> arrayPointer[i];
  35. }
  36.  
  37. cout << "Define an edge by listing a pair of vertices, i.e. \"AB\", or -1 to stop." << endl;
  38. cin >> key;
  39. while (key != "-1")
  40. {
  41. for (int i = 0; i < dynamicSize; i++)
  42. {
  43. if (arrayPointer[i] == key[0])
  44. {
  45. point1 = i;
  46. break;
  47. }
  48. }
  49. for (int i = 0; i < dynamicSize; i++)
  50. {
  51. if (arrayPointer[i] == key[1])
  52. {
  53. point2 = i;
  54. break;
  55. }
  56. }
  57.  
  58. cout << "Added edge " << key[0] << "->" << key[1] << endl;
  59. graph[point1][point2] = 1;
  60. cout << "Define an edge by listing a pair of vertices, i.e. \"AB\", or -1 to stop." << endl;
  61. cin >> key;
  62. }
  63. cout << "Your edges are: ";
  64. for (int i = 0; i < dynamicSize; i++)
  65. {
  66. for (int j = 0; j < dynamicSize; j++)
  67. {
  68. if (graph[i][j] == 1)
  69. {
  70. cout << arrayPointer[i] << arrayPointer[j] << " ";
  71. }
  72. }
  73. }
  74. system("pause");
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement