Latest :

C Program : To Find Maximum Element in A Row | C Programs

Write a C program to find the maximum element in each row of a given square matrix. To do so, we require the size of the square matrix and the data values or elements of the square matrix as input. Our desired output is the maximum value of each row being displayed on the console screen in the end.

The first step after understanding any given problem statement thoroughly is, gathering the necessary inputs. For reading all the required inputs at runtime in C, we can make use of predefined input function, scanf(). This function is well-known for reading inputs of any primitive datatype like int, double, char, long, etc., at runtime from the keyboard. The datatype of the input being read is determined by the format specifier mentioned in the scanf() function.

Since all the inputs here as integers, we’ll use ‘%d’ format specifier. We first read the size (n) of the square matrix and create a 2D array or matrix (a[n][n]) of the mentioned size dynamically using calloc(). Then, we iterate through the 2D array through all the rows and columns and 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 display the 2D array in the matrix format with every row in a new line on the console screen using the predefined output function, printf().

After gathering all the inputs, we iterate through every row in the array and check for the maximum value. For every row, we first initialize the max variable to the first element of the row (a[i][0]). Then we iterate from 1 to n column of the same row and check whether the value at that index position is greater than max variable.

If it is greater then, we update the max variable to this value and then, continue to do the same. After iterating for each row, the value stored in max variable is the maximum element of that row. Hence, we display this on the console screen and proceed with the next row.

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

max=a[i][0];

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

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

max=a[i][j];

}

}

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

}

By the completion of all the iterations for every row, we will now have displayed the maximum element in each row of the given input array   on the console screen.

Maximum Element in A Row

Output:

Using Function

We have seen in the above method, the logic to be used to display the maximum element of each row in the give matrix or 2D array. But as we can notice, the entire code is within main method only. The disadvantage we encounter because of this is that, if same logic is required elsewhere, we would have to rewrite it.

Also, since the entire code is within main method, finding a particular part can be time consuming so, the code isn’t very readable.

To overcome these disadvantages, we can use functions for the same. The time and space complexity in both the cases are the same since, the same logic is used. But, using functions, if same logic is needed somewhere else, we can just make a function call instead and pass necessary parameters thereby, making the code reusable.

Apart from that, since, everything is split based on its functionality and placed within separate function blocks, the readability of the code is improved.

To implement this, we can first read the size of square matrix using scanf() and create an array of the same size dynamically using calloc() in main method. Then, we call the input() function which reads the elements of the 2D array or matrix.

Following which, we call the print() function, which displays the array in matrix form with every row in a new line. Then, we call the RowMaximum() function which contains the logic as above.

This way the same logic is used but, the code is split into three parts- input, print , RowMaximum and each of this function is reusable any number of times within the code. Hence, using functions we can make the code reusable and enhance its readability.

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