samedi 25 avril 2015

matrix represents tree the function returns 0

I have some homwrok and it looks like this: Question 1 ( First program adjacency.c file ) Directed tree structure T has N nodes represented by the adjacency matrix A size NxN as follows:

A [ u ] [ v] == TRUE if and only if there is a directed arc from u to v in T , or in other words : u is the parent of v . In the following example a tree with N = 11 nodes :

tree

We obtain the following neighboring matrix :

matrix

this are the questions:

A. You must define with #define command and / or enum the N and permanent TRUE and FALSE. Typedef should be set with a character named adj_mat defines the neighboring matrix size N.

B. You must write a function called path, which accepts as a parameter adjacency matrix A and indexes of two nodes u and v and returns TRUE if and only if there is a directed path (by directional arrow) at the intersection u v, the tree is represented by a matrix A. Otherwise it returns FALSE.

For example: path (1,8) will return TRUE. The same path (1,3). On the other hand path (3,8) will FALSE.

C. First you must write a function (main) defines a variable type adj_mat, asks the user entries for this matrix, and indexes of the two nodes. The main function function call path, to see if there is a directed path between two nodes in the data. The function to print the test result output.

have to get some help guys

#include <stdio.h>

#define N 11
enum {FALSE, TRUE};
typedef int adj_mat[N][N];

int path2(adj_mat A, int u, int v, int temp)
{
if(u == temp && A[u][v] == FALSE)
return TRUE;

if(u == temp && A[u][v] == FALSE)
return FALSE;

if(A[u][v] == FALSE)
return path2(A, u-1, v, temp);

if(A[u][v] == TRUE)
return path2(A, N, u, temp);

return FALSE;
}

int path(adj_mat A, int u, int v)
{
return path2(A, N, v, u);
}



int main()
{

int arr[N][N]= {{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}};
int u;
int v;
printf("please enter two numbers \n");
scanf("%d %d", &u, &v);
printf("The answer is %d", path(arr, u, v),".");
return 0;
}

The problem is at the terminal when i put 1,8 it returns 0. please help me guys im going inasane

Aucun commentaire:

Enregistrer un commentaire