-3

I am a beginner in C++ and I faced the following problem: In my program I have a function to which I pass an array with fixed size as a parameter. I iterate through it and perform some operations. As a result I have 2 variables - br and a, which I want to return from the function. I create a pair and assign to it these values. However when I run the whole program the compiler throws an error - cannot convert std::pair<> to int. I would like to ask why this happens?

 #include <utility>
  using namespace std;
  pair <double,double> rez;
  //int main() ...
   double sumaf(int k[20])
{

    for(int i=0; i<20; i++)
    {
        if(k[i]>0 && k[i]%3==0)
        {
            a+=k[i];
            br++;
        }
    }

    rez.first=a;
    rez.second=br;
    return rez;

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
A.Petrov
  • 33
  • 1
  • 6

1 Answers1

4

You need to change the return type of sumaf().

Also, there is no need for a global variable (rez).

#include <utility>

using namespace std;

pair<double, double> sumaf(int k[20])
{
    double a = 0,
           br = 0;
    for(int i = 0; i < 20; i++)
    {
        if (k[i] > 0 && k[i] % 3 == 0)
        {
            a += k[i];
            br++;
        }
    }
    return make_pair(a, br);
}
Sid S
  • 6,037
  • 2
  • 18
  • 24