A Debug/Trace Tool 
by Rainer Storn

Listing One

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define INF 1000      /*Infinity must be higher than the largest path  value */
#define N     10       /*Maximum number of nodes in the network */
#define TRUE   1
#define FALSE  0
#define PERM   1       /*Permanently labeled */
#define TEMP   0       /*Temporarily labeled */

/*--------Function declarations------------------------------------*/

int dijkstra(int s, int t, int w[][N], int final[], int dist[], int pred[]);
extern void dbg_init(void);
extern void dbg(char *, int, char *, ...);

/*--------Function definitions-------------------------------------*/
int dijkstra(int s, int t, int w[][N], int final[], int dist[], int pred[])
{
  int u, v, y, recent, newlabel, min, path;

/*--------Initialization---------------------------*/
dbg_init();
  for (v=0; v<N; v++)
  {
    dist[v]  = INF;   /*Set all distances to infinity */
    final[v] = TEMP;  /*Set all nodes to "temporarily labeled" */
    pred[v]  = -1;    /*Set nodes in shortest path trace to "non existent" */
  }
  dist[s]  = 0;       /*Distance from s to s is 0 */
  final[s] = PERM;    /*Set node s to "permanently labeled" */
  path     = TRUE;    /*Assume there is a path from s to t */
  recent   = s;       /*Most recent node is s */


/*--------Run Dijkstra's Algorithm------------------*/
  for(u=0;u<6;u++)
  {
     for(v=0;v<6;v++)
       dbg("1_init1",2,"w[%d][%d] = %d   ",u,v,w[u][v]);
  }
  dbg("1_init2",1,"&u = %p\n",&u);
  while(final[t]==TEMP)   /*while destination node t is temporarily labeled */
  {
     for(v=0; v<N; v++)
     { 
    if ((w[recent][v] < INF) && (final[v]==TEMP)) /*node temporarily labeled */
        {
           newlabel = dist[recent] + w[recent][v];
           if (newlabel < dist[v]) /*new distance, smaller than previous one */
           {
          dist[v] = newlabel;   /*update distance */
          pred[v] = recent;     /*put predecessor in shortest path */
          dbg("2_dist",4,"recent = %d   newlabel = %d \n",recent,newlabel);
           }
        }
     }
     min = INF;           /*reset temporary minimum */
     for(u=0; u<N; u++)  /*Find smallest labeled node */
     {
    dbg("3_u",1,"u=%d\n",u);
        if ((final[u]==TEMP) && (dist[u] < min))  /*node temporarily labeled */
        {
           y   = u;       /*Save node in y */
           min = dist[u]; /*Reduce temporary minimum */
        } 
     }
     if (min < INF)       /*if there is a path */
     {
        final[y] = PERM;
        recent   = y;
     }
     else                 /*if there is no path */
     {
        path     = FALSE;
        final[t] = PERM;  /*induce break of while loop */
     }
  }
  return(path);
}


Example 1:

(a)

void     dbg(char *stop, int level, char *format, ...);


(b)

    mean = 0;
    for  (j = min; j < max; j++)    
    {                               
        mean = mean + array[j];

       dbg("7_stp", 2, "\n mean = %f", mean);
    }
    mean = mean / (max - min + 1);

Example 2:

 for(u=0;u<6;u++)
  {
     for(v=0;v<6;v++)
       dbg("1_init1",2,"w[%d][%d] = %d   ",u,v,w[u][v]);
  }


Example 3:

(a)

#include <stdarg.h>
extern void dbg_init(void);
extern void dbg(char *, int, char *, ...);


(b)

void main(void)
{
    int     i, j;
    float   x, y , z;
    dbg_init();
    ...
}


