Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi guys: Can any one please tell me how to sum the array values in the function ?

Thanks.

Thanks.

```
#include <iostream>
using namespace test;
void calcTotal(int num[][]);
/* Display array column by column
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
*/
int main()
{
double numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
void calcTotal(int num[][])
{
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
}
system("pause");
return 0;
}
```

error.txt
Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

int calcTotal(int num[][])

{

}

int main()

{

int numbers[3] [5] = {};

cout << calcTotal(numbers);

return 0;

}

You need to provide an interface that will compile.

```
$ cat > problem.c
int calcTotal(int num[][])
{
return 1;
}
$ cc problem.c
problem.c:1: error: array type has incomplete element type
```

```
#include <iostream>
using namespace test;
int calcTotal(int num[][]);
int main()
{
double numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
cout << calcTotal(numbers);
int calcTotal(int num[][])
{
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
}
system("pause");
return 0;
}
```

```
#include <iostream>
using namespace test;
int calcTotal(int num[][]);
int main()
{
double numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
cout << calcTotal(numbers);
int calcTotal(int num[][])
{
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
}
system("pause");
return 0;
}
```

http://www.cplusplus.com/doc/tutorial/arrays/

Here are some ideas on how to implement it in C:

http://cboard.cprogramming.com/c-programming/106375-2d-array-function-parameter.html

http://www.java2s.com/Tutorial/Cpp/0140__Function/Passingatwodimensionalarraytoafunction.htm

```
#include <iostream>
using namespace test;
int calcTotal(int num[][]);
int main()
{
double numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
cout << calcTotal(numbers);
system("pause");
return 0;
}
int calcTotal(int num[][])
{
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
}
```

If you want to eliminate the function:

```
#include <iostream>
using namespace test;
int calcTotal(int num[][]);
int main()
{
double numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
system("pause");
return 0;
}
```

declaration of 'num' as multidimensional array must have bounds for all dimensions accept the first

int calcTotal(int num[][]);

error.txt

```
#include <iostream>
using namespace test;
int calcTotal(int num[][]);
int main()
{
int numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
cout << calcTotal(numbers);
system("pause");
return 0;
}
int calcTotal(int num[][])
{
int sumofnumbers = 0;
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{ sumofnumbers = sumofnumbers + numbers[i][j];
}
return sumofnumbers;
cout << endl;
}
}
```

```
int calcTotal(int num[][]);
```

If you read carefully the tutorials and links that I posted on multi-dimensional arrays, then you will learn much.
```
sumofnumbers = sumofnumbers + [b]num[/b][i][j];
```

@ phoffric

Now I see your point:

Line 3:

```
int calcTotal(int num[3][5]);
```

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
#include <iostream>
using namespace std;
int calcTotal(int num[3][5]);
int main()
{
int numbers[3] [5] = {};
int i = 0, j = 0;
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[0][3] = 4;
numbers[0][4] = 5;
numbers[1][0] = 6;
numbers[1][1] = 7;
numbers[1][2] = 8;
numbers[1][3] = 9;
numbers[1][4] = 0;
numbers[2][0] = 1;
numbers[2][1] = 2;
numbers[2][2] = 3;
numbers[2][3] = 4;
numbers[2][4] = 5;
cout << calcTotal(numbers) << endl;
system("pause");
return 0;
}
int calcTotal(int num[3][5])
{
int sumofnumbers = 0, i = 0, j = 0;
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{ sumofnumbers = sumofnumbers + num[i][j];
}
return sumofnumbers;
cout << endl;
}
}
```

Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

To sum the array values you can create a variable that holds the sum of all values in the array.

Let's take a look at your code:

Open in new window