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 -
#include<stdio.h>
-
#include<cstdlib>
-
#include<cstring>
-
#include<fstream>
-
#include<iostream.h>
-
#define member 1 // use to indicate current node
-
#define nonmember 0
-
#define infinity 999 // intial distance
-
struct arc
-
{
-
int wt;
-
};
-
struct arc g[10][10];
-
int path[10];
-
//end of global defintn
-
void jointwt(struct arc g[][10], int n1, int n2 ,int wt)
-
// function to assign the weight on arcs
-
{
-
g[n1][n2].wt = wt; // assign the weight to arc n1 -n2
-
}
-
// end of function joint
-
cost(struct arc c[][10], int n)
-
// function to get the weights
-
{
-
int i,j,wt;
-
for(i=0;i<n;i++)
-
for(j=0;j<n;j++)
-
{
-
printf("\n weight of arc %d %d", i,j);
-
scanf("%d",&wt);
-
jointwt(c,i,j,wt);
-
}
-
printf("\n");
-
}
-
// end of function
-
mat(struct arc a[][10], int n)
-
// function to print the weight in matrx
-
{
-
int i,j;
-
printf("\n\t");
-
for(i=0;i<n;i++)
-
{
-
for(j=0;j<n;j++)
-
{
-
printf("%d",a[i][j].wt);
-
}
-
printf("\n\t");
-
}
-
} //end of mat
-
shortpath(int wt, int s, int t, int proced, int n)
-
struct arc wt[][10];
-
int s,t,proced[10];
-
int n;
-
{
-
int dist[10], perm[10];
-
int curr, i,k,dc,h=0;
-
int smalldist, newdist;
-
for(i=0;i<n;i++)
-
{
-
perm[i] = nonmember;
-
dist[i] =infinity;
-
}
-
perm[s] = member;
-
dist[s] = 0;
-
curr = s;
-
while(curr != t)
-
{
-
smalldist = infinity;
-
dc = dist[curr];
-
for(i=0;i<n;i++)
-
if(perm[i] == nonmember)
-
{
-
newdist = dc + wt[curr][i].wt;
-
if(newdist<smalldist)
-
{
-
dist[i] = newdist;
-
proced[i] = curr;
-
}
-
if(dist[i]<smalldist)
-
{
-
smalldist = dist[i];
-
k=i;
-
}
-
}
-
curr = k;
-
perm[curr] = member;
-
path[h++] = curr;
-
}
-
printf("\n\t path is = %d", s+1);
-
for(i=0;i<h;i++)
-
printf("-> %d",path[i]+1);
-
return(dist[t]);
-
} // end of short path
-
/*int cost(struct arc , int );
-
int mat(struct arc , int );
-
void jointwt(struct arc , int , int ,int );
-
int shortpath(int , int , int , int , int );*/
-
int main()
-
{
-
struct arc c[10][10];
-
int n,s,t,proced[10];
-
int x;
-
printf("\n\t\t How many nodes :");
-
scanf("%d",&n);
-
printf("\n\t\t Enter the adjacency matrix of weight");
-
printf("\n\t\t Enter 999 if no edge exits :");
-
cost(c,n);
-
printf("\n\t\t Weight matrix :");
-
mat(c,n);
-
printf("\n\t\t Enter source and distnation :");
-
scanf("%d %d",&s, &t);
-
printf("\n\t\t shortest path from %d to %d is:\n",s,t);
-
x = shortpath(c, s - 1, t - 1, proced, n);
-
} //efmain
7 3118
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.
Usually shortest distance will be computed with help of Graphs.
You should start from there.
raghu
Banfa 9,065
Expert Mod 8TB
Perhaps you could list the specific compiler errors you do not understand.
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.)
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.)
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.
Banfa 9,065
Expert Mod 8TB
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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...
|
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...
| |