我有一个用 C++ 实现邻接矩阵的程序。
这是我的代码-
#include<iostream>
using namespace std;
void addEdge(int * add){
cout<<"Edge added successfully.\n";
*add = 1;
}
int main(){
int vertex;
cout<<"Enter number of vertices in the graph: ";
cin>>vertex;
int adjMat[vertex][vertex];
for(int i=0;i<vertex;i++){
for(int j=0;j<vertex;j++){
adjMat[i][j] = 0;
}
}
cout<<"\n";
cout<<"Add edges (from and to): ";
int from,to;
while(cin>>from>>to){
if((from >= vertex || from < 1) || (to >= vertex || to < 1)){
cout<"Warning: Out of bound index.\n";
}else{
addEdge(&adjMat[--from][--to]);
}
}
cout<<"\n";
for(int i=0;i<vertex;i++){ …Run Code Online (Sandbox Code Playgroud)