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;
}

Open in new window

error.txt
mustish1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

jimyXCommented:
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;
        }
     }

Open in new window

mustish1Author Commented:
Im sorry my mistake. Can you please tell me how to write code in the function to sum all the values
jimyXCommented:
Sorry just need to confirm one thing is this Homework or assignment?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jimyXCommented:
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.
mustish1Author 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++
jimyXCommented:
What's wrong with adding the values to the array?
You are adding the values correctly.
jimyXCommented:
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;
}
phoffric\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

Open in new window

mustish1Author 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;
}

Open in new window

#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;
}

Open in new window

phoffric\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


   
jimyXCommented:
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;
        }
     }

Open in new window



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;
}

Open in new window

mustish1Author 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[][]);
mustish1Author 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
mustish1Author 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;
        }
     }

Open in new window

phoffric\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[][]); 

Open in new window

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

Open in new window


@ phoffric
Now I see your point:

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

Open in new window

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
mustish1Author 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;
        }
     }

Open in new window

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