+ 3

Hi I want to print the largest number of each column in a matrix How do I do that?

14th May 2022, 5:53 AM
Mhdis
Mhdis - avatar
36 ответов
+ 2
I do not know that
14th May 2022, 6:08 AM
Mhdis
Mhdis - avatar
+ 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
14th May 2022, 6:17 AM
Mhdis
Mhdis - avatar
+ 2
I could not make the columns
14th May 2022, 6:30 AM
Mhdis
Mhdis - avatar
+ 2
I can not write the code to recognize the largest number in the column
14th May 2022, 6:44 AM
Mhdis
Mhdis - avatar
+ 2
learner no, they are filling it using user input ie cin >> test[i][j]
14th May 2022, 5:17 PM
Raul Ramirez
Raul Ramirez - avatar
+ 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
14th May 2022, 5:30 PM
Raul Ramirez
Raul Ramirez - avatar
+ 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
14th May 2022, 5:55 PM
Raul Ramirez
Raul Ramirez - avatar
+ 2
idk, try this: https://code.sololearn.com/c9dKc5wxEWRQ/?ref=app I know, it looks like bad code, but...
14th May 2022, 6:31 PM
SCP-173 Скульптура
SCP-173 Скульптура - avatar
+ 2
Uh, bruh, i forgot that i should did: https://code.sololearn.com/c9mX4r1Xwl29/?ref=app Just replaced some variables
14th May 2022, 6:46 PM
SCP-173 Скульптура
SCP-173 Скульптура - avatar
+ 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
14th May 2022, 11:01 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li Wow!!! really nice😍😘
16th May 2022, 1:38 AM
Vkd
Vkd - avatar
+ 2
Vkd thanks
16th May 2022, 3:39 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li Thank you for your help
7th Jun 2022, 10:07 AM
Mhdis
Mhdis - avatar
+ 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); } } }
7th Jun 2022, 2:39 PM
Ankit Kumar Singh
Ankit Kumar Singh - avatar
+ 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
7th Jun 2022, 2:43 PM
Ankit Kumar Singh
Ankit Kumar Singh - avatar
+ 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
14th May 2022, 6:25 AM
Raul Ramirez
Raul Ramirez - avatar
+ 1
Share a link to your code
14th May 2022, 6:32 AM
Raul Ramirez
Raul Ramirez - avatar
+ 1
It compiles fine. What did you mean by not being able to make the columns ?
14th May 2022, 6:40 AM
Raul Ramirez
Raul Ramirez - avatar
+ 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
14th May 2022, 6:50 AM
Raul Ramirez
Raul Ramirez - avatar