Min/Max 2d Array
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
My homework asks me to define a 2d array and find the max value, and the min value in the index of the max value here's how I did it
public class Main
public static void main(String args)
int ar = 11,12,9,14,60,200,50,23,19,25,31,100,33,34,35;
int max = ar[0][0];
int y = 0;
for (int i = 0; i <ar.length;i++)
for (int k=0;k < ar[i].length;k++)
if (ar[i][k] > max)
max = ar[i][k];
y = i;
int minInMaxLine = ar[y][0];
for (int i =0;i<ar[y].length;i++)
if (ar[y][i] < minInMaxLine)
minInMaxLine = ar[y][i];
System.out.println("Max value is : "+max);
System.out.println("Index of max value is : "+y);
System.out.println("Min value in max value's index is : "+minInMaxLine);
this outputs :
Max value is : 200
Index of max value is : 1
Min value in max value's index is : 19
java array
add a comment |Â
up vote
0
down vote
favorite
My homework asks me to define a 2d array and find the max value, and the min value in the index of the max value here's how I did it
public class Main
public static void main(String args)
int ar = 11,12,9,14,60,200,50,23,19,25,31,100,33,34,35;
int max = ar[0][0];
int y = 0;
for (int i = 0; i <ar.length;i++)
for (int k=0;k < ar[i].length;k++)
if (ar[i][k] > max)
max = ar[i][k];
y = i;
int minInMaxLine = ar[y][0];
for (int i =0;i<ar[y].length;i++)
if (ar[y][i] < minInMaxLine)
minInMaxLine = ar[y][i];
System.out.println("Max value is : "+max);
System.out.println("Index of max value is : "+y);
System.out.println("Min value in max value's index is : "+minInMaxLine);
this outputs :
Max value is : 200
Index of max value is : 1
Min value in max value's index is : 19
java array
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My homework asks me to define a 2d array and find the max value, and the min value in the index of the max value here's how I did it
public class Main
public static void main(String args)
int ar = 11,12,9,14,60,200,50,23,19,25,31,100,33,34,35;
int max = ar[0][0];
int y = 0;
for (int i = 0; i <ar.length;i++)
for (int k=0;k < ar[i].length;k++)
if (ar[i][k] > max)
max = ar[i][k];
y = i;
int minInMaxLine = ar[y][0];
for (int i =0;i<ar[y].length;i++)
if (ar[y][i] < minInMaxLine)
minInMaxLine = ar[y][i];
System.out.println("Max value is : "+max);
System.out.println("Index of max value is : "+y);
System.out.println("Min value in max value's index is : "+minInMaxLine);
this outputs :
Max value is : 200
Index of max value is : 1
Min value in max value's index is : 19
java array
My homework asks me to define a 2d array and find the max value, and the min value in the index of the max value here's how I did it
public class Main
public static void main(String args)
int ar = 11,12,9,14,60,200,50,23,19,25,31,100,33,34,35;
int max = ar[0][0];
int y = 0;
for (int i = 0; i <ar.length;i++)
for (int k=0;k < ar[i].length;k++)
if (ar[i][k] > max)
max = ar[i][k];
y = i;
int minInMaxLine = ar[y][0];
for (int i =0;i<ar[y].length;i++)
if (ar[y][i] < minInMaxLine)
minInMaxLine = ar[y][i];
System.out.println("Max value is : "+max);
System.out.println("Index of max value is : "+y);
System.out.println("Min value in max value's index is : "+minInMaxLine);
this outputs :
Max value is : 200
Index of max value is : 1
Min value in max value's index is : 19
java array
edited Mar 22 at 6:00
Raystafarian
5,4331046
5,4331046
asked Mar 22 at 5:41
Noor Mo
1
1
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
This approach looked great I think you should try to consider more cases then the ones you added in your example your algorithm should consider cases as if the 2d array was null as well. For reference please click here.
add a comment |Â
up vote
0
down vote
I don't think it can get much simpler or more straightforward than your code. One way to improve upon what you already have is to also handle the case that the maximum value occurs more than once. True, the exercise does not prescribe how to deal with this situation, but it's something you might want to consider.
You could also choose a more meaningful variable name for y
, like you did with the other variables.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This approach looked great I think you should try to consider more cases then the ones you added in your example your algorithm should consider cases as if the 2d array was null as well. For reference please click here.
add a comment |Â
up vote
1
down vote
This approach looked great I think you should try to consider more cases then the ones you added in your example your algorithm should consider cases as if the 2d array was null as well. For reference please click here.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This approach looked great I think you should try to consider more cases then the ones you added in your example your algorithm should consider cases as if the 2d array was null as well. For reference please click here.
This approach looked great I think you should try to consider more cases then the ones you added in your example your algorithm should consider cases as if the 2d array was null as well. For reference please click here.
edited Mar 22 at 23:46
Isac
494210
494210
answered Mar 22 at 6:08
crisam
112
112
add a comment |Â
add a comment |Â
up vote
0
down vote
I don't think it can get much simpler or more straightforward than your code. One way to improve upon what you already have is to also handle the case that the maximum value occurs more than once. True, the exercise does not prescribe how to deal with this situation, but it's something you might want to consider.
You could also choose a more meaningful variable name for y
, like you did with the other variables.
add a comment |Â
up vote
0
down vote
I don't think it can get much simpler or more straightforward than your code. One way to improve upon what you already have is to also handle the case that the maximum value occurs more than once. True, the exercise does not prescribe how to deal with this situation, but it's something you might want to consider.
You could also choose a more meaningful variable name for y
, like you did with the other variables.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I don't think it can get much simpler or more straightforward than your code. One way to improve upon what you already have is to also handle the case that the maximum value occurs more than once. True, the exercise does not prescribe how to deal with this situation, but it's something you might want to consider.
You could also choose a more meaningful variable name for y
, like you did with the other variables.
I don't think it can get much simpler or more straightforward than your code. One way to improve upon what you already have is to also handle the case that the maximum value occurs more than once. True, the exercise does not prescribe how to deal with this situation, but it's something you might want to consider.
You could also choose a more meaningful variable name for y
, like you did with the other variables.
answered Mar 23 at 8:31
Stingy
1,888212
1,888212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190170%2fmin-max-2d-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password