-1

Help me to find the error in Prim code, thanks.

```
#include <iostream.h>
#include <stdio.h>
#define MAX 10
class prims {
private:
int cost[MAX][MAX], tree[MAX][MAX]; int n;
public:
void readmatrix();
int spanningtree (int src);
void display (int cost);
};
void prims :: readmatrix() {
int i, j, k; n = 3; k = 0;
int x[9] = { 9, 19, 21, 11, 8, 17, 15, 10, 5};
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cost[i][j] = x[k]; k++;
printf ("A[%d][%d] = %d ", i, j, cost[i][j]); }
printf ("\n"); }
}
int prims :: spanningtree (int src) {
int visited[MAX], d[MAX], parent[MAX];
int i, j, k, min, u, v, stcost;
for (i = 1; i <= n; i++) {
d[i] = cost[src][i]; visited[i] = 0;
parent[i] = src; }
visited[src] = 1; stcost = 0; k = 1;
for (i = 1; i < n; i++) {
min = 999;
for (j = 1; j <= n; j++) {
if (!visited[j] && d[j] < min) {
min = d[j]; u = j; }
}
visited[u] = 1;
stcost = stcost + d[u];
tree[k][1] = parent[u];
tree[k++][2] = u;
for (v = 1; v <= n; v++)
if (!visited[v] && (cost[u][v] < d[v])) {
d[v] = cost[u][v]; parent[v] = u; }
}
return (stcost);
}
void prims :: display (int cost) {
int i;
cout << "\n The Edges of the Mininum Spanning Tree are \n";
for (i = 1; i < n; i++)
cout << tree[i][1] << " " << tree[i][2] << endl;
cout << "\n The Total cost of the Minimum Spanning Tree is : " << cost;
}
int main() {
int source, treecost; prims pri;
pri.readmatrix();
cout << "\n Enter the Source : "; cin >> source;
treecost = pri.spanningtree (source);
pri.display (treecost); return 0;
}
```