//File Name: lab6.cpp
//Name: Zac Carlson
//Date: 10/29
#include<iostream>
/*#include<sys/time.h>*/
#include<cstdlib>
using namespace std;
int factorialN ( int nNum ){
int factN = nNum;
if ( nNum == 1 ){
return 1;
}
else{
return factN * factorialN ( nNum - 1 );
}
}
int factorialR ( int rNum ){
int factR = rNum;
if ( rNum == 1){
return 1;
}
else{
return factR * factorialR ( rNum - 1 );
}
}
int factorialNminusR ( int nMinusRtotal ){
int factNminusR;
if ( nMinusRtotal == 1 ){
return 1;
}
else{
return factNminusR * factorialNminusR ( nMinusRtotal - 1 );
}
}
int main(){
int nNum, rNum, cNr, nMinusRtotal;
cout << "Using the statistics equation c(n,r) enter a number for n: ";
cin >> nNum;
cout << "Now enter a number for r: ";
cin >> rNum;
nMinusRtotal = nNum - rNum;
cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
cout << "Your calculation for c(n,r) is: " << cNr << endl;
/*struct timeval stop, start;
gettimeofday(&start, NULL);
//Time your function here
gettimeofday(&stop, NULL);
cout << "Time: " << stop.tv_usec - start.tv_usec << endl;*/
return 0;
}
17 Replies - 319 Views - Last Post: 30 October 2012 - 06:55 PM
#1
Stack overflow problem.....
Posted 30 October 2012 - 10:03 AM
Replies To: Stack overflow problem.....
#2
Re: Stack overflow problem.....
Posted 30 October 2012 - 10:09 AM
#3
Re: Stack overflow problem.....
Posted 30 October 2012 - 10:14 AM
aresh, on 30 October 2012 - 10:09 AM, said:
Sorry. I did fix it in the actual code just not on this post. Therefore, it should say: int factNminusR = nMinusRtotal;
I'll rewrite the code and it still causes a stack overflow:
//File Name: lab6.cpp
//Name: Zac Carlson
//Date: 10/29
#include<iostream>
#include<sys/time.h>
#include<cstdlib>
using namespace std;
int factorialN ( int nNum ){
int factN = nNum;
if ( nNum == 1 ){
return 1;
}
else{
return factN * factorialN ( nNum - 1 );
}
}
int factorialR ( int rNum ){
int factR = rNum;
if ( rNum == 1){
return 1;
}
else{
return factR * factorialR ( rNum - 1 );
}
}
int factorialNminusR ( int nMinusRtotal ){
int factNminusR = nMinusRtotal;
if ( nMinusRtotal == 1 ){
return 1;
}
else{
return factNminusR * factorialNminusR ( nMinusRtotal - 1 );
}
}
int main(){
int nNum, rNum, cNr, nMinusRtotal;
cout << "Using the statistics equation c(n,r) enter a number for n: ";
cin >> nNum;
cout << "Now enter a number for r: ";
cin >> rNum;
nMinusRtotal = nNum - rNum;
cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
cout << "Your calculation for c(n,r) is: " << cNr << endl;
struct timeval stop, start;
gettimeofday(&start, NULL);
//Time your function here
gettimeofday(&stop, NULL);
cout << "Time: " << stop.tv_usec - start.tv_usec << endl;
return 0;
}
#4
Re: Stack overflow problem.....
Posted 30 October 2012 - 12:41 PM
Jim
#5
Re: Stack overflow problem.....
Posted 30 October 2012 - 01:47 PM
#6
Re: Stack overflow problem.....
Posted 30 October 2012 - 02:05 PM
cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
Do you really need to call these three recursive functions on the same line. You are probably burning through your available stack memory half way thru this mess.
Jim
#7
Re: Stack overflow problem.....
Posted 30 October 2012 - 05:29 PM
jimblumberg, on 30 October 2012 - 02:05 PM, said:
cNr = factorialN ( nNum ) / ( factorialR ( rNum )* factorialNminusR ( nMinusRtotal));
Do you really need to call these three recursive functions on the same line. You are probably burning through your available stack memory half way thru this mess.
Jim
Jim,
I was able to figure it out. I did do adjustments to make it less messy and I don't know if that was the actual cause because after I figured out the problem, it works just fine. The problem was I was putting values for n and r that would cause the solution to be negative. Once I started to put n bigger than r it works fine.
Second, about your "mess" comment. One thing that I don't like, and this may be the problem with emails and emotions through them, but to tell somebody you are trying to help that their code is a mess is not very encouraging. I could not picture my college instructor telling me or somebody else that when helping them their work is a mess. If I come off sensitive, so be it. I very well may be sensitive, but at the same time I take words pretty serious I guess you could say. I'm not saying you are good help, because you are. You've helped me with this assignment as well as a previous one. Also, last time, I complained about how I've been spoken to regarding help my post got ceased and I got the -2 reputation as you see now. Therefore, if you, or anybody does this again, I'm going to laugh as I did the last one.
#8
Re: Stack overflow problem.....
Posted 30 October 2012 - 05:41 PM
Quote
Take a step back for a minute. Nobody is coming here simply to bash you or your code. A lot of our members are very direct when helping, not to be rude, but simply so to get the point across to fix things and move onto the next problem. If somebody is doing something the wrong way, should it be encouraged or corrected? Nobody here is flaming you, or coming here simply to bash you or your code. Take it in strides and try to find the helpful advice in it all. This is the internet, after all.
Quote
The last time, you called someone a dick head. Quoting from your post below.
Quote
If you really feel somebody is attacking you, then you are welcome to shoot myself or another team member a PM to discuss further.
#9
Re: Stack overflow problem.....
Posted 30 October 2012 - 05:50 PM
#10
Re: Stack overflow problem.....
Posted 30 October 2012 - 05:52 PM
#11
Re: Stack overflow problem.....
Posted 30 October 2012 - 06:00 PM
Quote
Quote
You don't like that jim called your code messy; however, you figured out the solution after you made the code "less messey".
Don't take everything as an "attack", we simply say what will guide your to find a solution without spoon feeding it to you.
#12
Re: Stack overflow problem.....
Posted 30 October 2012 - 06:24 PM
macosxnerd101, on 30 October 2012 - 05:41 PM, said:
Quote
Take a step back for a minute. Nobody is coming here simply to bash you or your code. A lot of our members are very direct when helping, not to be rude, but simply so to get the point across to fix things and move onto the next problem. If somebody is doing something the wrong way, should it be encouraged or corrected? Nobody here is flaming you, or coming here simply to bash you or your code. Take it in strides and try to find the helpful advice in it all. This is the internet, after all.
Quote
The last time, you called someone a dick head. Quoting from your post below.
Quote
If you really feel somebody is attacking you, then you are welcome to shoot myself or another team member a PM to discuss further.
Look back at the post and try to understand why I said it. I was not calling him a dick head. I originally thought "DIC head" was what he purposely made for his name. Then I saw that it's the name for everybody. Which concludes that this site should think about changing that portion. I hope this makes sense.
jon.kiparsky, on 30 October 2012 - 05:50 PM, said:
And your team talking about not spoon feeding me? What is this garbage? Are you my mother, my father?
#13
Re: Stack overflow problem.....
Posted 30 October 2012 - 06:26 PM

POPULAR
#14
Re: Stack overflow problem.....
Posted 30 October 2012 - 06:31 PM
#15
Re: Stack overflow problem.....
Posted 30 October 2012 - 06:48 PM
macosxnerd101, on 30 October 2012 - 06:31 PM, said:
I'm done with all this. Look at the name, DIC, say it out loud with out saying it as it should be, as in, "D", "I, "C". Say it as a whole, "DIC". Do you see my point? If you don't, I'm sorry to hear that. Have a nice day.
|
|

New Topic/Question
This topic is locked



MultiQuote







|