Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C Program: Final Sum of Max Row and Min Column

C Program: Final Sum of Max Row and Min Column

πŸ”· C Program: Final Sum of Max Row and Min Column

#include <stdio.h>

int main()
{
    int n, m;
    printf("Enter the number of rows (N): ");
    scanf("%d", &n);
    printf("Enter the number of coloumns (M): ");
    scanf("%d", &m);

    int a[n][m];

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    int max_row_sum = 0;
    for (int i = 0; i < n; i++)
    {
        int row_sum = 0;
        for (int j = 0; j < m; j++)
        {
            row_sum += a[i][j];
        }
        if (i == 0 || row_sum > max_row_sum)
        {
            max_row_sum = row_sum;
        }
    }

    int min_col_sum = 0;
    for (int j = 0; j < m; j++)
    {
        int col_sum = 0;
        for (int i = 0; i < n; i++)
        {
            col_sum += a[i][j];
        }
        if (j == 0 || col_sum < min_col_sum)
        {
            min_col_sum = col_sum;
        }
    }

    int final_sum = max_row_sum + min_col_sum;
    printf("\nsum of row with highest sum: %d\n", max_row_sum);
    printf("sum of column with lowest sum: %d\n", min_col_sum);
    printf("Final sum (Largest row sum + smallest column sum): %d\n", final_sum);

    return 0;
}
  

πŸ“˜ Explanation:

This C program works on a 2D array (matrix) and calculates two things:

πŸ”Ή Maximum Row Sum: The sum of the row which has the highest total.
πŸ”Ή Minimum Column Sum: The sum of the column which has the smallest total.

πŸ”Ή After collecting user input into the matrix, it traverses each row to find the maximum row sum.
πŸ”Ή Then it traverses each column to find the minimum column sum.
πŸ”Ή It adds both to compute the final_sum and displays all three values: max row sum, min col sum, and their sum.

πŸ” Sample Output:

Enter the number of rows (N): 3
Enter the number of coloumns (M): 3
Enter the elements of the array:
1 2 3
4 5 6
7 8 9

sum of row with highest sum: 24
sum of column with lowest sum: 12
Final sum (Largest row sum + smallest column sum): 36
    

🏷️ Keywords:

2D array in C, row sum, column sum, max row, min column, matrix manipulation in C, matrix row column sum program

Comments

Popular Posts

πŸŒ™