Difference entre C et C++ à l'exécution
Bonjour,
voici un programme
Si je le compile en C ou en C++, je n'obtiens pas le même
résultat. En C++ fonctionne comme attendu, en C, j'ai un seg fault
(runtime error)
Voyez-vous une raison ?
Cordialement,
David
#include <stdio.h>
int Max( int i, int j ) { return ( i > j ? i : j ); }
int Min( int i, int j ) { return ( i < j ? i : j ); }
int CycleLength( int n ) {
int cycleLength = 1;
while ( n != 1 ) {
if ( n &0x01 ) n = 3*n + 1;
else n >>= 1;
++cycleLength;
}
return cycleLength;
}
int maxCycle( int i, int j ){
int max = CycleLength( i++ );
for( ;i <= j; ++i )
max = Max( max, CycleLength( i ) );
return max;
}
int main()
{
int i, j;
while( fscanf(stdin, "%d %d\n", &i, &j ) == 2 )
fprintf(stdout, "%d %d %d\n", i, j ,
maxCycle( Min(i,j), Max(i,j) ));
}
|