Home > Net >  Check if the array is a lower triangular matrix
Check if the array is a lower triangular matrix

Time:07-13

Write a function in c that takes a single 2d nxn matrix. If the matrix is a lower triangular matrix, the program should output 1 and output 0 if it is not a lower triangular matrix. Here is my code.

// function code

#include <stdio.h>
#include <stdlib.h>
#define NUMS 5    

int isLowerTriangularMatrix(int array[NUMS][NUMS]) {
    for (int row = 0; row < NUMS; row  ) {
        for (int col = 0; col < NUMS; col  ) {
            if (row > col && array[row][col] == 0) {
                return 1;
            }
            else {
                return 0;
            } 
        }
    }
}
 
// test code
int main(void) {

    int matrix[5][5] = {{1,2,3,4,5},
                        {7,8,9,2,3},
                        {7,8,9,2,3},
                        {7,8,9,2,3},
                        {7,8,9,2,3}};
    int result = isLowerTriangularMatrix(matrix, 5);
    printf("%d", result);
    return 0;
}

My question is that how to modify my code?

Here is the compiler warnings

q23.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
   16 | }

CodePudding user response:

The warning is spurious. Or rather, it should have issued a different warning, one that indicated you have code that will never be executed.

That is to say that while your code doesn't suffer from the identified problem, it is nonetheless incorrect. It always returns zero. The first pass through the loops, you check if row > col. Since it's false, you execute the else clause and return zero.

You shouldn't be returning 0 in that case. But you shouldn't be returning 1 either. You can only return 1 after you've checked the entire matrix, so the return 1 should be outside the loops.

This fixes these problems:

int isLowerTriangularMatrix( int array[ NUMS ][ NUMS ] ) {
   for ( int row = 0; row < NUMS;   row ) {
      for ( int col = 0; col < NUMS;   col ) {
         if ( col > row && array[ row ][ col ] != 0 ) {
            return 0;
         } 
      }
   }

   return 1;
}

There's one last issue to address, and it's the needless "visiting" of cells in the lower triangle. It's trivial to skip these.

int isLowerTriangularMatrix( int array[ NUMS ][ NUMS ] ) {
   for ( int row = 0; row < NUMS;   row ) {
      for ( int col = row 1; col < NUMS;   col ) {
         if ( array[ row ][ col ] != 0 ) {
            return 0;
         } 
      }
   }

   return 1;
}

CodePudding user response:

As I mentioned in my top comments ...

For your original question as to how to initialize a matrix:

With an initializer:

int mtx[3][3] = {
    { 3, 9, 7 },
    { 0, 5, 6 },
    { 0, 0, 1 }
};

Or, with assignment statements:

int mtx[3][3];
mtx[0][0] = 3;
mtx[0][1] = 9;
mtx[0][2] = 7;
mtx[1][0] = 0;
mtx[1][1] = 5;
mtx[1][2] = 6;
mtx[2][0] = 0;
mtx[2][1] = 0;
mtx[2][2] = 1;

@Craig Estey Yes, it is a method, but it also means the n not working very well on my code. – Cedric xu

You do not have a true 2D int array. You have a 1D array of int * to int arrays. Do this instead:

#define NUMS 5
int mtx[NUMS][NUMS];

You can define the function as:

int isLowerTriangularMatrix(int arr[NUMS][NUMS])

Or, if you want the function to handle the size dynamically:

int isLowerTriangularMatrix(int n,int arr[n][n])

The first one will be a bit faster and the second a bit more flexible [albeit a bit slower].

Edit: Your function is being [correctly] flagged as reaching the end of the function and not having a return statement. It can't [seem to] determine that the code will always do one of the return statements inside the loops. Note that both gcc and clang flag it the same way.

I'm not sure your function will return a valid result because the if does a return on both the if and the else. So, it will never examine all the elements [that it needs to].

Usually, the inner return is just on the if part. Then, at the function bottom do: (e.g.) return 1;


Here's a refactored version:

// function code

#include <stdio.h>
#include <stdlib.h>

#define NUMS 5

int
isLowerTriangularMatrix(int array[NUMS][NUMS])
{

    // we only need to probe the upper right part of the matrix
    // if we see non-zero, it's _not_ triangular

    for (int row = 0; row < NUMS; row  ) {
        for (int col = row   1; col < NUMS; col  ) {
            //printf("arr[%d][%d] = %d\n",row,col,array[row][col]);
            if (array[row][col] != 0)
                return 0;
        }
    }

    return 1;
}

 // test code
int
main(void)
{

    // this is _not_ a triangular matrix
    int matrix[5][5] = {
        {1, 2, 3, 4, 5},
        {7, 8, 9, 2, 3},
        {7, 8, 9, 2, 3},
        {7, 8, 9, 2, 3},
        {7, 8, 9, 2, 3}
    };

    int result = isLowerTriangularMatrix(matrix);
    printf("%d\n", result);

    // this _is_ a triangular matrix
    int m2[5][5] = {
        { 1, 0, 0, 0, 0 },
        { 3, 1, 0, 0, 0 },
        { 4, 5, 1, 0, 0 },
        { 7, 6, 2, 1, 0 },
        { 8, 9, 7, 3, 1 }
    };

    result = isLowerTriangularMatrix(m2);
    printf("%d\n", result);

    return 0;
}

Here is the program output (with the debug printf):

arr[0][1] = 2
0
arr[0][1] = 0
arr[0][2] = 0
arr[0][3] = 0
arr[0][4] = 0
arr[1][2] = 0
arr[1][3] = 0
arr[1][4] = 0
arr[2][3] = 0
arr[2][4] = 0
arr[3][4] = 0
1
  • Related