c - adjacency matrix find if neighbores -


i have homewrok looks this:

question 1 ( first program adjacency.c file )

directed tree structure t has n nodes represented adjacency matrix size nxn follows:

a [ u ] [ v] == true if , if there directed arc u v in t , or in other words : u parent of v. in example tree n = 11 nodes.

we obtain following neighboring matrix.

the questions are:

  1. you must define #define command , / or enum n , permanent true , false. typedef should set character named adj_mat defines neighboring matrix size n.

  2. you must write function called path, accepts parameter adjacency matrix , indexes of 2 nodes u , v , returns true if , if there directed path (by directional arrow) @ intersection u v, tree represented matrix a. otherwise returns false.

for example: path (1,8) return true. same path (1,3). on other hand path (3,8) false.

  1. first must write function (main) defines variable type adj_mat, asks user entries matrix, , indexes of 2 nodes. main function function call path, see if there directed path between 2 nodes in data. function print test result output.

have 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 2 numbers \n"); scanf("%d %d", &u, &v); printf("the answer %d", path(arr, u, v),"."); return 0; } 

the problem @ terminal when put 1,8 doesnt nothing.

you have number of issues attempt read beyond end of arr resulting in undefined behavior. first when declare array n = 11 elements, array indexes arr[0] - arr[10]. consider following in path:

     return path2 (a, n, v, u); 

you pass a, n, v, u arguments path2:

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

then in path2 attempt a[u][v] wrong, a[11][v] beyond end of array. (undefined behavior). @ order of arguments. a passed a path2, n passed u, v v, , u temp, making u = 11.

next, both of following cannot correct:

    if (u == temp && a[u][v] == false)         return true;      if (u == temp && a[u][v] == false)         return false; 

what did intend? also, this?

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

that not compile (see below). when compile, make sure enable warnings, @ minimum -wall -wextra point out problems in code.

finally, please give user guidance on range of permissible numbers, , check scanf return:

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

i make no comment on whether logic correct, should address foregoing before going further.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -