Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 07-29-2008, 04:49 AM   #1 (permalink)
Registered User
 
Join Date: Jul 2008
Posts: 2
OS: ubuntu


Dijkstra

this is the code i ve written for dijkstra in c for linux(ubuntu).. there is some error in it can u help me rectify it.. thanks

#include <stdio.h>

void main()
{
int i,j,k,graph[5][5],dist[5][5],visited[5][5],prev[5][5],q[5],least,least_node,sum,alt; // vist=visited or no q=set of al nodes

for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
if(i==j) {
graph[i][j]=-1;
}
else {
printf("Enter value from %d to %d:",i,j);
scanf("%d",&graph[i][j]);
}

dist[i][j]=-1;
visited[i][j]=0;
}
}


for (i=0;i<5;i++) {
least=9999;
sum=0;

// Finding least node
for(j=0;j<5;j++) {
if((graph[i][j]!=-1)&&(least>graph[i][j] )) {
least=graph[i][j];
least_node=j;
}
}


sum+=least;
dist[i][least_node]=least;
visited[i][least_node]=1;
visited[least_node][i]=1;

for(k=0;k<5;k++) {
//while((graph[least_node][k]!=-1)&&(visited[least_node][k]!=1)){
if((graph[least_node][k]!=-1)&&(visited[least_node][k]!=1)) {
alt=sum+graph[least_node][k];
if((graph[i][k]>alt)||(graph[i][k]==-1)) {
dist[i][k]=alt;
prev[i][k]=least_node;

}
}
else {
dist[i][k]=graph[i][k];
prev[i][k]=i;
}
}

}


for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%d\n",dist[i][j]);
}
}
}
chintanmehta is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 07-29-2008, 04:49 AM   #2 (permalink)
Registered User
 
Join Date: Jul 2008
Posts: 2
OS: ubuntu


Re: Dijkstra

pls give me the rectified code and do mention my mistake too.. thanks
chintanmehta is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 07-30-2008, 10:13 PM   #3 (permalink)
Registered User
 
Join Date: Oct 2007
Location: Littleton, Colorado USA
Posts: 470
OS: xp 64 sp2 Fedora Core 8 (vmware xp core 8 x32) Minix


Re: Dijkstra

Compiled fine and ran OK. Have no idea what you are doing! But it didn't core dump. Are you using "vi" as your editor? If not use it and learn the "%" command. Lay the cursor on a brace, curley, square, or parenthesis and then enter the %. VI will show you the other closing brace. VI did get confused on the "//" comment line on line 65, because it had unclosed braces. I just deleted the line to make it work.

Learn the VI editor and the cntl-Z command to stop the edit session. Use "fg" to bring the edit session back to the foreground. Don't forget to save the C-file before you cntl-Z. The "jobs" command will show the stop programs. This method works very well for code writing and your hands don't have to leave the keyboard.

Sorry I don't do homework. The code seemed error free except for "void main()" which is not C90 or C99 (I think). The main should return an integer status value. The gcc compiler flagged that as warning.
lensman3 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 07-31-2008, 07:36 AM   #4 (permalink)
Manager
 
shuuhen's Avatar
 
Join Date: Sep 2004
Location: Colorado
Posts: 987
OS: Mac OS 9.1, Mac OS X 10.5.8, WinXP Pro, FreeBSD 6.0, Gentoo Linux

My System

Re: Dijkstra

What exactly is not working? Give us specific error messages, etc.

I recommend changing void main() to int main( void ), then you should have return 0; at the bottom of your main function. Please indent your code and post with code tags around it.
__________________


Has it been a few days since I replied to your thread? Don't panic! I'm a busy college student and may forget a post if I'm extra busy (or it might just take me a while to be able to do a decent reply). If you still need help and are awaiting my reply after a few days, PM me about it.

When posting what errors you get, please give the full message. It makes helping you much easier.
shuuhen is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 12:26 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85