Recursion int param const issue
May 2, 2009 at 11:52am UTC
Hi,
I am running the code below but I get the error
invalid suffix n on integer constant
This error relates to the bolded line below at the first recursive call. Apparently I can't pass the paramater back again to the method. Any suggestions here?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
using namespace std;
class Sequence{
public :
void dCount(long n);
Sequence();
~Sequence();
private :
long count;
};
void Sequence::dCount(long n){
count++;
if (n ==1)
return ;
if (n %2 ==0)
dCount(n / 2);
else if (n % 2 != 0){
//NEXT LINE PRODUCES ERROR...CAN'T SEND 'n' back to method ??
dCount(3n+1) ;
}
}
Sequence::Sequence(){
count =0;
}
Sequence::~Sequence(){; }
int main()
{
Sequence s;
s.dCount(5L);
return 0;
}
May 2, 2009 at 12:14pm UTC
change the bolded line to dCount(n*3+1);
May 2, 2009 at 12:54pm UTC
thanks! whenever i encounter an error i assume that it's my lack of c++ knowledge, not some silly error like this one, that causes the problem.
May 2, 2009 at 1:44pm UTC
Yeah it happens to us all lol :) am i right in presuming you're a maths student of some sort?
'3n' thinking it will mean '3 x n' has got maths student written all over it lol.
Topic archived. No new replies allowed.