Writing a better computation function in C++?











up vote
0
down vote

favorite












I am beginner in C++ (Data-structure algorithm) and I need some help to write a better computation function in my int computemodes method. Is there way to write a better function that sorts the item count pair list in descending order by count. For example the item count pair list becomes,
{2,3},{4,3},{5,2}, {3,2}, {1,2}. Thanks for the help.



Right now it display as: 16 10 11 10 15 15 10 14 16 14 14 13 15 14 13 11 10 11 11 11 14 14 15 10 .




here is my code




    /*
the mode of a set of things is that thing that appears the greater number of times in the set
a set may have several modes
*/
int computemodes(int source, int size, int modes, int& msize)


{
/*
1. fill the modes array with zeroes
*/
fill(modes, size, 0);

/*
2. store the number of times each source element appears in the modes array.
if an element appears more than once in the source array then its counts appears
more than once the modes array.
source and modes form a parallel array structure
*/
for(int i = 0; i < size; i++)
modes[i] = count(source, size, source[i]);



/*
3. calculate the largest number in the modes array. this number is the number of
times the mode or modes appears in the source array
*/
int modevalue = findLargest(modes, size);

/*
4. assign -1 to the mode array elements that are less than the mode value
now only mode values in the modes array are not equal to -1.
the corresponding elements in the source array are the modes.
*/
for(int i = 0; i < size; i++)
if(modes[i] != modevalue) modes[i] = -1;





/*
5. we use the modes array to identify the source elements that are modes:
any element in the modes array that is not -1 corresponds to a mode in the
source array. if the mode is 1 then every source element is a mode
and no element in the modes array is -1; if the mode is greater than 1 then
a. many modes array entries are -1
b. the number of times a mode appears in the source equals its corresponding modes value
c. the number of modes array entries that are not -1 are the number of times the modes
appear in the source array

the following nested for loop transforms the modes array into an array in which
the first appearance of a mode in the source corresponds to a modes array entry
that is not -1 and subsequent appearances of this mode in the source correspond to
modes array entries that are -1.
*/
for(int i = 0; i < size; i++)
if(modes[i] != -1) //first appearance of the mode in the source
for(int j = i + 1; j < size; j++)
if(source[i] == source[j]) modes[j] = -1;
//subsequent appearances
/*
at this point the usage of the modes array changes.
heretofore, an entry that is not -1 in the modes array is the number of times
a mode appears in the source array. now an entry in the modes array is a mode.
the loop adds modes from the source array to the modes array.
msize serves 2 purposes:
a. it is number of modes copied so far.
b. it is the next free modes array position.
*/
msize = 0;
for (int i = 0; i < size; i++)
if (modes[i] != -1) //first occurrence of a mode in the source
{
modes[msize] = source[i];
msize++;
}
return modevalue;
}









share|improve this question







New contributor




Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I am beginner in C++ (Data-structure algorithm) and I need some help to write a better computation function in my int computemodes method. Is there way to write a better function that sorts the item count pair list in descending order by count. For example the item count pair list becomes,
    {2,3},{4,3},{5,2}, {3,2}, {1,2}. Thanks for the help.



    Right now it display as: 16 10 11 10 15 15 10 14 16 14 14 13 15 14 13 11 10 11 11 11 14 14 15 10 .




    here is my code




        /*
    the mode of a set of things is that thing that appears the greater number of times in the set
    a set may have several modes
    */
    int computemodes(int source, int size, int modes, int& msize)


    {
    /*
    1. fill the modes array with zeroes
    */
    fill(modes, size, 0);

    /*
    2. store the number of times each source element appears in the modes array.
    if an element appears more than once in the source array then its counts appears
    more than once the modes array.
    source and modes form a parallel array structure
    */
    for(int i = 0; i < size; i++)
    modes[i] = count(source, size, source[i]);



    /*
    3. calculate the largest number in the modes array. this number is the number of
    times the mode or modes appears in the source array
    */
    int modevalue = findLargest(modes, size);

    /*
    4. assign -1 to the mode array elements that are less than the mode value
    now only mode values in the modes array are not equal to -1.
    the corresponding elements in the source array are the modes.
    */
    for(int i = 0; i < size; i++)
    if(modes[i] != modevalue) modes[i] = -1;





    /*
    5. we use the modes array to identify the source elements that are modes:
    any element in the modes array that is not -1 corresponds to a mode in the
    source array. if the mode is 1 then every source element is a mode
    and no element in the modes array is -1; if the mode is greater than 1 then
    a. many modes array entries are -1
    b. the number of times a mode appears in the source equals its corresponding modes value
    c. the number of modes array entries that are not -1 are the number of times the modes
    appear in the source array

    the following nested for loop transforms the modes array into an array in which
    the first appearance of a mode in the source corresponds to a modes array entry
    that is not -1 and subsequent appearances of this mode in the source correspond to
    modes array entries that are -1.
    */
    for(int i = 0; i < size; i++)
    if(modes[i] != -1) //first appearance of the mode in the source
    for(int j = i + 1; j < size; j++)
    if(source[i] == source[j]) modes[j] = -1;
    //subsequent appearances
    /*
    at this point the usage of the modes array changes.
    heretofore, an entry that is not -1 in the modes array is the number of times
    a mode appears in the source array. now an entry in the modes array is a mode.
    the loop adds modes from the source array to the modes array.
    msize serves 2 purposes:
    a. it is number of modes copied so far.
    b. it is the next free modes array position.
    */
    msize = 0;
    for (int i = 0; i < size; i++)
    if (modes[i] != -1) //first occurrence of a mode in the source
    {
    modes[msize] = source[i];
    msize++;
    }
    return modevalue;
    }









    share|improve this question







    New contributor




    Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am beginner in C++ (Data-structure algorithm) and I need some help to write a better computation function in my int computemodes method. Is there way to write a better function that sorts the item count pair list in descending order by count. For example the item count pair list becomes,
      {2,3},{4,3},{5,2}, {3,2}, {1,2}. Thanks for the help.



      Right now it display as: 16 10 11 10 15 15 10 14 16 14 14 13 15 14 13 11 10 11 11 11 14 14 15 10 .




      here is my code




          /*
      the mode of a set of things is that thing that appears the greater number of times in the set
      a set may have several modes
      */
      int computemodes(int source, int size, int modes, int& msize)


      {
      /*
      1. fill the modes array with zeroes
      */
      fill(modes, size, 0);

      /*
      2. store the number of times each source element appears in the modes array.
      if an element appears more than once in the source array then its counts appears
      more than once the modes array.
      source and modes form a parallel array structure
      */
      for(int i = 0; i < size; i++)
      modes[i] = count(source, size, source[i]);



      /*
      3. calculate the largest number in the modes array. this number is the number of
      times the mode or modes appears in the source array
      */
      int modevalue = findLargest(modes, size);

      /*
      4. assign -1 to the mode array elements that are less than the mode value
      now only mode values in the modes array are not equal to -1.
      the corresponding elements in the source array are the modes.
      */
      for(int i = 0; i < size; i++)
      if(modes[i] != modevalue) modes[i] = -1;





      /*
      5. we use the modes array to identify the source elements that are modes:
      any element in the modes array that is not -1 corresponds to a mode in the
      source array. if the mode is 1 then every source element is a mode
      and no element in the modes array is -1; if the mode is greater than 1 then
      a. many modes array entries are -1
      b. the number of times a mode appears in the source equals its corresponding modes value
      c. the number of modes array entries that are not -1 are the number of times the modes
      appear in the source array

      the following nested for loop transforms the modes array into an array in which
      the first appearance of a mode in the source corresponds to a modes array entry
      that is not -1 and subsequent appearances of this mode in the source correspond to
      modes array entries that are -1.
      */
      for(int i = 0; i < size; i++)
      if(modes[i] != -1) //first appearance of the mode in the source
      for(int j = i + 1; j < size; j++)
      if(source[i] == source[j]) modes[j] = -1;
      //subsequent appearances
      /*
      at this point the usage of the modes array changes.
      heretofore, an entry that is not -1 in the modes array is the number of times
      a mode appears in the source array. now an entry in the modes array is a mode.
      the loop adds modes from the source array to the modes array.
      msize serves 2 purposes:
      a. it is number of modes copied so far.
      b. it is the next free modes array position.
      */
      msize = 0;
      for (int i = 0; i < size; i++)
      if (modes[i] != -1) //first occurrence of a mode in the source
      {
      modes[msize] = source[i];
      msize++;
      }
      return modevalue;
      }









      share|improve this question







      New contributor




      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am beginner in C++ (Data-structure algorithm) and I need some help to write a better computation function in my int computemodes method. Is there way to write a better function that sorts the item count pair list in descending order by count. For example the item count pair list becomes,
      {2,3},{4,3},{5,2}, {3,2}, {1,2}. Thanks for the help.



      Right now it display as: 16 10 11 10 15 15 10 14 16 14 14 13 15 14 13 11 10 11 11 11 14 14 15 10 .




      here is my code




          /*
      the mode of a set of things is that thing that appears the greater number of times in the set
      a set may have several modes
      */
      int computemodes(int source, int size, int modes, int& msize)


      {
      /*
      1. fill the modes array with zeroes
      */
      fill(modes, size, 0);

      /*
      2. store the number of times each source element appears in the modes array.
      if an element appears more than once in the source array then its counts appears
      more than once the modes array.
      source and modes form a parallel array structure
      */
      for(int i = 0; i < size; i++)
      modes[i] = count(source, size, source[i]);



      /*
      3. calculate the largest number in the modes array. this number is the number of
      times the mode or modes appears in the source array
      */
      int modevalue = findLargest(modes, size);

      /*
      4. assign -1 to the mode array elements that are less than the mode value
      now only mode values in the modes array are not equal to -1.
      the corresponding elements in the source array are the modes.
      */
      for(int i = 0; i < size; i++)
      if(modes[i] != modevalue) modes[i] = -1;





      /*
      5. we use the modes array to identify the source elements that are modes:
      any element in the modes array that is not -1 corresponds to a mode in the
      source array. if the mode is 1 then every source element is a mode
      and no element in the modes array is -1; if the mode is greater than 1 then
      a. many modes array entries are -1
      b. the number of times a mode appears in the source equals its corresponding modes value
      c. the number of modes array entries that are not -1 are the number of times the modes
      appear in the source array

      the following nested for loop transforms the modes array into an array in which
      the first appearance of a mode in the source corresponds to a modes array entry
      that is not -1 and subsequent appearances of this mode in the source correspond to
      modes array entries that are -1.
      */
      for(int i = 0; i < size; i++)
      if(modes[i] != -1) //first appearance of the mode in the source
      for(int j = i + 1; j < size; j++)
      if(source[i] == source[j]) modes[j] = -1;
      //subsequent appearances
      /*
      at this point the usage of the modes array changes.
      heretofore, an entry that is not -1 in the modes array is the number of times
      a mode appears in the source array. now an entry in the modes array is a mode.
      the loop adds modes from the source array to the modes array.
      msize serves 2 purposes:
      a. it is number of modes copied so far.
      b. it is the next free modes array position.
      */
      msize = 0;
      for (int i = 0; i < size; i++)
      if (modes[i] != -1) //first occurrence of a mode in the source
      {
      modes[msize] = source[i];
      msize++;
      }
      return modevalue;
      }






      c++






      share|improve this question







      New contributor




      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 17 mins ago









      Mike

      1




      1




      New contributor




      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          Mike is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209408%2fwriting-a-better-computation-function-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Mike is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Mike is a new contributor. Be nice, and check out our Code of Conduct.













          Mike is a new contributor. Be nice, and check out our Code of Conduct.












          Mike is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fcodereview.stackexchange.com%2fquestions%2f209408%2fwriting-a-better-computation-function-in-c%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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'