Max – Mean – Median & Mode Matrix operation C program

Here is a Max – Mean – Median & Mode Matrix (2D array) operation C program . The below program is follows functional approach.

Max – Mean – Median & Mode Matrix operation C program

Source Code:

#include <stdio.h>
const int N = 5;

void print_matrix(int mat[N][N]){
    for(int i = 0; i<N; i++){
        for(int j = 0; j<N; j++){
            printf("%d\t",mat[i][j]);
        }
        printf("\n");
    }
}

int mat_max(int mat[N][N]){
    int maxElement = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (mat[i][j] > maxElement) {
                maxElement = mat[i][j];
            }
        }
    }
   return maxElement; 
}

int mat_mean(int mat[N][N]){
   int sum = 0;
   for (int i=0; i<N; i++){
      for (int j=0; j<N; j++){
         sum += mat[i][j];
      }
   }
   return (int)sum/(N*N);
}

int mat_median(int mat[N][N]){    
    if (N % 2 != 0){
        return mat[N/2][N/2];
    }
    if (N%2 == 0){
        return (mat[(N-2)/2][N-1] + mat[N/2][0])/2;
    }
}

int mat_mode(int mat[N][N]){
   int maxValue = 0, maxCount = 0, k, l, i, j;
   for (k = 0; k < N; ++k) {
        for(l = 0; l < N; ++l){
            int count = 0;
            
            for (i = 0; i < N; ++i){
                for (j =0; j < N; ++j){
                    if (mat[i][j] == mat[k][l])
                    ++count;
                }
            }
            if (count > maxCount) {
                maxCount = count;
                maxValue = mat[k][l];
            }
       }
   }
   return maxValue;
}

int main(){
    int ch, tmp;
    char d ='y';
    //int mat[5][5]= {
    //    {1, 2, 3, 4, 5},
    //    {6, 7, 8, 9, 10},
    //    {11, 12, 13, 14, 15},
    //    {16, 17, 18, 19, 20},
    //    {21, 22, 23, 24, 25}};
    
    int mat[N][N];
    printf("Enter 5x5 matrix values\n");
    // insert element for 2D array
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            printf("Enter values for mat[%d][%d]:",i,j);
            scanf("%d",&mat[i][j]);
        }
    }

     // sort 2D array using bubble sort  
    for (int i = 0; i < N * N - 1; ++i) {
        for (int j = 0; j < N * N - 1 - i; ++j) {
            if (mat[j / N][j % N] > mat[(j + 1) / N][(j + 1) % N]) {
                tmp = mat[(j + 1) / N][(j + 1) % N];
	        mat[(j + 1) / N][(j + 1) % N] = mat[j / N][j % N];
	        mat[j / N][j % N] = tmp;
	    }
        }
    }

    printf("Below is the inputed 5x5 matrix...\n");
    print_matrix(mat);
    
    while(d == 'y'){
        printf("\n\nHi Sir!\nWhich operation do you want to perform.\n");
        printf("1:Find max element of matrix\n2:Find mean of matrix\n3:Find median of matrix\n4:Find mode of matrix");
        
        printf("\nEnter your choice:");
        scanf("%d",&ch);
    
        switch(ch){
            case 1:
                printf("Max element in the matrix is: %d",mat_max(mat));
            break;
        
            case 2:
                printf("Mean of the matrix: %d",mat_mean(mat));
            break;
        
            case 3:
                printf("\nMedian of the matrix : %d",mat_median(mat));
            break;
        
            case 4:
                printf("\nMode of the matrix: %d ", mat_mode(mat));
            break;
            
            default:
                printf("\nPlease enter correct mentioned choice...");
        }
        printf("\nDo you want to continue(y or n):");
        scanf("%s",&d);
    }
    printf("\nBye! Bye!\nSee you soon...");
   return 0;
}

Input & Output:

Enter 5x5 matrix values
Enter values for mat[0][0]:12
Enter values for mat[0][1]:15
Enter values for mat[0][2]:12
Enter values for mat[0][3]:12
Enter values for mat[0][4]:13
Enter values for mat[1][0]:14
Enter values for mat[1][1]:18
Enter values for mat[1][2]:19
Enter values for mat[1][3]:25
Enter values for mat[1][4]:16
Enter values for mat[2][0]:29
Enter values for mat[2][1]:28
Enter values for mat[2][2]:30
Enter values for mat[2][3]:31
Enter values for mat[2][4]:35
Enter values for mat[3][0]:32
Enter values for mat[3][1]:12
Enter values for mat[3][2]:14
Enter values for mat[3][3]:15
Enter values for mat[3][4]:16
Enter values for mat[4][0]:18
Enter values for mat[4][1]:19
Enter values for mat[4][2]:17
Enter values for mat[4][3]:16
Enter values for mat[4][4]:12
Below is the inputed 5x5 matrix...
12      12      12      12      12
13      14      14      15      15
16      16      16      17      18
18      19      19      25      28
29      30      31      32      35


Hi Sir!
Which operation do you want to perform.
1:Find max element of matrix
2:Find mean of matrix
3:Find median of matrix
4:Find mode of matrix
Enter your choice:1
Max element in the matrix is: 35
Do you want to continue(y or n):y


Hi Sir!
Which operation do you want to perform.
1:Find max element of matrix
2:Find mean of matrix
3:Find median of matrix
4:Find mode of matrix
Enter your choice:2
Mean of the matrix: 19
Do you want to continue(y or n):y


Hi Sir!
Which operation do you want to perform.
1:Find max element of matrix
2:Find mean of matrix
3:Find median of matrix
4:Find mode of matrix
Enter your choice:3

Median of the matrix : 16
Do you want to continue(y or n):y


Hi Sir!
Which operation do you want to perform.
1:Find max element of matrix
2:Find mean of matrix
3:Find median of matrix
4:Find mode of matrix
Enter your choice:4

Mode of the matrix: 12 
Do you want to continue(y or n):n

Bye! Bye!
See you soon...

Recommended Post:

Posted in: C