algorithm - c++ comparing sum of arrays -
i have 2 arrays each 5 integers, how can compare both of array array larger array b, following code return 0:
#include <iostream> using namespace std; int main() { const int amax = 5, bmax = 6; int i; bool c1 = true, c2 = false; int a[amax] = { 1, 2, 3, 4, 5 }; int b[bmax] = { 6, 7,8, 9, 1}; (i = 0; < bmax; i++) if (b[i] == a[i]) cout << c1 << endl; else cout << c2 << endl; return 0; }
what missing here?
update:
#include <iostream> using namespace std; int main(){ int a[] = {6,7,29}; int b[] = {3,2,11}; int acc1=0; int acc2 = 0; (int i=0;i<3;i++){ acc1+=a[i]; } for(int j=0;j<3;j++){ acc2+=b[j]; } if(acc1 < acc2){ printf("array b greater array b "); } else{ printf("array b greater array a"); } return 0; }
you can use built-in algorithm std::equal
compare 2 arrays. example:
#include <iostream> #include <algorithm> using namespace std; #define countof(x) sizeof(x)/sizeof(x[0]) int main() { int a[] = { 2, 4, 1, 5, 9 }; int b[] = { 9, 12, 32, 43, 23}; int c[] = { 2, 4, 1, 5, 9 }; cout << equal(a, + countof(a), b) << endl; cout << equal(a, + countof(a), c) << endl; }
output: 0 1
Comments
Post a Comment