Home c Where is the mistake? A small program on si.Postroit incidence matrix for...

Where is the mistake? A small program on si.Postroit incidence matrix for a given adjacency matrix of a directed graph

Author

Date

Category

Write program incidence matrix calculation given by the digraph adjacency matrix.


Help please, very zaputalsya.Pochemu k (the number of columns of the matrix B) = 10 in the output, although it should be 6.Obyasnite I upustil.I how to implement such algoritm.Poryadok columns in the output matrix is ​​not important, the main thing – what would their content was vernym.I general conclusion is not quite what you want …

#include & lt; stdio.h & gt;
  #Include & lt; Windows.h & gt;
  #Include & lt; conio.h & gt;
  INT MAIN ()
  {
    int i, j, X, Y;
    int a [100] [100], u;
    int kor [2];
    printf ( "Enter the dimension of the matrix X =");
    scanf_s ( "% d", & amp; X);
    printf ( "Enter the dimension of the matrix Y =");
    scanf_s ( "% d", & amp; Y);
    for (i = 0; i & lt; X; i ++)
      for (j = 0; j & lt; Y; j ++)
      {
        printf ( "a [% d] [% d] =", i + 1, j + 1);
        if ((scanf_s ( "% d", & amp;! u)) = 0)
          a [i] [j] = u;
      }
    printf ( "The entered array: \ n");
    for (i = 0; i & lt; X; i ++)
    {
      for (j = 0; j & lt; Y; j ++)
        printf ( "% d", a [i] [j]);
      PrintF ("\ n");
    }
    int k = 0;
    int incidence [10] [10] =
    {
    {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}
    };
    for (i = 0; i & lt; X; i ++) {
      for (j = 0; j & lt; Y; j ++)
      {
        if (a [i] [j] = 1) {
          if (i == j) {
            incidence [i] [k] = 2;
            k ++;
        Break;
        }
          ELSE {
            incidence [i] [k] = -1;
            incidence [j] [k] = 1;
            k ++;
          }
        }
      }
    }
    printf ( "The output array: \ n");
    for (i = 0; i & lt; X; i ++)
    {
      for (j = 0; j & lt; k; j ++)
        printf ( "% 5d", incidence [i] [j]);
      PrintF ("\ n");
    }
    SYSTEM ("PAUSE");
    Return 0;
  }

Answer 1, Authority 100%

The first error in line:

if (a [i] [j] = 1)

This condition will always be carried out, so I think there is need to compare the == , but does not assign = .

So I do not understand graphs, but why would you use the operator break where i == j ?

And just a small blot. You can k ++ to make a provision, because the in any case k to increase by one. And initialize static matrix of zeros can be in one line: int incidence [10] [10] {0};

for (i = 0; i & lt; X; i ++) {
  for (j = 0; j & lt; Y; j ++) {
    if (a [i] [j] == 1) {
      if (i == j)
        incidence [i] [k] = 2;
      ELSE {
        incidence [i] [k] = -1;
        incidence [j] [k] = 1;
      }
      k ++;
    }
  }
}

The output of the program:


Answer 2, Authority 100%

  1. adjacency matrix square NxN, so entering and using the wrong two sizes
  2. The incidence matrix declared 10×10, initialized as a 5×5, but in general its size should be NXE, where E – the number of arcs, which can be up to N ^ 2 (if the parallel arcs not), its definition should count edinichka in the adjacency matrix (with input), or to use a vector, adding the column

If these shortcomings fix – suddenly earn?

Yeah, Paul More If = noticed

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions