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.
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
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.
ASKER
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 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[][])
{
}
int main()
{
int numbers[3] [5] = {};
cout << calcTotal(numbers);
return 0;
}
>> int calcTotal(int num[][])
You need to provide an interface that will compile.
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
ASKER
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think You meant:
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;
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;
}
ASKER
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[][]);
declaration of 'num' as multidimensional array must have bounds for all dimensions accept the first
int calcTotal(int num[][]);
ASKER
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
error.txt
ASKER
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;
}
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}
}
ASKER
i find it its the braces.
it fix thanks a lot
it fix thanks a lot
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