C ++比较数组的总和数组、总和

2023-09-11 07:26:59 作者:来生缘

我有每2阵列5整数,我怎么能比较两种阵列,使阵列A比阵列B大,下面是我的code在那里我得到的回报0:

 的#include<的iostream>
使用名字空间std;

诠释的main()
{
    const int的AMAX = 5,BMAX = 6;
    INT I;
    布尔C1 = TRUE,C2 = FALSE;
    INT A〔AMAX] = {1,2,3,4,5};
    INT B〔BMAX] = {6,7,8,9,1};

    对于(i = 0; I< BMAX;我++)
        如果(B [我] == A [1])
            COUT<< C1<< ENDL;
        其他
            COUT<< C2<< ENDL;
    返回0;
}
 

我在想什么吗?

更新:

 的#include<的iostream>
使用名字空间std;
诠释的main(){
  诠释一个[] = {6,7,29};
  INT B〔] = {3,2,11};
  INT ACC1 = 0;
  INT ACC2 = 0;
  的for(int i = 0;我3;;我++){
    ACC1 + = A [1];
}
  为(诠释J = 0; J&所述; 3; J ++){
    ACC2 + = B [J]。
}

如果(ACC1< ACC2){
  的printf(数组b大于数组b);
}

其他{
  的printf(数组b大于阵列A);
}

  返回0;
}
 

解决方案 C语言 05数组和字符串

如果你想比较哪些的金额的数组中的元素(即确定是否所有元素的总和 A 大于所有元素的 B )的总和,因为这个问题的标题似乎在暗示,这应该做些什么你想要的:

 的#include<的iostream>
#包括<算法>
#包括<数字>
#包括<了iomanip>

诠释的main()
{
    使用名字空间std;

    INT A [] = {2,4,1,5,9};
    INT B〔] = {9,12,32,43,23};

    INT SUMA =累加(开始(A),端(A),0);
    INT sumB =累加(开始(B),终端(B),0);

    COUT<< boolalpha<< (SUMA> sumB);

    返回0;
}
 

如果你不使用C ++ 11,这里是写在同等功能的程序C ++ 03:

 的#include<的iostream>
#包括<数字>

诠释的main()
{
    使用名字空间std;

    INT A [] = {2,4,1,5,9};
    INT B〔] = {9,12,32,43,23};

    INT SUMA =累加(A,A +的sizeof(A)/的sizeof(INT),0);
    INT sumB =累加(B,B +的sizeof(B)/的sizeof(INT),0);

    COUT<< ((SUMA> sumB)真:假);

    返回0;
}
 

i have 2 arrays each with 5 integers, how can i compare both of array so that array A is larger than array B, following are my code where i get 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};

    for (i = 0; i < BMAX; i++)
        if (B[i] == A[i])       
            cout << c1 << endl;
        else
            cout << c2 << endl;
    return 0;
}

What am i 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;
  for (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 is greater than Array B ");
}

else{
  printf("Array B greater than Array A");
}

  return 0;
}

解决方案

If what you want to compare are the sums of the elements in the array (i.e. determine whether the sum of all elements in A is greater than the sum of all elements in B), as the question's title seems to imply, this should do what you want:

#include <iostream>
#include <algorithm>
#include <numeric>
#include <iomanip>

int main()
{
    using namespace std;

    int A[] = { 2, 4, 1, 5, 9 };
    int B[] = { 9, 12, 32, 43, 23};

    int sumA = accumulate(begin(A), end(A), 0);
    int sumB = accumulate(begin(B), end(B), 0);

    cout << boolalpha << (sumA > sumB);

    return 0;
}

In case you are not using C++11, here is the equivalent program written in C++03:

#include <iostream>
#include <numeric>

int main()
{
    using namespace std;

    int A[] = { 2, 4, 1, 5, 9 };
    int B[] = { 9, 12, 32, 43, 23};

    int sumA = accumulate(A, A + sizeof(A) / sizeof(int), 0);
    int sumB = accumulate(B, B + sizeof(B) / sizeof(int), 0);

    cout << ((sumA > sumB) ? "true" : "false");

    return 0;
}