Displaying the largest number from the column with the smallest number

In the previous example, we observed how to print the smallest number from the array matrix. In this example, we will look for the smallest number in the matrix and then the maximum number in the same column. The logic behind this is: we first find the minimum number, remember the row number it belongs to, and extract the maximum number in the same column.

Let's use the same matrix that we used in the previous example. The output for this exercise in the matrix that we are using will be 4. The following steps will be implemented to perform this exercise:

  1. Find the minimum value in the matrix that we declare
  2. Identify the column of that minimum number
  3. Find the maximum number in the identified column

We already performed step 1 in the previous example, where we found the minimum number in the matrix, so we will be using the same code for this example and just change the variables a little:

int abc[][]={{2, 4, 5}, {3, 0, 7}, {1, 2, 9}}

Let's move to step 2. If we observe the code, we see that i stands for the row number and j stands for the column number. So j will take the value of the column where the smallest number is present and we will take this value of j and assign it to a variable that will call mincolumn. So we write code under the swapping command, which will assign the value of j to mincolumn. The code will look something like this:

mincoloumn=j;

So the moment we find the smallest number in the matrix, we assign to it the value of j, which is the column number to mincloumn. In this case, the value of mincolumn will be 1. This takes care of step 2.

In step 3, we look for the maximum number from the column in which the minimum number is present. We create a while loop outside the outer for loop that we had created to find the lowest number in the matrix. We initialize the condition variable, k, as 0 and iterate it every time the while look condition is met. The condition for the while loop is set to k less than 3; this is because we have three rows to traverse to look for the maximum value in them. The code for the while loop will look as follows:

while(k<3)
{
k++;
}

We declare a variable named max and give it an initial value of row 0 and column mincolumn. This gives the variable max an initial value of 4, since 4 is the first element in the row which contains the minimum number in the matrix. The declaration code will be as follows:

int max=abc[0][mincoloumn];

Within the while loop, we add an if loop and set a condition that compares whether the variable in the column with the minimum number is greater than the variable max that we declared. If the condition is met, the value of that number is assigned to the max variable and the controller moves out of the if loop and back to the while look after iterating k by 1. The iteration will take the controller to the next row as k is used to signify the row that is being traversed to look for the maximum number.

The code for the if loop will look as follows:

if(abc[k][mincoloumn]>max)
{
max=abc[k][mincoloumn];
}

So, for the first value of k, which is 0, we go to the first row and second column and assign the value to max; in this example, the value is 4. In the if condition, we compare the value of the first row, second column with the value of max. In this example, both the values are the same, so the if loop is not executed and we iterate k and enter the while loop again. Next, we compare the value of the second row, second column with the value of max; we move to the second row because the value of k is iterated by 1 and the current value of k is 1. So, on comparing, we see that o is less that 4, where 4 is the value of the max variable. The condition is not met again and the if loop is skipped again. This continues for the third row too and the final value of max is 4, which is the largest number in the column. Finally, we leave a print statement to print the value of max at the end.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.244.83