I was doing some practice questions in C++. I ran into an issue where I want to find the sum of elements of a 2d array. I can write a get sum method which returns the sum. But I was exploring if "operator<<" method could be overloaded to achieve the same result.
#include <iostream>
using namespace std;
int operator<<(const int arr[5][5])
{
int sum = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
sum += arr[i][j];
}
}
return sum;
}
int main()
{
int arr[5][5] = { {1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8},
{5,6,7,8,9} };
cout << &arr << endl;
}
I want to achieve the sum as in the std::cout method. Is this possible?