I tried adjusting the loop so it would run one step farther with
for(d = 2; d==m; d++)
but no luck. I also tried
for(d = 2; d==(m+1); d++)
but it still skips the code.
Any ideas?

03-24-2007

laserlight

The thing is, your question asks you to convert the C code to C++ code. If the C code is invalid, you simply cannot convert it. If it is logically incorrect, then your C++ code should likewise be logically incorrect, otherwise you would not meet the requirements of the question.

03-24-2007

cdn_bacon

Quote:

Originally Posted by laserlight

The thing is, your question asks you to convert the C code to C++ code. If the C code is invalid, you simply cannot convert it. If it is logically incorrect, then your C++ code should likewise be logically incorrect, otherwise you would not meet the requirements of the question.

I guess you are right. If I had to get the program working what would you suggest I edit. I just cant wrap my head around this problem even though it should be so simple.

03-24-2007

MacGyver

You're rewriting the code already given to you. Don't do that. Here's the old loop:

Code:

for(d = 2; d<min; d++)

if(((a%d)++0) && ((b%d) ==0)) break;

It's the same thing as this:

Code:

for(d = 2; d<min; d++){
if(((a%d)++0) && ((b%d) ==0)){
break;}}

Undertand what it's doing. It's cycling through all posibilities to divide a and b by d, starting with 2 and moving all the way up until it reaches either a or b, whichever is lowest. If it can't find a common denominator by then, there is none. If it found one, it doesn't need to search anymore, so it breaks the loop.

You changed the entire loop to include everything else after it. Take that stuff out and put it after the loop.

Outside of changing the obviously weird ++ to a ==, I don't think you should be altering much else.

03-24-2007

cdn_bacon

Quote:

Originally Posted by MacGyver

You're rewriting the code already given to you. Don't do that. Here's the old loop:

Code:

for(d = 2; d<min; d++)

if(((a%d)++0) && ((b%d) ==0)) break;

It's the same thing as this:

Code:

for(d = 2; d<min; d++){
if(((a%d)++0) && ((b%d) ==0)){
break;}}

Undertand what it's doing. It's cycling through all posibilities to divide a and b by d, starting with 2 and moving all the way up until it reaches either a or b, whichever is lowest. If it can't find a common denominator by then, there is none. If it found one, it doesn't need to search anymore, so it breaks the loop.

You changed the entire loop to include everything else after it. Take that stuff out and put it after the loop.

Outside of changing the obviously weird ++ to a ==, I don't think you should be altering much else.

Ok, thanks! That helped out huge. I guess all the indents that are on the C code sample I was given are messed up and that is why I was placing the code in the loop. It looked like it should be there. Your explanation makes sense though. Thanks MacGyver and laserlight for all your help.! I really appreciate it!!!