Let's try a few exercises that will help us understand and work with arrays. These exercises will also explain concepts while giving an interview.
Let's create another class for this exercise, name it InterviewMinnumber
, and define the array in the main block. The definition code will be as follows:
int abc[][]={{2,4,5},{3,2,7},{1,2,9}};
This code declares a 3 x 3 matrix named abc
. Now we need to traverse each number in the matrix, and look for the smallest number in it. To traverse every number in the multidimensional array, we need to use the same concept that we have used in the Logic programming on multidimensional arrays section.
We use two for
loops here: an outer for
loop to traverse the rows and an inner for
loop to traverse the columns. The two for
loops code will look at follows:
for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { } }
To find the smallest number, we declare a variable, min
, and assign the first...