Homework Help: Programming a function in C

1. Sep 18, 2010

SpiffyEh

Writing a function problem

If m different species share the same habitat the model becomes a system of recurrent equations:
Ni+1 [1] = Ni [1] + r [1] Ni [1] ( 1 - Ni [1] / K [1] ),
Ni+1 [2] = Ni [2] + r [2] Ni [2] ( 1 - Ni [2] / K [2] ),
……………………………………………………, i = 0, 1, …, i , i + 1, … (2)
Ni+1 [m] = Ni [m] + r [m] Ni [m] ( 1 - Ni [m] / K [m] ).
Lesson 2. Write a C function, NextMany(), that returns Ni+1 for given Ni, r, K for each of m species.

Ok so I know that r and k are 1D m vectors
N - 2D mxn matrix
m - # of species
n - # number of time steps

I don't know why but I'm having a hard time starting this function. So, I understand that I need to take and do the equation n times for each of m species. But how do I pass in the arrays? I know I have to pass in pointers to the function but then I can't remember how to get the size of the array. Also since the N array is mxn and n should only be a value shouldn't it be mx1? or am i storing the Ni +1 in this array?

2. Sep 18, 2010

airborne18

Re: Writing a function problem

C does not track the size of arrays. Just pass the pointer to the array, and an integer with the array size to the function.

3. Sep 18, 2010

SpiffyEh

Re: Writing a function problem

hmm I guess i'll have to pass in the array size then. I'm use to java and I haven't done C in years

4. Sep 18, 2010

airborne18

Re: Writing a function problem

C++ has an array class like java that does all that, but in C you are on your own.

One thing you can do is create a struct that contains the info.

struct myarray{
int nelements;
void *data;
};

Then pass that around to functions, or a pointer to it.

5. Sep 18, 2010

SpiffyEh

Re: Writing a function problem

oh ok, i'll try something, I've done some C++ but not C so i'm very lost since it doesn't have the same array features

6. Sep 18, 2010

SpiffyEh

Without sacrificing the concept we will consider a simple model that allows predation but nothing else. With the predation term the equations (2) become as follows:
Ni+1 [1] = Ni [1] + r [1] Ni [1] ( 1 - Ni [1] / K [1] ) - C[1,2] Ni [1] Ni [2] + C[1,3] Ni [1] Ni [3] + …,
Ni+1 [2] = Ni [2] + r [2] Ni [2] ( 1 - Ni [2] / K [2] ) - C[2,1] Ni [2] Ni [1] + C[2,3] Ni [2] Ni [3] + …,
………………………………………………………., i = 0, 1, …, i , i + 1, … (3)
Ni+1 [m] = Ni [m] + r [m] Ni [m] ( 1 - Ni [m] / K [m] ) - C[m,1] Ni [m] Ni [1] + C[m,2] Ni [m] Ni [2] + ….
Here C[i, j] is a measure of predation efficiency of a species j preying on a species i .
Of course, rabbits do not prey on cows, so, in this and other similar cases predation efficiency factor should be set 0.
Lesson 3. Write a C function, NextManyPredatorsPreys(), that returns Ni+1 for given Ni, r, K, C[i, j] for each of m species and their predator-prey interaction.

I was told that r and k are 1D arrays
N is a 2D mxn array - which doesn't make sense to me
C[i,j] - 2D mxm array of doubles

m is the number of species
n is the number of steps in time

I tried to write a function. But I don't see how N is a 2D matrix it is referred to as N[1]... in the equations above. Also I don't see how time steps come into account in this problem. And I don't know how to write in C, so my code is C++ as of right now...

Here's what I have:
Code (Text):

void NextManyPredatorsPrey(int Ni[], float r[],int K[], double C[][])
{
int m = sizeof(Ni);
float Ni1[m];
for(int i =1; i <= m; i++)
{
float cterm = 0;

for(int j = 2; j <= m, j++)
{
cterm = cterm - C[m][j] * Ni[m] *Ni[j];
}
Ni1[m] = Ni[m] + r[m]*Ni[m]*(1-(Ni[m]/K[m])) - cterm;
}
}

Can anyone see more form the problem than i'm seeing?

7. Sep 18, 2010

airborne18

You didn't write this did you? There are alot of problems with that code, and the least of them is the usage of sizeof. C or C++, it is all the same, you are not using any C++ unique features.

the variable m will not be correct.

Have you actually compiled this code? The for loop has a , and it should be a ; , I believe you will get some complaints about the sizeof and the arguements. I think they need a size when they are passed by value. ( but it has been a while so I could be wrong ).

And you are right Ni is not a 2 dimensional, at least not in theory. C/C++ does not care how you access an array. Also this n, steps in time, where are you using that?

8. Sep 18, 2010

airborne18

So why do you write this a pseudo code, and then it might be more helpful.

9. Sep 18, 2010

SpiffyEh

I haven't compiled the code, I just wrote it out as an idea of what to do. Sorry for all the errors, I missed them. I don't really understand the n steps in time. I was trying to figure out a way to finding the size without having to pass it in and i searched and apparently sizeof is what everyone kept suggesting to others so i figured i would try it.

10. Sep 18, 2010

airborne18

here is where sizeof will work..

int myArray[5];
int x = sizeof[myArray];

it should be x=20 at this point.

int myArray[];
int x = sizeof[myArray];

this will not work..

also

int *myarray = new[int*5];
int x = sizeof(myarray);

in this case x will be the size of a pointer.

You are looking for an easy answer and it does not exist in c/c++. the only way sizeof works for arrays, is for constant size arrays.

11. Sep 18, 2010

SpiffyEh

oh ok, i guess i'm stuck passing it in then

12. Sep 18, 2010

airborne18

What is K in all of this as well?

13. Sep 18, 2010

SpiffyEh

K is the carrying capacity of the species

14. Sep 18, 2010

airborne18

If you read the exact instructions, it says write a function for a given value, so you do not need the arrays.

You are being passed the indivual values for each elelment of the array. If I read it correctly

15. Sep 18, 2010

airborne18

Get rid of the first 2 lines in your function. and fix the for loop where it has j<=m make the , a ; and it should compile.

Keep in mind that the arrays need to be valid arrays before this function is called. But you do not have to change anything else in the code.

Are the calcluations correct. I have no clue, but it will handle passin in the arrays.

16. Sep 18, 2010

SpiffyEh

I actually asked about the array issue since the instructions weren't clear to me. I was told that each different part of the equation was. Apparently Ni is a 2D array. This was the response:
From the equations for part 3 it looks like Ni is a 1D array but you said it has time steps too which makes it 2D.

Let's say I have three species N[0], N[1], N[2].
Each of those will change in time. For time steps, 0, 1, 2, ..., m-1 we have to store m values for each species. So, it becomes a 2D array: N[3][m].

which makes me believe the rest of my code is incorrect

17. Sep 19, 2010

SpiffyEh

does that make any sense? I'm still confused about the time step stuff