Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C To implement Prim’s algorithm to generate a min-cost spanning tree.
C++
#include
#include
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];

void main()
{
    clrscr();
    cout > n;
    cout > cost[i][j];
    if( cost[i][j]==0 )
       cost[i][j] = 999;
    }  // where is the starting brace ???

    visited[1]=1;
    cout << "\n";
    while( ne < n )
    {
        for( i=1, min=999; i <= n; i++ )
            for( j=1; j <= n; j++ )
                if( cost[i][j] < min )
                    if( visited[i] != 0 )
                    {
                        min=cost[i][j];
                        a=u=i;
                        b=v=j;
                    }

        if( visited[u] == 0 || visited[v] == 0 )
        {
            cout << "\n Edge: " << ne++ << "(" << a << "," << b << ") cost: " << min;
            mincost += min;
            visited[b] = 1;
        }
        cost[a][b] = cost[b][a] = 999;
    }
    cout << "\n Minimun cost= " << mincost;
    getch();
}


What I have tried:

Anyone, please explain this program line by line...
Posted
Updated 27-Jan-19 7:32am
v3
Comments
Richard MacCutchan 27-Jan-19 3:43am    
You don't need a line by line explanation since the first few lines will not even compile. I suggest you start by studying your C++ notes or manual.
Rick York 27-Jan-19 13:35pm    
Using variables named min or max is not a good idea because there are two commonly used macros with those names and you can have a collision resulting in broken code. Also, even if you don't need braces for single statements, it is a good idea to have them to enhance the clarity of the code. Plus, they will be there if/when you have to add statements inside the loops or conditionals.
Rick York 27-Jan-19 13:44pm    
Why should we spend the time explaining something to you when you can spend the same time yourself learning it? You need to work through a c++ tutorial or find someone willing to tutor you in the language. Asking for a line-by-line explanation is not likely to result in one.

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Quote:
Anyone, please explain this program line by line...

Easy, this program is non sense, it do not even compile.

You don't understand your code.
There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900