Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C Program To Check Upper Triangular Matrix or Not | C Programs
Latest :

C Program To Check Upper Triangular Matrix or Not | C Programs

C program to determine whether the given matrix is an upper triangular matrix or not. A square matrix can be called an upper triangular matrix if all the elements below the main diagonal are zero.

The inputs required for this are the size of the square matrix (n) along with the data values or elements of the matrix (2D array). Our desired output is the statement telling whether it is upper triangular matrix or not.

The first step here is to gather all the required inputs for the given problem statement. To read all the necessary inputs at runtime, the predefined input function, scanf(). This function reads any primitive datatype input and on the basis of the format specifier the input type will be decided.

For integers, it is ‘%d’ format specifier. We first read the size of square matrix following which we’ll create an 2D array of the size arr[n][n] dynamically using calloc() function. Then, we read the data values or elements of the matrix (2D array) by iterating through the rows and columns.

int n,i, j,**arr1,c=0;

printf(“\nEnter the N value for square matrix :”);

scanf(“%d”,&n);

arr1 = (int **)calloc(n,sizeof(int*));

for (i=0; i<n; i++)

arr1[i] =(int *)calloc(n,sizeof(int*));

printf(“\nEnter the elements in %dX%d matrix :”,n,n);

for (i = 0; i<n;i++) {

for(j=0;j<n; j++) {

scanf(“%d”,&arr1[i][j]);

}

}

After reading all the inputs, we display the 2D array in matrix format with every row in a new line for a better understanding of the user. We do so using the predefined output function, printf() and display it on the output console screen.

Now, we iterate through every column from i=0 t0 n and for every column we check whether all the elements from i+1 to n rows and zero or not.

For this, we maintain a flag variable (c) which is initially zero. Then we iterate and check for every value, if any value if the given condition is non-zero then, we update the flag value (c) to 1 and break from the iteration and exit all the loops.

for (i=0;i<n;i++) {

for(j=i+1;j<n;j++) {

if(arr1[j][i]) {

c=1;

break;

}

}

if(c)

break;

}

After completion of the iterations as it exits the loop we check whether the value of flag (c) is zero or not. If it is 1 then, the given matrix is not an upper triangular matrix hence, we display the same on console screen.

Else, if the value is zero it means, all the values below the main diagonal are zero and that it is an upper triangular matrix hence, we display the same on the console screen.

if(c)

printf(“given matrix is not upper triangular matrix”);

else

printf(“given matrix is upper triangular matrix”);

Check Whether a Matrix Is Upper Triangular or Not

Output:

Using Function

Functions are generally used to make the code reusable and enhance its readability. Using functions, if same logic that is within a function is required elsewhere in the code then, we can just call the function and pass the required parameters instead of rewriting it. Placing it in separate function blocks makes the code lot cleaner and readable.

To implement the same we first read the size of the square matrix and create a matrix of the same size using dynamic memory allocation, calloc(). Then, we call the function input() which reads the data values or elements of the array or matrix.

Then we display the 2D array using print() function in matrix format with every row in a new line.

Then, we make a function call to the check() function which contains the same logic as above to determine whether it is an upper triangular matrix or not. If it encounters any non-zero element below the main diagonal it returns zero.

Until the end if there is no non-zero element below the main diagonal then, it return 1. So when, 1 is returned then, the main method prints that it is an upper triangular matrix else, it prints that, it is not an upper triangular matrix.

This way, we can differentiate based on the functionality and split it into different functions thereby, enhancing the readability of the code and making it reusable.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...