Link to home
Start Free TrialLog in
Avatar of mustish1
mustish1

asked on

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
Avatar of jimyX
jimyX

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

Avatar of mustish1

ASKER

Im sorry my mistake. Can you please tell me how to write code in the function to sum all the values
Sorry just need to confirm one thing is this Homework or assignment?
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.
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++
What's wrong with adding the values to the array?
You are adding the values correctly.
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;
}
>> 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

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

SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

i find it its the braces.
it fix thanks a lot