Comparing each element of the array










-3















I'm trying to answer a question of the algorithm from using c++ language. The question is compare each element of the two arrays and give a score. Basically compare the first element of the first array with the first element of the second array and give some score. If the first element of the first array is bigger than the first element of the second array, the first array receive one score. In the end return output with the sum of the score of this two arrays.



I did this but unfortunately this code aren't giving me which I expect answer.



#include <iostream>

int array_a[3] = 6, 4, 6;
int array_b[3] = 5, 4, 10;
int array_output[2] = ;


int main()

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int z = 0; z < 2; z++) //z is for array_output[2]

if (array_a[i] > array_b[j])

array_output[z]++; //if int array_a[0] is bigger than int array_b[0] the first element of the output[0] receive +1

else if (array_a[i] == array_b[j])

array_output[z] = 0;//if the int array_a[1] and int array_b[1] are equal anyone receive score

else if (array_a[i] < array_b[j])

array_output[z]++; //if int array_a[2] is less than int array_b[2] the second element of the array_output receive +1

else







std::cout << "" << array_output[0] << " , " << array_output[1] << "";
std::cout << std::endl;
return 0;



inputs int array_a[3] = 6, 4, ,6 and int array_b[3] = 5, 4, ,10



I expect the output array_output[2] = 1,1.



With this code the are returning array_output[2] = 4,4










share|improve this question
























  • Your problem is less than clear. Please provide an example of what result you expect to see.

    – Henning Koehler
    Nov 10 '18 at 23:20











  • Also, you don't need three nested loops to compare the arrays. You can do it in one.

    – sma
    Nov 10 '18 at 23:26











  • Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

    – Bo R
    Nov 10 '18 at 23:32















-3















I'm trying to answer a question of the algorithm from using c++ language. The question is compare each element of the two arrays and give a score. Basically compare the first element of the first array with the first element of the second array and give some score. If the first element of the first array is bigger than the first element of the second array, the first array receive one score. In the end return output with the sum of the score of this two arrays.



I did this but unfortunately this code aren't giving me which I expect answer.



#include <iostream>

int array_a[3] = 6, 4, 6;
int array_b[3] = 5, 4, 10;
int array_output[2] = ;


int main()

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int z = 0; z < 2; z++) //z is for array_output[2]

if (array_a[i] > array_b[j])

array_output[z]++; //if int array_a[0] is bigger than int array_b[0] the first element of the output[0] receive +1

else if (array_a[i] == array_b[j])

array_output[z] = 0;//if the int array_a[1] and int array_b[1] are equal anyone receive score

else if (array_a[i] < array_b[j])

array_output[z]++; //if int array_a[2] is less than int array_b[2] the second element of the array_output receive +1

else







std::cout << "" << array_output[0] << " , " << array_output[1] << "";
std::cout << std::endl;
return 0;



inputs int array_a[3] = 6, 4, ,6 and int array_b[3] = 5, 4, ,10



I expect the output array_output[2] = 1,1.



With this code the are returning array_output[2] = 4,4










share|improve this question
























  • Your problem is less than clear. Please provide an example of what result you expect to see.

    – Henning Koehler
    Nov 10 '18 at 23:20











  • Also, you don't need three nested loops to compare the arrays. You can do it in one.

    – sma
    Nov 10 '18 at 23:26











  • Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

    – Bo R
    Nov 10 '18 at 23:32













-3












-3








-3








I'm trying to answer a question of the algorithm from using c++ language. The question is compare each element of the two arrays and give a score. Basically compare the first element of the first array with the first element of the second array and give some score. If the first element of the first array is bigger than the first element of the second array, the first array receive one score. In the end return output with the sum of the score of this two arrays.



I did this but unfortunately this code aren't giving me which I expect answer.



#include <iostream>

int array_a[3] = 6, 4, 6;
int array_b[3] = 5, 4, 10;
int array_output[2] = ;


int main()

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int z = 0; z < 2; z++) //z is for array_output[2]

if (array_a[i] > array_b[j])

array_output[z]++; //if int array_a[0] is bigger than int array_b[0] the first element of the output[0] receive +1

else if (array_a[i] == array_b[j])

array_output[z] = 0;//if the int array_a[1] and int array_b[1] are equal anyone receive score

else if (array_a[i] < array_b[j])

array_output[z]++; //if int array_a[2] is less than int array_b[2] the second element of the array_output receive +1

else







std::cout << "" << array_output[0] << " , " << array_output[1] << "";
std::cout << std::endl;
return 0;



inputs int array_a[3] = 6, 4, ,6 and int array_b[3] = 5, 4, ,10



I expect the output array_output[2] = 1,1.



With this code the are returning array_output[2] = 4,4










share|improve this question
















I'm trying to answer a question of the algorithm from using c++ language. The question is compare each element of the two arrays and give a score. Basically compare the first element of the first array with the first element of the second array and give some score. If the first element of the first array is bigger than the first element of the second array, the first array receive one score. In the end return output with the sum of the score of this two arrays.



I did this but unfortunately this code aren't giving me which I expect answer.



#include <iostream>

int array_a[3] = 6, 4, 6;
int array_b[3] = 5, 4, 10;
int array_output[2] = ;


int main()

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int z = 0; z < 2; z++) //z is for array_output[2]

if (array_a[i] > array_b[j])

array_output[z]++; //if int array_a[0] is bigger than int array_b[0] the first element of the output[0] receive +1

else if (array_a[i] == array_b[j])

array_output[z] = 0;//if the int array_a[1] and int array_b[1] are equal anyone receive score

else if (array_a[i] < array_b[j])

array_output[z]++; //if int array_a[2] is less than int array_b[2] the second element of the array_output receive +1

else







std::cout << "" << array_output[0] << " , " << array_output[1] << "";
std::cout << std::endl;
return 0;



inputs int array_a[3] = 6, 4, ,6 and int array_b[3] = 5, 4, ,10



I expect the output array_output[2] = 1,1.



With this code the are returning array_output[2] = 4,4







c++ algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 8:36







glashunti

















asked Nov 10 '18 at 23:16









glashuntiglashunti

2015




2015












  • Your problem is less than clear. Please provide an example of what result you expect to see.

    – Henning Koehler
    Nov 10 '18 at 23:20











  • Also, you don't need three nested loops to compare the arrays. You can do it in one.

    – sma
    Nov 10 '18 at 23:26











  • Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

    – Bo R
    Nov 10 '18 at 23:32

















  • Your problem is less than clear. Please provide an example of what result you expect to see.

    – Henning Koehler
    Nov 10 '18 at 23:20











  • Also, you don't need three nested loops to compare the arrays. You can do it in one.

    – sma
    Nov 10 '18 at 23:26











  • Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

    – Bo R
    Nov 10 '18 at 23:32
















Your problem is less than clear. Please provide an example of what result you expect to see.

– Henning Koehler
Nov 10 '18 at 23:20





Your problem is less than clear. Please provide an example of what result you expect to see.

– Henning Koehler
Nov 10 '18 at 23:20













Also, you don't need three nested loops to compare the arrays. You can do it in one.

– sma
Nov 10 '18 at 23:26





Also, you don't need three nested loops to compare the arrays. You can do it in one.

– sma
Nov 10 '18 at 23:26













Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

– Bo R
Nov 10 '18 at 23:32





Just guessing but is this something you are expecting: #include <iostream> int array_a[3] = 4, 5, 6; int array_b[3] = 4, 6, 10; int array_output[2] = ; int main() for (int i = 0; i < 3; i++) if (array_a[i] > array_b[i]) array_output[0]++; else if (array_a[i] < array_b[i]) array_output[1]++; else // equal, no change in score std::cout << "" << array_output[0] << " , " << array_output[1] << "n"; return 0; ? It will output 0, 2.

– Bo R
Nov 10 '18 at 23:32












2 Answers
2






active

oldest

votes


















0














If you want to compare a.first with b.first, a.second with b.second, and so on, then a single loop should be enough; and if you are just interested in the sums, even the result array is superfluous:



int main() 

int array_a[3] = 4, 5, 6;
int array_b[3] = 4, 6, 10;
int sum_a=0, sum_b=0;

for (int i = 0; i < 3; i++)
if (array_a[i] > array_b[i])
sum_a++;
else if (array_b[i] > array_a[i])
sum_b++;


std::cout << "sum a:" << sum_a << "; sum b:" << sum_b << std::endl;







share|improve this answer























  • Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

    – vivek_23
    Nov 11 '18 at 5:29


















0














#include <iostream> 

void compare(int array_a, int array_b); //Function prototype

int main()

int array_al[3] = 6, 4, 6 ; // array inputs
int array_bo[3] = 5, 4, 10 ;

compare(array_al, array_bo); //call the function compare

return 0;


void compare(int array_a, int array_b)

int al = 0, bo = 0;

for (int j = 0; j < 3; j++) // read and compare the values until reach the numbers of the elements

if (array_a[j] > array_b[j])

al++;

else if (array_a[j] < array_b[j])

bo++;


std::cout << "" << al << " , " << bo << ""; // print out the sum of the values
std::cout << std::endl;






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%2f53244355%2fcomparing-each-element-of-the-array%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














    If you want to compare a.first with b.first, a.second with b.second, and so on, then a single loop should be enough; and if you are just interested in the sums, even the result array is superfluous:



    int main() 

    int array_a[3] = 4, 5, 6;
    int array_b[3] = 4, 6, 10;
    int sum_a=0, sum_b=0;

    for (int i = 0; i < 3; i++)
    if (array_a[i] > array_b[i])
    sum_a++;
    else if (array_b[i] > array_a[i])
    sum_b++;


    std::cout << "sum a:" << sum_a << "; sum b:" << sum_b << std::endl;







    share|improve this answer























    • Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

      – vivek_23
      Nov 11 '18 at 5:29















    0














    If you want to compare a.first with b.first, a.second with b.second, and so on, then a single loop should be enough; and if you are just interested in the sums, even the result array is superfluous:



    int main() 

    int array_a[3] = 4, 5, 6;
    int array_b[3] = 4, 6, 10;
    int sum_a=0, sum_b=0;

    for (int i = 0; i < 3; i++)
    if (array_a[i] > array_b[i])
    sum_a++;
    else if (array_b[i] > array_a[i])
    sum_b++;


    std::cout << "sum a:" << sum_a << "; sum b:" << sum_b << std::endl;







    share|improve this answer























    • Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

      – vivek_23
      Nov 11 '18 at 5:29













    0












    0








    0







    If you want to compare a.first with b.first, a.second with b.second, and so on, then a single loop should be enough; and if you are just interested in the sums, even the result array is superfluous:



    int main() 

    int array_a[3] = 4, 5, 6;
    int array_b[3] = 4, 6, 10;
    int sum_a=0, sum_b=0;

    for (int i = 0; i < 3; i++)
    if (array_a[i] > array_b[i])
    sum_a++;
    else if (array_b[i] > array_a[i])
    sum_b++;


    std::cout << "sum a:" << sum_a << "; sum b:" << sum_b << std::endl;







    share|improve this answer













    If you want to compare a.first with b.first, a.second with b.second, and so on, then a single loop should be enough; and if you are just interested in the sums, even the result array is superfluous:



    int main() 

    int array_a[3] = 4, 5, 6;
    int array_b[3] = 4, 6, 10;
    int sum_a=0, sum_b=0;

    for (int i = 0; i < 3; i++)
    if (array_a[i] > array_b[i])
    sum_a++;
    else if (array_b[i] > array_a[i])
    sum_b++;


    std::cout << "sum a:" << sum_a << "; sum b:" << sum_b << std::endl;








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 '18 at 23:29









    Stephan LechnerStephan Lechner

    27.4k22140




    27.4k22140












    • Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

      – vivek_23
      Nov 11 '18 at 5:29

















    • Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

      – vivek_23
      Nov 11 '18 at 5:29
















    Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

    – vivek_23
    Nov 11 '18 at 5:29





    Although the problem statement is not quite clear, it seems from OP's code that he wants to compare every element of first array with every element of second array.

    – vivek_23
    Nov 11 '18 at 5:29













    0














    #include <iostream> 

    void compare(int array_a, int array_b); //Function prototype

    int main()

    int array_al[3] = 6, 4, 6 ; // array inputs
    int array_bo[3] = 5, 4, 10 ;

    compare(array_al, array_bo); //call the function compare

    return 0;


    void compare(int array_a, int array_b)

    int al = 0, bo = 0;

    for (int j = 0; j < 3; j++) // read and compare the values until reach the numbers of the elements

    if (array_a[j] > array_b[j])

    al++;

    else if (array_a[j] < array_b[j])

    bo++;


    std::cout << "" << al << " , " << bo << ""; // print out the sum of the values
    std::cout << std::endl;






    share|improve this answer



























      0














      #include <iostream> 

      void compare(int array_a, int array_b); //Function prototype

      int main()

      int array_al[3] = 6, 4, 6 ; // array inputs
      int array_bo[3] = 5, 4, 10 ;

      compare(array_al, array_bo); //call the function compare

      return 0;


      void compare(int array_a, int array_b)

      int al = 0, bo = 0;

      for (int j = 0; j < 3; j++) // read and compare the values until reach the numbers of the elements

      if (array_a[j] > array_b[j])

      al++;

      else if (array_a[j] < array_b[j])

      bo++;


      std::cout << "" << al << " , " << bo << ""; // print out the sum of the values
      std::cout << std::endl;






      share|improve this answer

























        0












        0








        0







        #include <iostream> 

        void compare(int array_a, int array_b); //Function prototype

        int main()

        int array_al[3] = 6, 4, 6 ; // array inputs
        int array_bo[3] = 5, 4, 10 ;

        compare(array_al, array_bo); //call the function compare

        return 0;


        void compare(int array_a, int array_b)

        int al = 0, bo = 0;

        for (int j = 0; j < 3; j++) // read and compare the values until reach the numbers of the elements

        if (array_a[j] > array_b[j])

        al++;

        else if (array_a[j] < array_b[j])

        bo++;


        std::cout << "" << al << " , " << bo << ""; // print out the sum of the values
        std::cout << std::endl;






        share|improve this answer













        #include <iostream> 

        void compare(int array_a, int array_b); //Function prototype

        int main()

        int array_al[3] = 6, 4, 6 ; // array inputs
        int array_bo[3] = 5, 4, 10 ;

        compare(array_al, array_bo); //call the function compare

        return 0;


        void compare(int array_a, int array_b)

        int al = 0, bo = 0;

        for (int j = 0; j < 3; j++) // read and compare the values until reach the numbers of the elements

        if (array_a[j] > array_b[j])

        al++;

        else if (array_a[j] < array_b[j])

        bo++;


        std::cout << "" << al << " , " << bo << ""; // print out the sum of the values
        std::cout << std::endl;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 10:15









        glashuntiglashunti

        2015




        2015



























            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%2f53244355%2fcomparing-each-element-of-the-array%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

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ