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}
c++ algorithm
add a comment |
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}
c++ algorithm
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
add a comment |
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}
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 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
add a comment |
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
add a comment |
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;
}
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
add a comment |
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;
}
add a comment |
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;
}
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
add a comment |
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;
}
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
add a comment |
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;
}
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 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
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 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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
#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 at 10:15
glashunti
205
205
add a comment |
add a comment |
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 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