15,568,965 members
1.00/5 (1 vote)
See more:
No ouput from this code its its not generating any output just getting compiled. I am not able to find the error

What I have tried:

C++
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// a structure to represent a weighted edge in graph
struct Edge {
int src, dest, weight;
};

// a structure to represent a connected, undirected
// and weighted graph
struct Graph {
// V-> Number of vertices, E-> Number of edges
int V, E;

// graph is represented as an array of edges.
// Since the graph is undirected, the edge
// from src to dest is also edge from dest
// to src. Both are counted as 1 edge here.
struct Edge* edge;
};

// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*)(malloc(sizeof(struct Graph)));
graph->V = V;
graph->E = E;

graph->edge = (struct Edge*)malloc(sizeof( struct Edge));

return graph;
}

// A structure to represent a subset for union-find
struct subset {
int parent;
int rank;
};

// A utility function to find set of an element i
// (uses path compression technique)
int find(struct subset subsets[], int i)
{
// find root and make root as parent of i
// (path compression)
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);

return subsets[i].parent;
}

// A function that does union of two sets of x and y
// (uses union by rank)
void Union(struct subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);

// Attach smaller rank tree under root of high
// rank tree (Union by Rank)
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;

// If ranks are same, then make one as root and
// increment its rank by one
else
{
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}

// Compare two edges according to their weights.
// Used in qsort() for sorting an array of edges
int myComp(const void* a, const void* b)
{
struct Edge* a1 = (struct Edge*)a;
struct Edge* b1 = (struct Edge*)b;
return a1->weight > b1->weight;
}

// The main function to construct MST using Kruskal's
// algorithm
void KruskalMST(struct Graph* graph)
{
int V = graph->V;
struct Edge
result[V]; // Tnis will store the resultant MST
int e = 0; // An index variable, used for result[]
int i = 0; // An index variable, used for sorted edges

// Step 1: Sort all the edges in non-decreasing
// order of their weight. If we are not allowed to
// change the given graph, we can create a copy of
// array of edges
qsort(graph->edge, graph->E, sizeof(graph->edge[0]),
myComp);

// Allocate memory for creating V ssubsets
struct subset* subsets
= (struct subset*)malloc(V * sizeof(struct subset));

// Create V subsets with single elements
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}

// Number of edges to be taken is equal to V-1
while (e < V - 1 && i < graph->E) {
// Step 2: Pick the smallest edge. And increment
// the index for next iteration
struct Edge next_edge = graph->edge[i++];

int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);

// If including this edge does't cause cycle,
// include it in result and increment the index
// of result for next edge
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}

// print the contents of result[] to display the
// built MST
printf(
"Following are the edges in the constructed MST\n");
int minimumCost = 0;
for (i = 0; i < e; ++i)
{
printf("%d -- %d == %d\n", result[i].src,
result[i].dest, result[i].weight);
minimumCost += result[i].weight;
}
printf("Minimum Cost Spanning tree : %d",minimumCost);
return;
}

// Driver program to test above functions
int main()
{

int V = 6; // Number of vertices in graph
int E = 9; // Number of edges in graph
struct Graph* graph = createGraph(V, E);

graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = 4;

graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 1;

graph->edge[2].src = 0;
graph->edge[2].dest = 3;
graph->edge[2].weight = 3;

graph->edge[3].src = 1;
graph->edge[3].dest = 2;
graph->edge[3].weight = 4;

graph->edge[4].src = 1;
graph->edge[4].dest = 3;
graph->edge[4].weight = 4;

graph->edge[5].src = 2;
graph->edge[5].dest = 3;
graph->edge[5].weight = 2;

graph->edge[6].src = 2;
graph->edge[6].dest = 5;
graph->edge[6].weight = 4;

graph->edge[7].src = 3;
graph->edge[7].dest = 5;
graph->edge[7].weight = 6;

graph->edge[8].src = 4;
graph->edge[8].dest = 5;
graph->edge[8].weight = 5;

KruskalMST(graph);

return 0;
}```
Posted
Updated 2-Jun-21 4:16am
v2
Richard MacCutchan 2-Jun-21 7:29am
You need to use the debugger to find out what is going on.
The Other John Ingram 2-Jun-21 8:22am
Just before you print out your results i would print out the value of 'e' and use print statements or breakpoints if you have them to figure out what is happening.
CHill60 2-Jun-21 9:16am
Runs fine for me and produces
```Following are the edges in the constructed MST
0 -- 2 == 1
2 -- 3 == 2
0 -- 4 == 0
5 -- 0 == 4
Minimum Cost Spanning tree : 7```
How are you trying to run it? If you are double-clicking it in windows try putting
`int c = getchar();`
before the return in your main routine.

## Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!