简体   繁体   中英

adjacency matrix find if neighbores

I have some homewrok that 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 this example a tree with N = 11 nodes.

We obtain the following neighboring matrix .

The questions are:

  1. 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.

  2. 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 uv, 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.

  1. 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 doesnt do nothing.

You have a number of issues where you attempt to read beyond the end of arr resulting in undefined behavior . First when you declare an array with N = 11 elements, your array indexes are arr[0] - arr[10] . Consider the following in path :

     return path2 (A, N, v, u);

You pass A, N, v, u as arguments to path2 :

int path2 (adj_mat A, int u, int v, int temp)

Then in path2 you attempt A[u][v] wrong , that is A[11][v] beyond the end of your array. ( undefined behavior ). Look at the order of the arguments. A is passed as A to path2 , N is passed as u , v as v , and u as temp , making u = 11 .

Next, both of the following cannot be correct:

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

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

What did you intend? Also, what is this?

    printf("The answer is %d", path(arr, u, v),".");

That will not even compile (see below). When you compile, make sure you enable warnings, at minimum -Wall -Wextra which will help point out problems in your code.

Finally, please give the user some guidance on the range of permissible numbers, and check the scanf return :

    printf ("\n please enter two numbers (0-10): ");
    if (scanf (" %d %d", &u, &v) == 2)
        printf ("\n  The answer is %d.\n", path (arr, u, v));
    else
        fprintf (stderr, "error: input failure.\n");

I make no comment on whether your logic is correct, but you should address the foregoing before going further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM