Sunday, August 2, 2009

Mad Hard C++?

I'm having trouble with some C++ code for my engineering class. I need to pass these two values, t and oldcd, between a void function and a single result function. Anyone know what's going on in this code?





void eulers (double patcharea[],double releaseconst,double patchdosage,double decayconst,double weight[],double metaconst,double%26amp; t)


{double newcd,oldcd,counter,integral;


oldcd=0;


for(t=0;t%26lt;=100;t++)


{


newcd=oldcd+(yprime(patcharea,releasec...


oldcd=newcd;


counter+=newcd;


cout%26lt;%26lt;newcd%26lt;%26lt;endl;


}


integral=counter/100.;


cout%26lt;%26lt;"The integral calculation is "%26lt;%26lt;integral%26lt;%26lt;endl;


}





double yprime (double patcharea[],double releaseconst,double patchdosage,double decayconst,double weight[],double metaconst,double%26amp; t,double%26amp; oldcd)


{double ans;


ans=((patcharea[0]*releaseconst


*patchdosage*exp(decayconst*t))/weight...


return an

Mad Hard C++?
Yahoo answers truncates long lines that do not have any white space, your call to yprime() has suffered that fate:


"newcd=oldcd+(yprime(patcharea,release...


so it is a little difficult to tell what the problem is. Adding some spaces would let the line be wrapped, or you could put line breaks in yourself. e.g.


newcd=oldcd+(yprime(


patcharea,


releasec...





I'm a little (OK, a lot) out of practice, but I think you might look carefully at how t is being used, since it was passed by reference to eulers() and then again by reference to yprime() I wonder if there could be some dereferencing problem?
Reply:void eulers (double patcharea[],double releaseconst,double patchdosage,double decayconst,double weight[],double metaconst,double%26amp; t)


//


//This line list the parameters for the function eulers


//


//


{double newcd,oldcd,counter,integral;


//


//declaring some double variables


//


oldcd=0; // setting the value of oldcd to 0


//


//


for(t=0;t%26lt;=100;t++) // this will cycle until t = 100


{


newcd=oldcd+(yprime(patcharea,releasec...


//


// assigns the value of the evaluated expression to newcd, old cd during each loop iteration.


//


oldcd=newcd;


counter+=newcd;


// gives counter the value of counter+newcd, this is just differnt //syntax, for counter = counter + newcd;


cout%26lt;%26lt;newcd%26lt;%26lt;endl;


//


// outputs newcd value to screen, and ends current line


//this will repeat every t cycle for t%26lt;=100


}


//After t cycles integral will be evaluated with the t-th value of //counter from the previous for loop


integral=counter/100.;


cout%26lt;%26lt;"The integral calculation is "%26lt;%26lt;integral%26lt;%26lt;endl;


//


//outputs The integral calculation is (whatever integral is) to //the screen, new line.


}





//Voids do not return a value





double yprime (double patcharea[],double releaseconst,double patchdosage,double decayconst,double weight[],double metaconst,double%26amp; t,double%26amp; oldcd)


{double ans;


ans=((patcharea[0]*releaseconst


*patchdosage*exp(decayconst*t))/weight...


return an





//evaluates the expression than return the double an
Reply:Mad hard? A ghetto engineer wannabe.


No comments:

Post a Comment