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

Click For Summary

Discussion Overview

The discussion revolves around implementing a multi-species population model in C, focusing on the mathematical formulation of species interactions through recurrent equations. Participants explore the coding challenges associated with translating these equations into a C function, particularly regarding array handling and the representation of time steps in the model.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant presents a system of recurrent equations for multiple species, indicating the need to implement a function, NextMany(), to compute future population sizes based on current values, growth rates, and carrying capacities.
  • Another participant notes that in C, array sizes are not tracked, suggesting that the size should be passed as an additional parameter to functions.
  • Concerns are raised about the differences between C and C++, particularly regarding array handling and the absence of built-in array size tracking in C.
  • A proposal is made to use a struct to encapsulate array information, allowing for easier management of array data across functions.
  • Discussion includes a modified model that accounts for predation, with a new function, NextManyPredatorsPreys(), suggested to incorporate predator-prey interactions into the equations.
  • Participants express confusion about the dimensionality of the population array, with some asserting it should be 2D due to time steps, while others believe it is 1D based on the equations presented.
  • Errors in the initial code are identified, including misuse of the sizeof operator and incorrect loop syntax, prompting further clarification on how to properly handle arrays in C.
  • Clarifications are made regarding the role of carrying capacity (K) in the equations, with one participant stating it represents the maximum population size that the environment can sustain.
  • Participants discuss the implications of passing arrays to functions and the necessity of ensuring valid array states before function calls.

Areas of Agreement / Disagreement

There is no consensus on the correct implementation of the population model, particularly regarding the dimensionality of the population array and the handling of time steps. Multiple competing views remain on how to structure the code and manage array data.

Contextual Notes

Participants express uncertainty about the definitions and roles of various parameters in the equations, particularly the handling of time steps and the structure of the population array. There are unresolved questions about the correct implementation of the code and the mathematical model.

SpiffyEh
Messages
191
Reaction score
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


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.
 


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
 


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.
 


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
 
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?
 
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 arguments. 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?
 
So why do you write this a pseudo code, and then it might be more helpful.
 
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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
11
Views
4K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
2
Views
2K
Replies
5
Views
2K