SabirSazzad

Adjacency Matrix

Feb 26th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int maxv = 'M'+1;
  8.  
  9. void initialize(int graph[maxv][maxv])
  10. {
  11.     int i, j;
  12.     for(i=0; i<maxv; i++)
  13.         for(j=0; j<maxv;j++)
  14.             graph[i][j]=0;
  15. }
  16. void read(int graph[maxv][maxv])
  17. {
  18.     ifstream inf;
  19.     inf.open("edge.dat");
  20.     char V1, V2;
  21.     int wt;
  22.     while(!inf.eof())
  23.     {
  24.         inf >> V1 >> V2 >> wt;
  25.         graph[V1][V2]=wt;
  26.         graph[V2][V1]=wt;
  27.     }
  28. }
  29. void print(int graph[maxv][maxv])
  30. {
  31.     ofstream outf;
  32.     outf.open("graph.out");
  33.     int i, j;
  34.     for(i=0; i<maxv; i++)
  35.     {
  36.         for(j=0; j<maxv; j++)
  37.             outf << graph[i][j] << " " << endl;
  38.     }
  39. }
  40. int main()
  41. {
  42.     int graph[maxv][maxv];
  43.  
  44.     initialize(graph);
  45.     read(graph);
  46.     print(graph);
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment