Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-2008, 10:13 AM
simpatico simpatico is offline
Newbie
 
Join Date: May 2007
Posts: 8
Rep Power: 0
simpatico is on a distinguished road
Default help me with this code pllz

hi i need help for this code
every time i try to excute the program there be some mistake
and i dont know wats the wrong

can any one help me plz

Quote:
#include <stdio.h>

/*
The input file (weight.txt) look something like this
4
0 0 0 21
0 0 8 17
0 8 0 16
21 17 16 0

The first line contains n, the number of nodes.
Next is an nxn matrix containg the distances between the nodes
NOTE: The distance between a node and itself should be 0
*/

int n; /* The number of nodes in the graph */

int weight[100][100]; /* weight[i][j] is the distance between node i and node j;
if there is no path between i and j, weight[i][j] should
be 0 */

char inTree[100]; /* inTree[i] is 1 if the node i is already in the minimum
spanning tree; 0 otherwise*/

int d[100]; /* d[i] is the distance between node i and the minimum spanning
tree; this is initially infinity (100000); if i is already in
the tree, then d[i] is undefined;
this is just a temporary variable. It's not necessary but speeds
up execution considerably (by a factor of n) */

int whoTo[100]; /* whoTo[i] holds the index of the node i would have to be
linked to in order to get a distance of d[i] */

/* updateDistances(int target)
should be called immediately after target is added to the tree;
updates d so that the values are correct (goes through target's
neighbours making sure that the distances between them and the tree
are indeed minimum)
*/
void updateDistances(int target) {
int i;
for (i = 0; i < n; ++i)
if ((weight[target][i] != 0) && (d[i] > weight[target][i])) {
d[i] = weight[target][i];
whoTo[i] = target;
}
}

int main(int argc, char *argv[]) {
FILE *f = fopen("dist.txt", "r");
fscanf(f, "%d", &n);
int i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
fscanf(f, "%d", &weight[i][j]);
fclose(f);

/* Initialise d with infinity */
for (i = 0; i < n; ++i)
d[i] = 100000;

/* Mark all nodes as NOT beeing in the minimum spanning tree */
for (i = 0; i < n; ++i)
inTree[i] = 0;

/* Add the first node to the tree */
printf("Adding node %c\n", 0 + 'A');
inTree[0] = 1;
updateDistances(0);

int total = 0;
int treeSize;
for (treeSize = 1; treeSize < n; ++treeSize) {
/* Find the node with the smallest distance to the tree */
int min = -1;
for (i = 0; i < n; ++i)
if (!inTree[i])
if ((min == -1) || (d[min] > d[i]))
min = i;

/* And add it */
printf("Adding edge %c-%c\n", whoTo[min] + 'A', min + 'A');
inTree[min] = 1;
total += d[min];

updateDistances(min);
}

printf("Total distance: %d\n", total);

return 0;
}

Last edited by simpatico; 03-02-2008 at 03:33 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-10-2008, 11:42 PM
Babbage Babbage is offline
Newbie
 
Join Date: Mar 2008
Posts: 10
Rep Power: 0
Babbage is on a distinguished road
Default Re: help me with this code pllz

Hi simpatico,

Your code seemed to work for me. Here is the output I received:

Adding node A
Adding edge A-D
Adding edge D-C
Adding edge C-B
Total distance: 45

A couple of things to point on, however. At the beginning of your program (in the comments), you said that the text file's name was 'weight.txt'. However, in your code you are opening the file 'dist.txt' for reading. This threw me off a little bit, and if you haven't worked on your code in a while, this might throw you off as well.

Also, if your program cannot find the file you are looking for and try to input information from that (like, say, by using fscanf), this could cause your program to crash. I know that when I tried to run the program with the text file named 'weight.txt' (ok, I didn't read the code close enough the first time round), I got an assert failure on line 52. If you have been getting an assert failure, it's because your program is not finding the file you are trying to open.

You might want to try and check first to see if the file is open before reading from it. Something like the following should do:

Code:
int main(int argc, char *argv[]) {
  FILE *f = fopen("dist.txt", "r");
  if(f == NULL){
    printf("ERROR: Cannot find specified file.\n");
    return -1;
  }
-Babbage
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-11-2008, 02:57 AM
lovefan lovefan is offline
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0
lovefan is on a distinguished road
Default Re: help me with this code pllz

good job
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Calculator AfTriX VB Tutorials 3 02-29-2008 09:53 AM
Generating executable machine code steffanp General Programming 5 02-03-2008 11:01 AM
RAW image code / data mpcode General Programming 1 04-30-2007 12:01 PM
Where to Put PHP Code clookid PHP Tutorials 1 01-11-2007 09:44 PM


All times are GMT -5. The time now is 11:42 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads