Dividing array elements on digits - problem with crashing





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.



My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.



Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90



Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13



The catch is, we are not allowed to use functions or more than one array to solve the problem.



Here's what I've already tried:



for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}

digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}

if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}


I'm not sure what's wrong with the code and why the program crashes. Thank you.










share|improve this question























  • First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

    – Jorge Adriano
    Nov 23 '18 at 17:00


















0















I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.



My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.



Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90



Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13



The catch is, we are not allowed to use functions or more than one array to solve the problem.



Here's what I've already tried:



for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}

digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}

if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}


I'm not sure what's wrong with the code and why the program crashes. Thank you.










share|improve this question























  • First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

    – Jorge Adriano
    Nov 23 '18 at 17:00














0












0








0








I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.



My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.



Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90



Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13



The catch is, we are not allowed to use functions or more than one array to solve the problem.



Here's what I've already tried:



for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}

digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}

if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}


I'm not sure what's wrong with the code and why the program crashes. Thank you.










share|improve this question














I'm a beginner who has unfortunately encountered a problem I cannot solve :) I apologize in advance since English is not my first language.



My task is to sort an array so that the elements with the same digits come first, followed by the rest of an array.



Example:
Input array: 1 22 43 444 51 16 7 8888 90 11 -1
After sorting: 1,22,444,7,8888,11,43,51,16,90



Input array: 12, 33, 1, 19, 44, 11, 27, 76, 13
After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13



The catch is, we are not allowed to use functions or more than one array to solve the problem.



Here's what I've already tried:



for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
digit=a[j]%10;
while (a[j]>0)
{
if (a[j]%10!=digit)
same=0;
a[j]/=10;
}

digit1=a[j+1]%10;
while (a[j+1]>0)
{
if (a[j+1]%10!=digit1)
same1=0;
a[j+1]/=10;
}

if (same==0 && same1==1)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}


I'm not sure what's wrong with the code and why the program crashes. Thank you.







arrays sorting crash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 16:27









JinhyeongJinhyeong

1




1













  • First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

    – Jorge Adriano
    Nov 23 '18 at 17:00



















  • First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

    – Jorge Adriano
    Nov 23 '18 at 17:00

















First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

– Jorge Adriano
Nov 23 '18 at 17:00





First and foremost you should document your code. Because anyone helping you will want to know what each piece is supposed to do. But more importantly because it will help you understand what you're doing. What is the index i and what does that outer loop accomplish? What is the index j and what does that inner loop accomplish?

– Jorge Adriano
Nov 23 '18 at 17:00












2 Answers
2






active

oldest

votes


















0














This might help,



    for (int i = 0; i < in.length; i++) {
for (int j = in.length - 1; j > i; j--) {
if (in[j] < in[j - 1]) {
int temp = in[j];
in[j] = in[j - 1];
in[j - 1] = temp;
}
}
}





share|improve this answer































    0














    Its not really coherent your definition of the problem to the samples you are providing, for instance you say :



    Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
    After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13


    But according to your statements, should not 11 be the very first element?



    Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :



    #include <iostream>
    #include <vector>

    using namespace std;


    bool samedigits(int x)
    {
    //if ( x < 10 ) return false;
    int digit = x%10;
    while(x>0)
    {
    if(x%10 != digit) return false;
    x = x/10;
    }
    return true;
    }

    int main() {
    int same = 0;
    int same1 = 1;
    int temp = 0;
    int n = 0;
    int digit = 0;
    int digit1 = 0;

    vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

    for(int n : a) {
    cout << n << " ";
    }

    for (int i=0; i <= a.size() -1; i++)
    {

    for (int j=0; j <= a.size() - 1 ; j++)
    {
    if ( j != i) {

    if (samedigits(a[j]) && samedigits(a[i])) {
    if (a[j] > a[i]) {
    int tmp = a[j];
    a[j] = a[i];
    a[i] = tmp;
    continue;
    }
    }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
    int tmp = a[j];
    a[j] = a[i];
    a[i] = tmp;
    continue;

    } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
    continue;
    }else {
    if (a[j] > a[i]) {
    int tmp = a[j];
    a[j] = a[i];
    a[i] = tmp;
    continue;
    }
    }
    }
    }
    }
    cout << endl;
    for(int n : a) {
    cout << n << " ";
    }

    }


    And it returns :



    enter image description here



    I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.



    You can test this here :



    https://www.jdoodle.com/online-compiler-c++



    Regards!






    share|improve this answer
























      Your Answer






      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: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450120%2fdividing-array-elements-on-digits-problem-with-crashing%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This might help,



          for (int i = 0; i < in.length; i++) {
      for (int j = in.length - 1; j > i; j--) {
      if (in[j] < in[j - 1]) {
      int temp = in[j];
      in[j] = in[j - 1];
      in[j - 1] = temp;
      }
      }
      }





      share|improve this answer




























        0














        This might help,



            for (int i = 0; i < in.length; i++) {
        for (int j = in.length - 1; j > i; j--) {
        if (in[j] < in[j - 1]) {
        int temp = in[j];
        in[j] = in[j - 1];
        in[j - 1] = temp;
        }
        }
        }





        share|improve this answer


























          0












          0








          0







          This might help,



              for (int i = 0; i < in.length; i++) {
          for (int j = in.length - 1; j > i; j--) {
          if (in[j] < in[j - 1]) {
          int temp = in[j];
          in[j] = in[j - 1];
          in[j - 1] = temp;
          }
          }
          }





          share|improve this answer













          This might help,



              for (int i = 0; i < in.length; i++) {
          for (int j = in.length - 1; j > i; j--) {
          if (in[j] < in[j - 1]) {
          int temp = in[j];
          in[j] = in[j - 1];
          in[j - 1] = temp;
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 16:59









          ChameeraChameera

          628




          628

























              0














              Its not really coherent your definition of the problem to the samples you are providing, for instance you say :



              Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
              After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13


              But according to your statements, should not 11 be the very first element?



              Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :



              #include <iostream>
              #include <vector>

              using namespace std;


              bool samedigits(int x)
              {
              //if ( x < 10 ) return false;
              int digit = x%10;
              while(x>0)
              {
              if(x%10 != digit) return false;
              x = x/10;
              }
              return true;
              }

              int main() {
              int same = 0;
              int same1 = 1;
              int temp = 0;
              int n = 0;
              int digit = 0;
              int digit1 = 0;

              vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

              for(int n : a) {
              cout << n << " ";
              }

              for (int i=0; i <= a.size() -1; i++)
              {

              for (int j=0; j <= a.size() - 1 ; j++)
              {
              if ( j != i) {

              if (samedigits(a[j]) && samedigits(a[i])) {
              if (a[j] > a[i]) {
              int tmp = a[j];
              a[j] = a[i];
              a[i] = tmp;
              continue;
              }
              }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
              int tmp = a[j];
              a[j] = a[i];
              a[i] = tmp;
              continue;

              } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
              continue;
              }else {
              if (a[j] > a[i]) {
              int tmp = a[j];
              a[j] = a[i];
              a[i] = tmp;
              continue;
              }
              }
              }
              }
              }
              cout << endl;
              for(int n : a) {
              cout << n << " ";
              }

              }


              And it returns :



              enter image description here



              I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.



              You can test this here :



              https://www.jdoodle.com/online-compiler-c++



              Regards!






              share|improve this answer




























                0














                Its not really coherent your definition of the problem to the samples you are providing, for instance you say :



                Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
                After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13


                But according to your statements, should not 11 be the very first element?



                Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :



                #include <iostream>
                #include <vector>

                using namespace std;


                bool samedigits(int x)
                {
                //if ( x < 10 ) return false;
                int digit = x%10;
                while(x>0)
                {
                if(x%10 != digit) return false;
                x = x/10;
                }
                return true;
                }

                int main() {
                int same = 0;
                int same1 = 1;
                int temp = 0;
                int n = 0;
                int digit = 0;
                int digit1 = 0;

                vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

                for(int n : a) {
                cout << n << " ";
                }

                for (int i=0; i <= a.size() -1; i++)
                {

                for (int j=0; j <= a.size() - 1 ; j++)
                {
                if ( j != i) {

                if (samedigits(a[j]) && samedigits(a[i])) {
                if (a[j] > a[i]) {
                int tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
                continue;
                }
                }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
                int tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
                continue;

                } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
                continue;
                }else {
                if (a[j] > a[i]) {
                int tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
                continue;
                }
                }
                }
                }
                }
                cout << endl;
                for(int n : a) {
                cout << n << " ";
                }

                }


                And it returns :



                enter image description here



                I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.



                You can test this here :



                https://www.jdoodle.com/online-compiler-c++



                Regards!






                share|improve this answer


























                  0












                  0








                  0







                  Its not really coherent your definition of the problem to the samples you are providing, for instance you say :



                  Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
                  After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13


                  But according to your statements, should not 11 be the very first element?



                  Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :



                  #include <iostream>
                  #include <vector>

                  using namespace std;


                  bool samedigits(int x)
                  {
                  //if ( x < 10 ) return false;
                  int digit = x%10;
                  while(x>0)
                  {
                  if(x%10 != digit) return false;
                  x = x/10;
                  }
                  return true;
                  }

                  int main() {
                  int same = 0;
                  int same1 = 1;
                  int temp = 0;
                  int n = 0;
                  int digit = 0;
                  int digit1 = 0;

                  vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

                  for(int n : a) {
                  cout << n << " ";
                  }

                  for (int i=0; i <= a.size() -1; i++)
                  {

                  for (int j=0; j <= a.size() - 1 ; j++)
                  {
                  if ( j != i) {

                  if (samedigits(a[j]) && samedigits(a[i])) {
                  if (a[j] > a[i]) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;
                  }
                  }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;

                  } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
                  continue;
                  }else {
                  if (a[j] > a[i]) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;
                  }
                  }
                  }
                  }
                  }
                  cout << endl;
                  for(int n : a) {
                  cout << n << " ";
                  }

                  }


                  And it returns :



                  enter image description here



                  I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.



                  You can test this here :



                  https://www.jdoodle.com/online-compiler-c++



                  Regards!






                  share|improve this answer













                  Its not really coherent your definition of the problem to the samples you are providing, for instance you say :



                  Input array  : 12, 33, 1, 19, 44, 11, 27, 76, 13 
                  After sorting: 33, 1, 44, 11, 12, 19, 27, 76, 13


                  But according to your statements, should not 11 be the very first element?



                  Please let me know if I am missing something. In any case ( and as you have not mentioned the language either ) I made this in C++ and its working ( insofar as I could understand what you were aiming for ) :



                  #include <iostream>
                  #include <vector>

                  using namespace std;


                  bool samedigits(int x)
                  {
                  //if ( x < 10 ) return false;
                  int digit = x%10;
                  while(x>0)
                  {
                  if(x%10 != digit) return false;
                  x = x/10;
                  }
                  return true;
                  }

                  int main() {
                  int same = 0;
                  int same1 = 1;
                  int temp = 0;
                  int n = 0;
                  int digit = 0;
                  int digit1 = 0;

                  vector <int>a = { 1,22,43,444,51,16,7,8888,90,11, 99};

                  for(int n : a) {
                  cout << n << " ";
                  }

                  for (int i=0; i <= a.size() -1; i++)
                  {

                  for (int j=0; j <= a.size() - 1 ; j++)
                  {
                  if ( j != i) {

                  if (samedigits(a[j]) && samedigits(a[i])) {
                  if (a[j] > a[i]) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;
                  }
                  }else if (! samedigits(a[j]) && samedigits(a[i]) ) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;

                  } else if ( samedigits(a[j]) && ! samedigits(a[i]) ){
                  continue;
                  }else {
                  if (a[j] > a[i]) {
                  int tmp = a[j];
                  a[j] = a[i];
                  a[i] = tmp;
                  continue;
                  }
                  }
                  }
                  }
                  }
                  cout << endl;
                  for(int n : a) {
                  cout << n << " ";
                  }

                  }


                  And it returns :



                  enter image description here



                  I neither get if you were considering a single digit number as being an "elements with the same digits". Hence, I commented a line that can fit well with that view or not. Feel free to comment.



                  You can test this here :



                  https://www.jdoodle.com/online-compiler-c++



                  Regards!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 17:52









                  Matias BarriosMatias Barrios

                  1,578418




                  1,578418






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450120%2fdividing-array-elements-on-digits-problem-with-crashing%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini