How do I pass a single row of a 2 dimensionsl aarray to a function?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;


// ------ Prototypes ------------------------------------------------------
double calcNums(double[]);

// ****** MAIN ************************************************************
int main()
{
    double nums[2][5] = {1,2,3,4,5,6,7,8,9,10};

    calcNums(nums[1][5]);
  return 0;

}

double calcNums(double nums[1][5])
{
    double calc = 0;

  //  calc = nums[1] +  nums[2] + nums[3] + nums[4]  + nums[5];


    return calc;
}

I know my syntac is all wonkalicious, but I have no clue how to code this. (barring sending the entire row each time.)

Recommended Answers

All 5 Replies

<deleted>

So I did this:

// ------ Prototypes ------------------------------------------------------
double calcNums(double []);

// ****** MAIN ************************************************************
int main()
{
    double nums[2][5] = {1,2,3,4,5,6,7,8,9,10};

    calcNums(&nums[1]);
  return 0;

}

double calcNums(double nums[5])
{
    double calc = 0;

  //  calc = nums[1] +  nums[2] + nums[3] + nums[4]  + nums[5];


    return calc;
}

And it tells me that I cannot convert, er this:

D:\empty\empty.cpp||In function 'int main()':|
D:\empty\empty.cpp|28|error: cannot convert 'double ()[5]' to 'double' for argument '1' to 'double calcNums(double*)'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

You don't need the adress of operator (&) in your call to the 1D function.

Just use the array name and index of the row. That is the address of, or pointer to, the row in question.

calcNums(nums[1]);

commented: Learned something new today :) +14

I got the same results when I tried it after I posted that reply.

Thanks guys, it worked like a charm. posting the code on the off chance someone wants a reference:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;


// ------ Prototypes ------------------------------------------------------
double calcNums(double *a);

// ****** MAIN ************************************************************
int main()
{
    double nums[2][5] = {1,2,3,4,5,6,7,8,9,10};

    cout << calcNums(nums[1]);
  return 0;

}

double calcNums(double nums[5])
{
    double calc = 0;

    calc = nums[1] +  nums[2] + nums[3] + nums[4]  + nums[5];


    return calc;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.