Latest :

C Program : To Find the Maximum Element in a Column

Write a C program to find the maximum element in each column of a given matrix. To do so, we require the size of matrix or 2D array and the elements of the array as inputs. Our desired output is the maximum element of each column displayed on the console screen.

For this, the first step is to gather all the necessary inputs. To read the inputs at runtime from the keyboard, we have a well-known predefined input function in C,scanf(). It reads primitive datatype input at runtime and based on the format specifier mentioned, the datatype of the input being read is determined.

As all the inputs are of integer type, we use ‘%d’ format specifier for all of them. Initially, we have to read the size (n) of the square matrix and create a 2D array (a[n][n]) of the same size dynamically using calloc() function of the “stdlib.h” library.

We then iterate through all the rows and columns using looping statements to read the elements of the array.

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

scanf(“%d”,&n);

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

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

a[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”,&a[i][j]);

}

}

Later, we can display this input array on the console screen in matrix format with every row in a new line for better understanding of the user. This can be done using predefined output function, printf().

We then iterate through each column, and for every column we initially assign the first index(a[0][i]) to max variable. Then we iterate row wise of each column and check if that element is greater than max variable. If yes then, we’ll update the max variable to this value and continue.

After completion of each inner iteration, the max variable in the end will be having the value of maximum element of that column and we display the same on the console screen and continue the same for the next column.

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

max=a[0][i];

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

if(a[j][i]>max) {

max=a[j][i];

}

}

printf(“max in %d column is %d\n”,i+1,max);

}

After completion of all the iterations, we will now have displayed the maximum element of each column in the given array which is our desired resultant output.

Output:

Program to Find the Maximum Element in Each Column Using Function

We have seen earlier the logic to find the maximum element of each row in the array. But we can notice that, the entire code is within main method. The disadvantage with it is that, if same logic is required elsewhere in the same code then, we have to rewrite it again. Apart from that, since the entire code is within main method, it could be confusing to find some part and this makes it less readable.

So, to make the code reusable and enhance the readability of the code, we can make use of functions. If we split and place each functionality within a separate function blocks then, if same functionality/ logic is needed somewhere else within the code, we can just make a function call instead.

Apart from that using functions, makes the code look lot cleaner and thereby, also enhances the readability.

To implement this for given problem statement, we can place the statements to read the elements of the array within input() function, displaying the array in matrix format within print() function and the logic to determine the maximum element of each column within MaxColumn() function.

So, in main method, we have to first read the input array size and create the array of the same size dynamically. Then, we invoke the input() function to read the array elements and display it in matrix format by calling print() function.

Later, we make a function call to the MaxColumn() function to display the maximum element of each column. This way using functions, we can increase the readability of the code and make it reusable.

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 ...