Comparing each element of the array
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
add a comment |
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
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 output0, 2.
– Bo R
Nov 10 '18 at 23:32
add a comment |
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
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
c++ algorithm
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 output0, 2.
– Bo R
Nov 10 '18 at 23:32
add a comment |
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 output0, 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
add a comment |
2 Answers
2
active
oldest
votes
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;
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
add a comment |
#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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
#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;
add a comment |
#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;
add a comment |
#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;
#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;
answered Nov 12 '18 at 10:15
glashuntiglashunti
2015
2015
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 output0, 2.– Bo R
Nov 10 '18 at 23:32