Write a c program to sort in ascending order and reverse the individual row elements of an mxn matrix

 #include <stdio.h>

#include <stdlib.h>


// Function to sort a row in ascending order

void sortRowAscending(int *row, int n) {

    for (int i = 0; i < n - 1; i++) {

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

            if (row[i] > row[j]) {

                int temp = row[i];

                row[i] = row[j];

                row[j] = temp;

            }

        }

    }

}


// Function to reverse a row

void reverseRow(int *row, int n) {

    int start = 0, end = n - 1;

    while (start < end) {

        int temp = row[start];

        row[start] = row[end];

        row[end] = temp;

        start++;

        end--;

    }

}


int main() {

    int m, n;


    printf("Enter the number of rows and columns: ");

    scanf("%d %d", &m, &n);


    int matrix[m][n];


    printf("Enter the elements of the matrix:\n");

    for (int i = 0; i < m; i++) {

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

            scanf("%d", &matrix[i][j]);

        }

    }


    // Sort each row in ascending order and then reverse it

    for (int i = 0; i < m; i++) {

        sortRowAscending(matrix[i], n);

        reverseRow(matrix[i], n);

    }


    printf("Sorted matrix with each row in descending order:\n");

    for (int i = 0; i < m; i++) {

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

            printf("%d ", matrix[i][j]);

        }

        printf("\n");

    }


    return 0;

}


Comments

Popular posts from this blog

Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

Write a c program to check whether the created linked list is palindrome or not

Write a c program to Create a Circular single Linked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node