B
B
bunnyyy2020-10-29 19:29:39
C++ / C#
bunnyyy, 2020-10-29 19:29:39

How to remove a row from a matrix?

It is necessary to remove from the matrix 6 * 7 the row containing the maximum element.
Here is a code snippet:

#define m 7
#define n 6
#define k 6
.....
for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      max = A[0][0];
      if (max < A[i][j])
      {
        max = A[i][j];
        str = i;
      }
    }
  }
  for (int i = str; i < m-1; i++) {
    for (int j = 0; j < k; j++)
    {
      B[i][j] = A[i + 1][j];
    }
  }

Displays just zeroed elements and strange numbers in the last row of the matrix.. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-10-29
@Rsa97

max = A[0][0];must be outside the cycles seeking the maximum.
B[i][j] = A[i + 1][j];- where did B come from?

J
jcmvbkbc, 2020-10-29
@jcmvbkbc

In full code:

for (int i = str; i < m-1; i++) {
    for (int j = 0; j <= n; j++)
    {
      B[i][j] = A[i + 1][j];
    }
  }

all B strings up to str were left uninitialized. In the loop on j, the condition is false, it should be j < n
In the last loop
for (int i = 0; i < m; i++)
, it should be wrong, it should be i < m - 1
kthrown out of the code and replaced withm - 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question