oowow
oowow

Reputation: 11

Sum of the lines using a two-dimensional array

I want to create a function that receives data[3][4] and obtain the sum of each row. The array that returns must be created through using (new) and returned (int*). But I dont know how can i use new and int*.

#include <iostream>

using namespace std;

int sum(int arr[][4], int size);

int main()
{
    int data[][4] = {
        {1, 2, 3, 4},
        {9, 2, 4, 6},
        {4, 2, 6, 8}
        };

    cout << "sum of row0: " << sum(data, 0) << endl;
    cout << "sum of row1: " << sum(data, 1) << endl;
    cout << "sum of row2: " << sum(data, 2) << endl;
    return 0;
}

int sum(int arr[][4], int size)
{

    int total = 0;

    for (int c = 0; c < 4; c++)
    {
        total += arr[size][c];
    }

    return total;
}

Upvotes: 1

Views: 419

Answers (4)

Tahir Shahzad
Tahir Shahzad

Reputation: 649

First of all, I think you are taking this question in wrong way. If we are sending single digit in return we don't need pointer array. As we can't return array from C++ function so we use pointer array to return array.

you are passing a two dimensional array to a function and then it should return an array of sum for each row. For example

int data[][3] = {
    {1,2,3},
    {4,5,6},
    {7,8,9}
}

Now calling function sum(data, numOfRows) should return an array of sum against each row like

result = [6, 15, 24]

Where at 0th index of result we have sum of 0th row. at 1st index of result we have sum of 1st row and on 2nd index of result we have sum of 2nd row.

If it is the case, now return type of your function will be pointer

int * sum(int arr[][3], int size){

}

We use size for one dimensional array. for two dimensional array we need rows and cols. Next You need to create array of total for each rows.

int * sum(int arr[][4], int rows, int cols){
    int* total = new int[size];
    for (int i = 0; i < size; i++){
        for (int j = 0; j < cols; j++){
            total[i] += arr[i][j];
        }
    }
    return total;
}

Now in main function you can utilize this result.

int rows = 3;
int cols = 3;
int* result = sum(data, rows, cols);
for(int i=0; i<rows; i++{
    cout << "sum of row "<<i<< ": " << result[i] << endl;

}

Further improvements can be done to make this program more dynamic to handle all kind of 2 dimensional arrays.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311010

What you need is the following.

#include <iostream>

const size_t N = 4;

int * sum( const int a[][N], size_t n )
{
    int *result = new int[n];

    for ( size_t i = 0; i < n; i++ )
    {
        result[i] = 0;
        for ( const int &item : a[i] ) result[i] += item;
    }

    return result;
}

int main() 
{
    int data[][N] = 
    {
        { 1, 2, 3, 4 },
        { 9, 2, 4, 6 },
        { 4, 2, 6, 8 }
    };

    const size_t M = sizeof( data ) / sizeof( *data );  

    auto result = sum( data, M );

    for ( size_t i = 0; i < M; i++ ) std::cout << i << ": " << result[i] << '\n';

    delete [] result;

    return 0;
}

The program output is

0: 10
1: 21
2: 20

That is in the function sum you need to allocate dynamically an array with the number of elements equal to the number of rows in the two-dimensional array passed to the function as an argument.

Do not forget to free it when it is not needed any more.

You could write a more generic function that is able to process two-dimensional arrays of various sizes. For example

#include <iostream>

template <size_t M, size_t N>
int * sum( const int ( &a )[M][N] )
{
    int *result = new int[M];

    for ( size_t i = 0; i < M; i++ )
    {
        result[i] = 0;
        for ( const int &item : a[i] ) result[i] += item;
    }

    return result;
}

int main() 
{
    const size_t N = 4;
    int data[][N] = 
    {
        { 1, 2, 3, 4 },
        { 9, 2, 4, 6 },
        { 4, 2, 6, 8 }
    };
    const size_t M = sizeof( data ) / sizeof( *data );

    auto result = sum( data );

    for ( size_t i = 0; i < M; i++ ) std::cout << i << ": " << result[i] << '\n';

    delete [] result;

    return 0;
}

The program output will be the same as shown above.

0: 10
1: 21
2: 20

Also it would be better to define the return type as long long int.

long long int * sum( const int a[][N], size_t n )
{
    long long int *result = new long long int[n];

    for ( size_t i = 0; i < n; i++ )
    {
        result[i] = 0;
        for ( const int &item : a[i] ) result[i] += item;
    }

    return result;
}

In this case the risk of overflow of calculated sums can be minimized.

Upvotes: 1

cigien
cigien

Reputation: 60228

You could write this function using the STL, like this:

int* sum(const int a[][4])
{
    int *result = new int[3];

    std::transform(a, a + 3, result, [](auto row) {
                     return std::accumulate(row, row + 4, 0);
                   }); 

    return result;
}

Of course, don't forget to delete the memory at the call site.

Upvotes: -1

Osama Ahmad
Osama Ahmad

Reputation: 2096

#include <iostream>

using namespace std;

int *sum(int arr[][4], int size);

int main()
{
    int data[][4] = {
         {1,2,3,4}
        ,{9,2,4,6}
        ,{4,2,6,8}
    };

    int* sum1 = sum(data, 0);
    int* sum2 = sum(data, 1);
    int* sum3 = sum(data, 2);

    cout << "sum of row0: " << *sum1 << endl;
    cout << "sum of row1: " << *sum2 << endl;
    cout << "sum of row2: " << *sum3 << endl;

    delete sum1;
    delete sum2;
    delete sum3;

    return 0;
}

int* sum(int arr[][4], int size)
{

    int* total = new int(0);

    for (int c = 0; c < 4; c++)
    {
        *total += arr[size][c];
    }

    return total;
}

If you want to return an array of the sums you can do something like this.

#include <iostream>

using namespace std;

int *sum(int arr[][4]);

int main()
{
    int data[][4] = {
         {1,2,3,4}
        ,{9,2,4,6}
        ,{4,2,6,8}
    };

    int* sumArray = sum(data);

    cout << "sum of row0: " << sumArray[0] << endl;
    cout << "sum of row1: " << sumArray[1] << endl;
    cout << "sum of row2: " << sumArray[2] << endl;

    // remember to free the allocated memory.
    delete[] sumArray;

    return 0;
}

int* sum(int arr[][4])
{

    int* total = new int[3]{ 0 };

    for (int i = 0; i < 3; i++)
        for (int c = 0; c < 4; c++)
        {
            total[i] += arr[i][c];
        }

    return total;
}

But I suggest you use std::vector insted and not using the keyword new.

Upvotes: 2

Related Questions