C++ Seg Fault

Discussion in 'Computing, Science, and Technology' started by mattym, Dec 7, 2009.

  1. mattym

    mattym New Member

    Joined:
    Sep 22, 2007
    Messages:
    786
    Likes Received:
    3
    Device:
    iPhone 4 (Black)
    Hey i got these programs due for class. I have to use the g++ compiler and run it on their unix/linux machines (dumb ass rules).

    I needed to recreate the equation from this site http://www.unc.edu/depts/cmse/math/Verhulst.html

    here is the code i came up with
    Code:
    #include <iostream>
    
    using namespace std;
    
    double verhulstEQ(double, int, int, double, double);
    
    int main()
    {
    	int capacity;
    	double popGrowthRate;
    	double popLossRate;
    	double initialPop;
    	int totalYears;
    	
    	cout << "Please enter the initial population: ";
    	cin >> initialPop;
    	cout << "Please enter the population growth rate (as percentage): ";
    	cin >> popGrowthRate;
    	cout << "Please enter the population loss rate (as percentage): ";
    	cin >> popLossRate;
    	cout << "Please enter the population capacity: ";
    	cin >> capacity;
    	cout << "Please enter the number of years to predict: ";
    	cin >> totalYears;
    
    	cout << "After " << totalYears << " years the population will be " <<verhulstEQ(initialPop, capacity, totalYears, popGrowthRate, popLossRate);
    
    
    return 0;
    }
    
    double verhulstEQ(double a, int b, int c, double d, double e)
    {
    	double pn = a;
    	double ans;
    	int m = b;
    	int years = c;
    	double g = d;
    	double h = e;
    	
    	if (years = 0)
    	{
    		return pn;
    	}else
    	{
    		double temp = verhulstEQ(pn, m, (years - 1), g, h);
    		return (((1 + (g-h)) * temp) - ((g * (temp * temp)) / m));
    	}
    }
    
    can someone compile and run this on their pc or another ide for C++.
    Mine compiles but seg faults at runtime. Im exhausted let me know if i missed something.
  2. Xii

    Xii New Member

    Joined:
    Sep 9, 2008
    Messages:
    1,147
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    It won't compile since you made an undefined reference to the verhulstEQ function.
  3. bgizzle

    bgizzle Administrator Staff Member

    Joined:
    Sep 13, 2007
    Messages:
    5,100
    Likes Received:
    543
    Device:
    iPhone 6
    Code:
    #include <iostream>
    
    using namespace std;
    
    double verhulstEQ(double n, int m, int years, double g, double h);
    
    int main()
    {
    	int capacity;
    	double popGrowthRate;
    	double popLossRate;
    	double initialPop;
    	int totalYears;
    	
    	cout << "Please enter the initial population: ";
    	cin >> initialPop;
    	cout << "Please enter the population growth rate (as percentage): ";
    	cin >> popGrowthRate;
    	cout << "Please enter the population loss rate (as percentage): ";
    	cin >> popLossRate;
    	cout << "Please enter the population capacity: ";
    	cin >> capacity;
    	cout << "Please enter the number of years to predict: ";
    	cin >> totalYears;
    
    	cout << "After " << totalYears << " years the population will be " << verhulstEQ(initialPop, capacity, totalYears, popGrowthRate[B][COLOR="Red"]/100[/COLOR][/B], popLossRate[B][COLOR="red"]/100[/COLOR][/B]);
    
    
    return 0;
    }
    
    double verhulstEQ([B][COLOR="red"]double n, int m, int years, double g, double h[/COLOR][/B])
    {
    	
    	if (years [B][COLOR="Red"]==[/COLOR][/B] 0) {
    		return n;
    	} else {
    		double temp = verhulstEQ(n, m, (years - 1), g, h);
    		return [COLOR="red"][B](1 + g - h) * temp - g * temp * temp / m[/B][/COLOR];
    	}
    }
    

    Fixed (i think, too lazy to try compiling) some of the problems for you. First of all, = is an assignment operator, not a comparison operator. In other words years = 0 sets years to 0. == is the comparison operator, and years == 0 is true if years is 0 and false if years is not 0.

    Secondly, wtf is up with all of the parentheses in the return statement? That sh*t makes the code really unreadable. You should read up on operator precedence rules.

    Also, i dunno why you were copying the function arguments into new variables, so i removed that.

    Finally, i see you were asking for the population rates in percent, but then not converting it to decimal by dividing by 100.

    Overall, its a good effort, but it seems you didn't really put any effort into trying to track down the sources of some of your bugs.
  4. mattym

    mattym New Member

    Joined:
    Sep 22, 2007
    Messages:
    786
    Likes Received:
    3
    Device:
    iPhone 4 (Black)
    bgizzle thats why you rule.

    Yeah, sadly when i started learning this everything was still done with Java. Now i need to retake half this stuff in a new language. It make my brain hurt.

Share This Page