473,770 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find short paths

10 New Member
i am a beginner, guys plizzzzzzzz help me. i am given a assignment to find short paths either using dijkstra algorithm or using binary search algorithm in linux OS.
check out what is wrong the code i tried
/program to find shortest path
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<fstream>
  5. #include<iostream.h>
  6. #define member 1 // use to indicate current node
  7. #define nonmember 0
  8. #define infinity 999 // intial distance
  9. struct arc
  10. {
  11.     int wt;
  12. };
  13. struct arc g[10][10];
  14. int path[10]; 
  15. //end of global defintn
  16. void jointwt(struct arc g[][10], int n1, int n2 ,int wt)
  17. // function to assign the weight on arcs
  18. {
  19.     g[n1][n2].wt = wt; // assign the weight to arc  n1 -n2
  20. }
  21. // end of function joint
  22.  cost(struct arc c[][10], int n)
  23. // function to get the weights
  24. {
  25.     int i,j,wt;
  26.     for(i=0;i<n;i++)
  27.     for(j=0;j<n;j++)
  28.     {
  29.         printf("\n weight of arc %d %d", i,j);
  30.         scanf("%d",&wt);
  31.         jointwt(c,i,j,wt);
  32.     }
  33.     printf("\n");
  34. }
  35. // end of function
  36.  mat(struct arc a[][10], int n)
  37. // function to print the weight in matrx
  38. {
  39.     int i,j;
  40.     printf("\n\t");
  41.     for(i=0;i<n;i++)
  42.     {
  43.          for(j=0;j<n;j++)
  44.          {
  45.               printf("%d",a[i][j].wt);
  46.          }
  47.          printf("\n\t");
  48.     }
  49. } //end of mat
  50.  shortpath(int wt, int s, int t, int proced, int n)
  51. struct arc wt[][10];
  52. int s,t,proced[10];
  53. int n;
  54. {
  55.     int dist[10], perm[10];
  56.     int curr, i,k,dc,h=0;
  57.     int smalldist, newdist;
  58.     for(i=0;i<n;i++)
  59.     {
  60.         perm[i] = nonmember;
  61.         dist[i] =infinity;
  62.     }
  63.          perm[s] = member;
  64.                 dist[s] = 0;
  65.         curr = s;
  66.     while(curr != t)
  67.     {
  68.         smalldist = infinity;
  69.         dc = dist[curr];
  70.          for(i=0;i<n;i++)
  71.             if(perm[i] == nonmember)
  72.             {
  73.                 newdist = dc + wt[curr][i].wt;
  74.                 if(newdist<smalldist)
  75.                 {
  76.                     dist[i] = newdist;
  77.                     proced[i] = curr;
  78.                 }
  79.                 if(dist[i]<smalldist)
  80.                 {
  81.                     smalldist = dist[i];
  82.                     k=i;
  83.                 }
  84.             }
  85.          curr = k;
  86.         perm[curr] = member;
  87.         path[h++] = curr;
  88.     }
  89.     printf("\n\t path is = %d", s+1);
  90.      for(i=0;i<h;i++)
  91.     printf("-> %d",path[i]+1);
  92.     return(dist[t]);
  93. } // end of short path
  94. /*int cost(struct arc , int );
  95. int mat(struct arc , int );
  96. void jointwt(struct arc , int , int  ,int );
  97. int shortpath(int , int , int , int , int );*/
  98. int main()
  99. {
  100.     struct arc c[10][10];
  101.     int n,s,t,proced[10];
  102.     int x;
  103.      printf("\n\t\t How many nodes :");
  104.     scanf("%d",&n);
  105.     printf("\n\t\t Enter the adjacency matrix of weight");
  106.     printf("\n\t\t Enter 999 if no edge exits :");
  107.     cost(c,n);
  108.     printf("\n\t\t Weight matrix :");
  109.     mat(c,n);
  110.     printf("\n\t\t Enter source and distnation  :");
  111.     scanf("%d %d",&s, &t);
  112.     printf("\n\t\t shortest path from %d to  %d is:\n",s,t);
  113.     x = shortpath(c, s - 1, t - 1, proced, n);
  114. } //efmain
Oct 29 '08 #1
7 3144
r035198x
13,262 MVP
Does the code compile and run correctly then? Better tell us a specific problem rather than ask us to look for errors in your code.
Oct 29 '08 #2
gpraghuram
1,275 Recognized Expert Top Contributor
Usually shortest distance will be computed with help of Graphs.
You should start from there.

raghu
Oct 29 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
Perhaps you could list the specific compiler errors you do not understand.
Oct 29 '08 #4
latalui
10 New Member
Does the code compile and run correctly then? Better tell us a specific problem rather than ask us to look for errors in your code.
errors in codes are
ISO C++ forbids declaration of `cost' with no type
shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
shortestpath.c: 51: error: expected `,' or `;' before "struct"
shortestpath.c: 54: error: expected unqualified-id before '{' token
shortestpath.c: 54: error: expected `,' or `;' before '{' token
shortestpath.c: In function `int main()':
shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)
Nov 6 '08 #5
latalui
10 New Member
Perhaps you could list the specific compiler errors you do not understand.
errors are:-
forbids declaration of `cost' with no type
shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
shortestpath.c: 51: error: expected `,' or `;' before "struct"
shortestpath.c: 54: error: expected unqualified-id before '{' token
shortestpath.c: 54: error: expected `,' or `;' before '{' token
shortestpath.c: In function `int main()':
shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)
Nov 6 '08 #6
arnaudk
424 Contributor
Usually shortest distance will be computed with help of Graphs.
You should start from there.

raghu
That depends what is meant by "shortest distance". If the OP means "the least distance connecting N points with straight lines" then you use graph theory (i.e. calculate the minimum spanning tree or work out the Steiner tree problem if you can add intermediate points). If it's just the shortest distance between two points along some cost function then you don't need graph theory, the problem can be solved numerically using Dijkstra's algorithm.
Nov 6 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
errors are:-
forbids declaration of `cost' with no type
shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type
shortestpath.c: 51: error: expected constructor, destructor, or type conversion before "struct"
shortestpath.c: 51: error: expected `,' or `;' before "struct"
shortestpath.c: 54: error: expected unqualified-id before '{' token
shortestpath.c: 54: error: expected `,' or `;' before '{' token
shortestpath.c: In function `int main()':
shortestpath.c: 113: error: `shortpath' undeclared (first use this function)
shortestpath.c: 113: error: (Each undeclared identifier is reported only once for each function it appears in.)
Now take each error in turn and examine the line of code given and the line of code before that error. For instance

shortestpath.c: 38: error: ISO C++ forbids declaration of `mat' with no type

Line 38 is the definition of you function mat, do you see anything wrong with how you have defined mat?
Nov 6 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7005
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it for Python. I'd be using it in Linux to regularly search for files with a script. Is this a good application for Python or does Perl have more functionality for quick & simple scripting in Linux? Thanks.
0
2704
by: Ethel Aardvark | last post by:
I am running a 9.0.1 database on a W2K server and have come across some strange behaviour with a SQL query. I have a query which runs in a PL/SQL cursor which has several PL/SQL variables used to switch on and off certain rules. One idea I had was to have two queries UNIONed together with a simple switch selecting which half was to operate (I know it sounds like there are probably better ways of doing this but I have my reasons). To cut...
11
17021
by: PÃ¥l Eilertsen | last post by:
Hi, I have recently installed Visual Studio .Net 2003 and am trying to compile and run a simple windows form app (used the VS wizard). When trying to run I get an error message telling me: "fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory". I have browsed to C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include and have found the file there. What can be wrong? I have also tried to uninstall...
0
1074
by: citizenkahn | last post by:
I am trying to parse a build log for errors. I figure I can do this one of three ways: - find the absolute platonic form of an error and search for that item - create definitions of what patterns describe errors for each tool which is used (ant, MSDEV, etc). - rework the build such that all the return codes for all 3rd party are captured and logged using my own error description The return code method has a high level of effort...
4
1746
by: Ben | last post by:
Hi We have a list of file paths and short file names in our database. We need to loop through them all, picking up the long file name from the file itself and check that the long file name does not include a "~" character. I have the short file name and path in a variable (eg "c:\data\123442~1.doc") how can I get the long file name from this data.
34
2986
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about the executable ? Is it possible ? Also, how does the compiler add inling to the program ? I know that whenever it sees"inline" in front of the procedure name, it inlines it. But if we give the -finline options, it inline all the procedures ? How
14
7169
by: CapCity | last post by:
If I have a strig that represents a path, how can I programmatically get the "short" DOS name for it? For example, if I have "C:\Program Files\" I want "C:\Progra~1\". Thanks
3
4417
by: Alan Cohen | last post by:
This seems like a really, really dumb question, but I can't seem to find the simple answer that it seems to deserve. My C#/ASP.net 2.0 app needs to email a URL link back to the site from a web page. Sending the email is no problem, but generating this URL is giving me an annoying issue in testing because of the difference in URLs between the VS web server and IIS. Specifically, the URL that I need to send when testing locally (VS...
3
2328
by: Alexander Vasilevsky | last post by:
Here, there is a challenge: to find the files must be on a local computer, have the same names. Get drives and folders to go through without problems. But what and how to store files while a search? http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas gift
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.