c - array type has incomplete element type adjacency -
i got homework , need write program wich given matrix represents adjacency. user need put matrix in , index , program returns true if there path between 2 values of indexes. example
0 1 2 3 4 1 0 1 0 0 2 0 0 1 0 3 0 0 0 0 4 0 0 0 0
if user provides matrix (without indexes numbers) , gives value 3,1 returns true because 3 son of 2 , 2 son of one. if user gives value 4,1 returns false.
#include <stdio.h> #define n 11 #define true 1 #define false 0 int path(long int [][20] a, int u, int v) { if(u == 0 && a[u][v] == true) return true; if(u == 0 && a[u][v] == false) return false; if(a[u][v] == false) return path(a, u--, v); else if(a[u][v] == true) return path(a, n, u); } int main() { int arr[11][11] = {{0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,1,0,0}, {0,0,0,0,0,0,0,0,0,1,0}, {0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0}}; return path(arr,1,8); return 0; }
when try run on gcc returns:
adjacency.c:8:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘a’ adjacency.c: in function ‘main’: adjacency.c:30:1: warning: implicit declaration of function ‘path’ [-wimplicit-function-declaration]
can enybody please tell me why? , if program works?? guys
the function prototypes doesn't match array size.
try changing
int path(long int [][20] a, int u, int v)
into
int path(long int a[][11], int u, int v) // move
and in main change
int arr[11][11]
to
long int arr[11][11]
or drop long in function
int path(int a[][11], int u, int v)
btw:
return path(a, n, u);
is bad n=11. index outside array.
maybe want
return path(a, n-1, u);
instead avoid indexing array out of range.
important:
return path(a, u--, v);
probably needs be:
return path(a, --u, v);
because u-- post-decrement. you'll have endless loop calling path(..) again , again same values.
you want decrement before function call. use --u pre-decrement.
Comments
Post a Comment