views:

128

answers:

3

Hey, so basically I have this issue, where I'm trying to put an equation inside of a function however it doesn't seem to set the value to the function and instead doesn't change it at all.

This is a predator prey simulation and I have this code inside of a for loop.

    wolves[i+1] = ((1 - wBr) * wolves[i] + I * S * rabbits[i] * wolves[i]); 
    rabbits[i+1] = (1 + rBr) * rabbits[i] - I * rabbits[i] * wolves[i];

When I execute this, it works as intended and changes the value of both of these arrays appropriately, however when I try to put it inside of a function,

    int calcRabbits(int R, int rBr, int I, int W)
{
     int x = (1 + rBr) * R - I * R * W;

    return x;
}

int calcWolves(int wBr, int W, int I, int S, int R)
{
    int x = ((1 - wBr) * W + I * S * R * R);
    return x;

}

And set the values as such

    rabbits[i+1] = calcRabbits ( rabbits[i], rBr, I, wolves[i]);
    wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);

The values remain the same as they were when they were initialized and it doesn't seem to work at all, and I have no idea why. I have been at this for a good few hours and it's probably something that I'm missing, but I can't figure it out.

Any and all help is appreciated.

Edit: I realized the parameters were wrong, but I tried it before with the correct parameters and it still didnt work, just accidentally changed it to the wrong parameters (Compiler mouse-over was showing the old version of the parameters)

Edit2: The entire section of code is this

    days = getDays(); // Runs function to get Number of days to run the simulation for
    dayCycle = getCycle(); // Runs the function get Cycle to get the # of days to mod by

    int wolves[days]; // Creates array wolves[] the size of the amount of days
    int rabbits[days]; // Creates array rabbits [] the size of the amount of days
    wolves[0] = W; // Sets the value of the starting number of wolves
    rabbits[0] = R; // sets starting value of rabbits


    for(int i = 0; i < days; i++) // For loop runs the simulation for the number of days
    {



//        rabbits[i+1] = calcRabbits ( rabbits[i], rBr, I, wolves[i]);    

// // //This is the code to change the value of both of these using the function 

//        wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);



    // This is the code that works and correctly sets the value for wolves[i+1]

        wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);
        rabbits[i+1] = (1 + rBr) * rabbits[i] - I * rabbits[i] * wolves[i];

    }

Edit: I realized my mistake, I was putting rBr and wBr in as ints, and they were floats which were numbers that were below 1, so they were being automatically converted to be 0. Thanks sje

A: 

Phil I cannot see anything evidently wrong in your code.

My hunch is that your are messing up the parameters.

Using gdb at this point would be an over kill. I recommend you put print outs in calcRabbits and calcWolves. Print out all the parameters, the new value, and the iteration number. That will give you a good idea of what is going on and will help trace the problem.

Do you have the full code with initialization we could try to test and run?

ntroncos
Accidentally deleted a */ in funcs.cpp, might have to add that back in there. Thanks again
Don Marko
A: 

I'm not sure this is the problem, but this is bad:

int wolves[days]; // Creates array wolves[] the size of the amount of days
int rabbits[days]; // Creates array rabbits [] the size of the amount of days

days is determined at runtime. This is nonstandard in c++ (and for large number of days could destroy your stack) you should only be using constants in array sizes. You can dynamically size a vector to workaround this limitation (or heap allocate the array).

Change to this:

std::vector<int> wolves(days);
std::vector<int> rabbits(days);

Or to this:

int *wolves = new int[days];
int *rabbits = new int[days];

// all your code goes here

delete [] wolves;  // when you're done
delete [] rabbits;  // when you're done

Which will dynamically allocate the array on the heap. The rest of the code should work the same.

Don't forget to #include <vector>, if you use the vector approach.

If you're still having problems, I would cout << "Days: " << days << endl; to make sure you're getting the right number back from getDays(). If you got zero, it would seem to manifest itself in "the loop not working".

Stephen
Thanks, I'll refrain from using vectors because for the purposes of the class, we have not worked with them yet. The number of days works fine, because when I try to run it with the original equation to set the value of the array, it works, so I think I can isolate the functions as being the cause of error. Thanks for the advice though, as soon as I get this running, I'll read up on the vector library.
Don Marko
@Phil : It's still dangerous. Edited to show how to heap allocate an array.
Stephen
A: 

I was using an integer as an argument for a double.

Don Marko