Pass array into the function

Hi guys: Can any one please tell me how to sum the array values in the function ?
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
Who is Participating?

x
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.

Commented:
You will not be able to pass the array as a parameter to CalcTotal because the type mismatches.

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:
``````	double numbers[3] [5] = {};    <----------   Double
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[][])    <----------   Integer
{
for(j = 0; j < 5; j++)
{
for(i = 0; i < 3; i++)
{
cout << numbers[i][j] << endl;
}
cout << endl;
}
}
``````
Author Commented:
Im sorry my mistake. Can you please tell me how to write code in the function to sum all the values
Commented:
Sorry just need to confirm one thing is this Homework or assignment?
Commented:
Do not worry I will help you as well even if it's assignment but I will make sure you understand what is going on rather than giving you a ready-made piece of code.
Author Commented:
trying to figure it out how to add the values into an array. I have a list of chart of accounts which needs to be manipulate into an array but first i want to understand its manipulation in C++
Commented:
What's wrong with adding the values to the array?
You are adding the values correctly.
Commented:
You need to make the calling to the function correct first:
int calcTotal(int num[][])
{
}

int main()
{
int numbers[3] [5] = {};
cout << calcTotal(numbers);
return 0;
}
\Commented:
>> int calcTotal(int num[][])
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
``````
Author Commented:
I still get the error
``````#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;
}
``````
\Commented:
There are various approaches you can take to pass a 2d array as an argument to a function. There is much to say about how to do this. Here is a tutorial that covers the basics. 2D arrays are a difficult concept in C, so study it carefully:
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

Commented:
I think You meant:
``````#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;
}
``````
Author Commented:
i get an error on function declaration
declaration of 'num' as multidimensional array must have bounds for all dimensions accept the first

int calcTotal(int num[][]);
Author Commented:
I did change in the function as i need the sum of it. Is this is correct ? Also it still gives error on function declaration.

error.txt
Author Commented:
Change in function.

``````#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;
}
}
``````
\Commented:
Your post in http:#35356291 should not compile, since as I mentioned earlier, the following code is not valid C/C++ code:
``````int calcTotal(int num[][]);
``````
If you read carefully the tutorials and links that I posted on multi-dimensional arrays, then you will learn much.
Commented:
``````sumofnumbers = sumofnumbers + [b]num[/b][i][j];
``````

@ phoffric

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

Experts Exchange Solution brought to you by

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

Author Commented:
its working now but the function return the wrong value.

``````#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;
}
}
``````
Author Commented:
i find it its the braces.
it fix thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.