How Do You Implement a Multi-Species Population Model in C?

In summary: D array. The m in NextManyPredatorsPrey should be replaced with 1.In summary, the function NextManyPredatorsPrey returns Ni+1 for given Ni, r, K, and C[i,j] for each of m species and their predator-prey interaction.
  • #1
SpiffyEh
194
0
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?
 
Physics news on Phys.org
  • #2


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


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


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


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
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:
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
You didn't write this did you? There are a lot 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
So why do you write this a pseudo code, and then it might be more helpful.
 
  • #9
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
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
oh ok, i guess I'm stuck passing it in then
 
  • #12
What is K in all of this as well?
 
  • #13
K is the carrying capacity of the species
 
  • #14
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
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
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
does that make any sense? I'm still confused about the time step stuff
 

Related to How Do You Implement a Multi-Species Population Model in C?

1. What is a function in C?

A function in C is a block of code that can be called and executed multiple times within a program. It can take in input parameters and return an output value, making it a powerful tool for organizing and reusing code.

2. How do you declare a function in C?

To declare a function in C, you must specify its return type, name, and any input parameters within the function header. For example:
int square(int num)
This declares a function named "square" that takes in an integer parameter and returns an integer value.

3. How do you call a function in C?

To call a function in C, you simply use its name followed by parentheses and any necessary input parameters. For example:
int result = square(5);
This calls the "square" function with an input value of 5 and assigns the returned value to the variable "result".

4. Can a function in C have multiple return statements?

Yes, a function in C can have multiple return statements. However, only one return statement will be executed during the function call. The other return statements will be ignored.

5. What is recursion in C?

Recursion in C is when a function calls itself within its own code. This can be useful for solving certain types of problems, such as searching through a data structure. However, it is important to ensure that the recursive function has a base case to prevent an infinite loop.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Quantum Physics
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top