+ 3
Hi I want to print the largest number of each column in a matrix How do I do that?
36 Answers
+ 2
I do not know that
+ 2
int test[3][4];
int main()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cin>>test[i][j];
}
}
I can only define it I do not know how to recognize the largest number
+ 2
I could not make the columns
+ 2
I can not write the code to recognize the largest number in the column
+ 2
learner no, they are filling it using user input ie cin >> test[i][j]
+ 2
You can think of an array to be a matrix where
test[n] ~ 1 x n matrix
test[i][j] ~ i x j matrix
+ 2
First you gotta fill the array which is why they have 2 for loops
Next they should write an algorithm to find the biggest/smallest value in each column
+ 2
idk, try this: https://code.sololearn.com/c9dKc5wxEWRQ/?ref=app
I know, it looks like bad code, but...
+ 2
Uh, bruh, i forgot that i should did: https://code.sololearn.com/c9mX4r1Xwl29/?ref=app
Just replaced some variables
+ 2
Here is something that can be used as a starting point:
To find the max or min of an array, assign the value of the first element to a variable. Then iterate through the array and compare the variable with the element. if it is bigger (or smaller if you want min), reassign the value to the variable. In the end you will have the max (or min).
https://code.sololearn.com/c1BGLaX8CAKU/?ref=app
+ 2
Bob_Li Wow!!! really nice😍😘
+ 2
Vkd thanks
+ 2
Bob_Li Thank you for your help
+ 2
Hi sorry for the late response
Here is the code in java
It's a 3x3 array so initially we need a variable say max =0 which will give us our ans i.e the max element in column because we keep on comparing until and unless we get the largest value in a 2d matrix
Since we need to check in each column we write arr[j] [I] > max and in each column we output the largest value after the inner loop ends...
public class Main
{
public static void main(String[] args) {
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, {7, 8, 9 }};
int max = 0;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(arr[j][i] > max)
max = arr[j][i];
}
System.out.println(max);
}
}
}
+ 2
But here's one drawback if you want impeccable code you can initialize max = Integer.MIN_VALUE and in c++ you can use max = INT_MIN which initializes with a minimum value possible... Now why INT_MIN and not MAX because if you initialize INT_MAX then there will be no such element greater than max variable hope it helps
+ 1
An example for finding max in a single array would be :
int arr[5] = {9,3,2,10,1};
int max = arr[0]; //for now
//compare max to other values
for (int i = 1; i< 5;i++ )
if( arr[i] > max ) max = arr[i];
// max would be 10
cout << max << end;
Similar idea for your problem except instead of going down a line
You need to go down each column
+ 1
Share a link to your code
+ 1
It compiles fine. What did you mean by not being able to make the columns ?
+ 1
Step one write the code to print each column
If you input :
1 2 5 3
4 7 2 5
7 6 1 4
Output :
1 4 7
2 7 6
5 2 1
3 5 4