Need to pass 2D array to function and find sum of row c++

So I need to create a function to find the sum of rows in my 2D array. Array is fixed, matrix[5][5], and user inputs 25 integers.

I know how to find the sum of my rows using the following code:

//for sake of ease lets say user inputs numbers 1-25
for (r = 0; r < 5; r++)
{
  for (c = 0; c < 5; c++)
  {
     sum = sum + matrix[r][c]
  }
  cout << "\n" << sum;
  sum = 0;
}


//the code above will display the sum of each row as follows:
15
40
65
90
115

I want to display the totals for each row as

Row 1:
Sum =

Row 2:
Sum =

etc...

How do I pass the array to a function in order to find the sum of each row and how do I separate the individual sum of rows to display like I want?

I have read a chapter on passing multidimensional arrays to functions like 4 times over in a c++ beginners book, I have read and looked at many different forums online and maybe it is because I have been starring at it for too long I am not seeing the answer but I have given myself a headache. I really just want to understand how to pass it. I have tried to modify the passing of an array to a function to find the sum of all the integers in the array but I could not get it to work for what I needed.

ETA(10/7/2017 1535 PCT): So I am trying the following to try and pass my 2D array to a function and calculate the sum...

void total(int matrix[][5], int n, int m)
    {     // I am getting an error here though that states "expected a ';' "
        for (r = 0; r < n; r++)
        {
            int sum = 0;
            for (c = 0; c < m; c++)
                sum += matrix[r][c];

        }
        cout << "Row " << r << " = " << sum << endl;
    }

Is this even how you create a function with a 2D array?

ETA (10/7/2017 2100 PCT)

So I think I figured out how to pass the array, but I cannot seem to get it to do the proper math, meaning this does not sum up the rows....

#include "stdafx.h"
#include "iostream"
using namespace std;

int total( const int [][5], int, int);

int main()
{
    int c, r, matrix[5][5];

    cout << "Please input any 25 numbers you'd like, seperated by a space, then press enter:" << endl;

    for (r = 0; r < 5; r++)
    {
        for (c = 0; c < 5; c++)
        {
            cin >> matrix[r][c];
        }
    }
    getchar();

    cout << endl << "Matrix: " << endl;
    for (r = 0; r < 5; r++)
    {
        cout << endl;
        for (c = 0; c < 5; c++)
        {
            cout << matrix[r][c] << "\t";
        }
        cout << endl;
    }

    cout << "Please press the enter key to get the sums of each row << endl;
    getchar();

    cout << "Sum = " << total << endl; //this displays "Sum = 013513F2"
    system("PAUSE");
}


int total(const int matrix[][5], int R, int C)
{
    int sum = 0;

    for (int r = 0; r < R; r++)
    {
        for (int c = 0; c < C; c++)
        {
            sum = sum + matrix[r][c];
        }
    }
    return sum;
}

Upvotes: 0

Views: 4254

Answers (2)

Just to help the future readers this is what I came up with. Took a lot of research and a million different trial and errors but here it is:

int math(int a[5]) //The function the array has been passed to
{
    //Declaring the variables in the function 
    int sum = 0;
    double average = 0;
    int min = 0;
    int max = 0;

    min = a[0]; //setting the minimum value to compare to

    for (int C = 0; C < 5; C++) //Creates the loop to go through the row elements 
    {
        sum = sum + a[C];   // calculates the sum of each row
        if (a[C] < min) min = a[C];  //assigns the element of lowest value from row
        if (a[C] > max) max = a[C];  //assigns the element of highest value from row
    }
    average = sum / 5;  //calculates the average of each row

        cout << "Sum = " << sum << endl;     //Outputs sum
        cout << "Average = " << average << endl;   //Outputs average
        cout << "Min = " << min << endl;  //Outputs min
        cout << "Max = " << max << endl;  //Oututs max
        cout << endl;
        return 0; //return value for function
}

Down the line that calls the function and displays the output I was looking for:

for (r = 0; r < 5; r++) //sets up row loop for display
{
    cout << "Row " << r+1 << ":" << endl;
    math(matrix[r]); //displays calculations done in math function
    cout << endl;
}

Hope this helps someone down the road...

Upvotes: 0

Carl
Carl

Reputation: 2067

Passing an array of any dimension can be done by using the syntax: type (&name)[numElements] by reference. Or by pointer you would replace the & with a *. Below is a basic example that compiles, which passes the array by reference to the pass2Darray function. Alternatively, you could simply use a regular array with size [5 * 5] to ensure that it's entirely contiguous. Since a 2D array is not natively something that exists in C++. And then, since you're working with matrices, you can access it in column major by [row * i + col] or in row major by [col * j + row].

#include <iostream>

// Reference to multiArray
// int (&someName)[num][num]

// Pointer to multiArray
// int (*someName)[num][num]

void pass2Darray(int (&passed)[1][1]) {
    std::cout << passed[0][0];
}

int main() {

    int arr[1][1] = { {1} };
    pass2Darray(arr);

    return 0;
}

Upvotes: 1

Related Questions