Comparing each element of the array











up vote
-3
down vote

favorite












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 at 23:20










  • Also, you don't need three nested loops to compare the arrays. You can do it in one.
    – sma
    Nov 10 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 at 23:32















up vote
-3
down vote

favorite












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 at 23:20










  • Also, you don't need three nested loops to compare the arrays. You can do it in one.
    – sma
    Nov 10 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 at 23:32













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











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 at 8:36

























asked Nov 10 at 23:16









glashunti

205




205












  • Your problem is less than clear. Please provide an example of what result you expect to see.
    – Henning Koehler
    Nov 10 at 23:20










  • Also, you don't need three nested loops to compare the arrays. You can do it in one.
    – sma
    Nov 10 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 at 23:32


















  • Your problem is less than clear. Please provide an example of what result you expect to see.
    – Henning Koehler
    Nov 10 at 23:20










  • Also, you don't need three nested loops to compare the arrays. You can do it in one.
    – sma
    Nov 10 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 at 23:32
















Your problem is less than clear. Please provide an example of what result you expect to see.
– Henning Koehler
Nov 10 at 23:20




Your problem is less than clear. Please provide an example of what result you expect to see.
– Henning Koehler
Nov 10 at 23:20












Also, you don't need three nested loops to compare the arrays. You can do it in one.
– sma
Nov 10 at 23:26




Also, you don't need three nested loops to compare the arrays. You can do it in one.
– sma
Nov 10 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 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 at 23:32












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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 at 5:29


















up vote
0
down vote













#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',
    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








    up vote
    0
    down vote













    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 at 5:29















    up vote
    0
    down vote













    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 at 5:29













    up vote
    0
    down vote










    up vote
    0
    down vote









    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 at 23:29









    Stephan Lechner

    24.5k21738




    24.5k21738












    • 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 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 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 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 at 5:29












    up vote
    0
    down vote













    #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

























      up vote
      0
      down vote













      #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























        up vote
        0
        down vote










        up vote
        0
        down vote









        #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 at 10:15









        glashunti

        205




        205






























             

            draft saved


            draft discarded



















































             


            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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues