Min/Max 2d Array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












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







share|improve this question



























    up vote
    0
    down vote

    favorite
    1












    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







    share|improve this question























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      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







      share|improve this question













      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









      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 22 at 6:00









      Raystafarian

      5,4331046




      5,4331046









      asked Mar 22 at 5:41









      Noor Mo

      1




      1




















          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.






          share|improve this answer






























            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.






            share|improve this answer





















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              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






























              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.






              share|improve this answer



























                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.






                share|improve this answer

























                  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.






                  share|improve this answer















                  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.







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Mar 22 at 23:46









                  Isac

                  494210




                  494210











                  answered Mar 22 at 6:08









                  crisam

                  112




                  112






















                      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.






                      share|improve this answer

























                        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.






                        share|improve this answer























                          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.






                          share|improve this answer













                          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.







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Mar 23 at 8:31









                          Stingy

                          1,888212




                          1,888212






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              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













































































                              Popular posts from this blog

                              Greedy Best First Search implementation in Rust

                              Function to Return a JSON Like Objects Using VBA Collections and Arrays

                              C++11 CLH Lock Implementation