YOU
YOU

Reputation: 123841

error: variable or field 'dtra' declared void

I am trying to compile following dijkstra algorithm, which is written in C.

but the following does not compile on windows with gcc.

void dtra(dd)
float dd[MAX][MAX];
{
    init();
    nstep=1;
    is=istart;
    output();
    while(nm!=0) {
        nstep++;
        cal(dd);
        output();
    }
    route();
}

and I'm getting following error on above block.

error: variable or field 'dtra' declared void

And I've never seen such a function declaration, so I have no idea how to fix it.

void dtra(dd)
float dd[MAX][MAX];

Could anybody lighten me up?

Edit:

Here is full code, copy pasted

/***** 14.1  Dijkstra Method ***/
/***** Programmed by M.Hisai and Y.Matano 1995 *****/
#include <stdio.h>
#include<stdlib.h>
#define MAX 20
void dtra();
void init();
void cal();
void output();
void route();
int i,j,n,istart,nm,is,jp,nstep,m[MAX],p[MAX],path[MAX][MAX];
float vmin,d[MAX][MAX],v[MAX],sd[MAX][MAX];
FILE *fp;

main()
{
    if((fp=fopen("dijkstra.dat","r"))==NULL) {
        printf("データファイルがありません。  \n");
        exit(1);
    }
    fp=fopen("dijkstra.dat","r");
    fscanf(fp,"%d",&n);
    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            fscanf(fp,"%f",&d[i][j]);
            if(d[i][j]==0.0)
                d[i][j]=9000.0;
        }
    }
    fscanf(fp,"%d",&istart);
    fclose(fp);
    fp=fopen("dijkstra_output.dat","w");
    dtra(d);
    fclose(fp);
    printf("dijkstra_output.txtにも出力しました\n");
}

void dtra(dd)
float dd[MAX][MAX];
{
    init();
    nstep=1;
    is=istart;
    output();
    while(nm!=0) {
        nstep++;
        cal(dd);
        output();
    }
    route();
}

void init()
{
    for(j=1;j<=n;j++) {
        v[j]=9000;
        p[j]=0;
    }
    v[istart]=0;
    nm=n-1;
    for(i=1;i<=nm;i++) {
        if(i<istart) m[i]=i;
        else        m[i]=i+1;
    }
}

void cal(dd)
float dd[MAX][MAX];
{
    for(i=1;i<=nm;i++) {
        j=m[i];
        if(dd[is][j]<9000 && v[is]+dd[is][j]<v[j]) {
            v[j]=v[is]+dd[is][j];
       p[j]=is;
        }
    }
    vmin=100000.0;
    for(i=1;i<=nm;i++) {
        j=m[i];
        if(v[j]<vmin) {
            vmin=v[j];
            is=j;
        }
    }
    nm+=-1;
    for(i=1;i<=nm;i++) {
        if(m[i]>=is) m[i]=m[i+1];
    }
}

void output()
{
    printf("STEP%2d \n",nstep);                 fprintf(fp,"STEP%2d \n",nstep);
    printf("   j       Vj   Pj  \n");           fprintf(fp,"   j       Vj   Pj  \n");
    for(j=1;j<=n;j++) {
        printf("%3d %9.1f %3d \n",j,v[j],p[j]); fprintf(fp,"%3d %9.1f %3d \n",j,v[j],p[j]);
    }
    printf("M=(");                              fprintf(fp,"M=(");
    for(i=1;i<=nm;i++) {
        printf("%2d ",m[i]);                    fprintf(fp,"%2d ",m[i]);
    }
    printf(") \n\n");                           fprintf(fp,") \n\n");
}

void route()
{
    printf("OPTIMAL SOLUTION \n");              fprintf(fp,"OPTIMAL SOLUTION \n");
    printf("START NODE=%2d \n",istart);         fprintf(fp,"START NODE=%2d \n",istart);
    for(j=1;j<=n;j++) {
        path[istart][j]=p[j];
        sd[istart][j]=v[j];
        if(j!=istart) {
            printf("SHORTEST ROUTE:");          fprintf(fp,"SHORTEST ROUTE:");
            jp=j;
            while(jp!=istart) {
                printf("%2d<--",jp);            fprintf(fp,"%2d<--",jp);
                jp=p[jp];
            }
            printf("%2d        ",jp);           fprintf(fp,"%2d        ",jp);
            printf("DISTANCE=%3.0f  \n",v[j]);  fprintf(fp,"DISTANCE=%3.0f  \n",v[j]);
        }
    }
}

Edit:

Thank you all, I've got it work.

/***** 14.1  Dijkstra Method ***/
/***** Programmed by M.Hisai and Y.Matano 1995 *****/
#include <stdio.h>
#include<stdlib.h>
#define MAX 20

int i,j,n,istart,nm,is,jp,nstep,m[MAX],p[MAX],path[MAX][MAX];
float vmin,d[MAX][MAX],v[MAX],sd[MAX][MAX];
FILE *fp;

void init(){
    for(j=1;j<=n;j++) {
        v[j]=9000;
        p[j]=0;
    }
    v[istart]=0;
    nm=n-1;
    for(i=1;i<=nm;i++) {
        if(i<istart) m[i]=i;
        else        m[i]=i+1;
    }
}

void cal(float dd[MAX][MAX]){
    for(i=1;i<=nm;i++) {
        j=m[i];
        if(dd[is][j]<9000 && v[is]+dd[is][j]<v[j]) {
            v[j]=v[is]+dd[is][j];
       p[j]=is;
        }
    }
    vmin=100000.0;
    for(i=1;i<=nm;i++) {
        j=m[i];
        if(v[j]<vmin) {
            vmin=v[j];
            is=j;
        }
    }
    nm+=-1;
    for(i=1;i<=nm;i++) {
        if(m[i]>=is) m[i]=m[i+1];
    }
}

void output(){
    printf("STEP%2d \n",nstep);                 fprintf(fp,"STEP%2d \n",nstep);
    printf("   j       Vj   Pj  \n");           fprintf(fp,"   j       Vj   Pj  \n");
    for(j=1;j<=n;j++) {
        printf("%3d %9.1f %3d \n",j,v[j],p[j]); fprintf(fp,"%3d %9.1f %3d \n",j,v[j],p[j]);
    }
    printf("M=(");                              fprintf(fp,"M=(");
    for(i=1;i<=nm;i++) {
        printf("%2d ",m[i]);                    fprintf(fp,"%2d ",m[i]);
    }
    printf(") \n\n");                           fprintf(fp,") \n\n");
}

void route(){
    printf("OPTIMAL SOLUTION \n");              fprintf(fp,"OPTIMAL SOLUTION \n");
    printf("START NODE=%2d \n",istart);         fprintf(fp,"START NODE=%2d \n",istart);
    for(j=1;j<=n;j++) {
        path[istart][j]=p[j];
        sd[istart][j]=v[j];
        if(j!=istart) {
            printf("SHORTEST ROUTE:");          fprintf(fp,"SHORTEST ROUTE:");
            jp=j;
            while(jp!=istart) {
                printf("%2d<--",jp);            fprintf(fp,"%2d<--",jp);
                jp=p[jp];
            }
            printf("%2d        ",jp);           fprintf(fp,"%2d        ",jp);
            printf("DISTANCE=%3.0f  \n",v[j]);  fprintf(fp,"DISTANCE=%3.0f  \n",v[j]);
        }
    }
}

void dtra(float dd[MAX][MAX]){
    init();
    nstep=1;
    is=istart;
    output();
    while(nm!=0) {
        nstep++;
        cal(dd);
        output();
    }
    route();
}

main()
{
    if((fp=fopen("dijkstra.dat","r"))==NULL) {
        printf("データファイルがありません。  \n");
        exit(1);
    }
    fp=fopen("dijkstra.dat","r");
    fscanf(fp,"%d",&n);
    for(i=1;i<=n;i++) {
        for(j=1;j<=n;j++) {
            fscanf(fp,"%f",&d[i][j]);
            if(d[i][j]==0.0)
                d[i][j]=9000.0;
        }
    }
    fscanf(fp,"%d",&istart);
    fclose(fp);
    fp=fopen("dijkstra_output.dat","w");
    dtra(d);
    fclose(fp);
    printf("dijkstra_output.txtにも出力しました\n");
}

dijkstra.dat

6
  0.0  30.0  15.0  55.0    0.0    0.0
10.0    0.0  15.0  20.0    0.0    0.0
20.0  10.0    0.0  35.0  65.0    0.0
  0.0  25.0    0.0   0.0   30.0  25.0
  0.0    0.0  55.0  25.0    0.0  10.0
  0.0    0.0    0.0  20.0  15.0    0.0
1

Upvotes: 1

Views: 3517

Answers (3)

eq-
eq-

Reputation: 10096

void dtra(dd)
float dd[MAX][MAX];

to

void dtra(float dd[MAX][MAX])

The original is an old-style K&R function definition.

edit: (on the full code)

You need to update the definition for cal too. Plus, you need to correct the declarations so that they match the definitions (just copy & paste).

Like so:

void dtra(float dd[MAX][MAX]);
/* later */
void dtra(float dd[MAX][MAX])
{ /* func definiton */ }

The code contains multiple oddities (like opening a file for reading twice - but only checking the success of the first attempt), and is not standard-conformant. Not a good source to learn C from!

Upvotes: 3

Prasoon Saurav
Prasoon Saurav

Reputation: 92864

That's old K & R style of declaring functions.

Upvotes: 0

codaddict
codaddict

Reputation: 455132

It's old K&R style syntax for defining a function.

Change it to:

void dtra(float dd[MAX][MAX]) {
....
}

Upvotes: 1

Related Questions