### What type of argument should I use in the function(s)?

Hello everyone,
I am a bit beginner in cplusplus and I need to code a small piece of code but I do not know how to do it.
I have two cost matrices like f_kt=[[3 5 7
6 4 2]]

and c_kt=[[ 1 2 3]
4 5 6]];

the lines represent the number of products which is 2 and the coloumns represents the number of periods which is 3. I have a special cost analysis function which can be applied only one product to calculate the cost for all periods , which does 3*A+ 5A +7A +1B+2B +3B. If I had only one product I could do it easily like
because in that case the arrays would be f_kt=[3 5 7] and c_kt=[1 2 3] . This data would be the input of my special cost analysis function and I could calculate it. How can I calculate the total cost by reading data of the other products? As I said, that special function is only applicable to compute the cost of one line (one product).

What I want to obtain: 3*A+ 5A +7A +1B+2B +3B+ 6*A+ 4A +2A +4B+5B +6B

Thank you so much in advance
Last edited on
27*A+21*B

If that is not what you require, then please have another go at trying to explain your problem.
I think you're asking "how do I pass multi-dimensional arrays as function arguments", so here's one option.
 ``12345678910111213141516171819202122`` ``````#include int combine(int one[2][3], int two[2][3]) { // Do your calculations, etc. int result = 0; for(int i = 0; i < 2; i ++) { for(int k = 0; k < 3; k ++) { result += one[i][k] + two[i][k]; } } return result; } int main() { int foo[2][3] = {{1, 1, 1},{1, 2, 3}}; int bar[2][3] = {{8, 8, 8},{4, 5, 6}}; std::cout << combine(foo, bar) << std::endl; }``````

Eventually I recommend you look into the standard containers such as vectors so that you can benefit from dynamic memory.
Last edited on
Wait, I think I understand what's being asked. Here's the same as above, but able to take arrays of products of generic size. Basically the function can handle any size of product array so long as you tell it how many products are in the arrays as well:

 ``12345678910111213141516171819202122`` ``````#include int combine(int one[][3], int two[][3], int count) { // Do your calculations, etc. int result = 0; for(int i = 0; i < count; i ++) { for(int k = 0; k < 3; k ++) { result += one[i][k] + two[i][k]; } } return result; } int main() { int foo[2][3] = {{1, 1, 1},{1, 2, 3}}; int bar[2][3] = {{8, 8, 8},{4, 5, 6}}; std::cout << combine(foo, bar, 2) << std::endl; }``````

Again, I recommend studying vectors soon, they solve many problems. https://cplusplus.com/reference/vector/vector/
Thank you so much , problem solved
Topic archived. No new replies allowed.