F
felipemartinsss
Hi Everybody.
I have a problem with the flag -O2 and I would like to know why this
problem is ocurring.
I have the following code:
#include <stdio.h>
int f3nPlus1 (int n, int count);
int main(void) {
/* Declaração de variáveis */
int n = 0;
int maximumCycle = 0;
int begin = 0;
int end = 0;
int temp = 0;
FILE *filePtr;
/* Código */
filePtr = fopen("a.in", "r");
if (filePtr) {
while (!feof(filePtr)) {
fscanf(filePtr, "%d%d\n", &begin, &end);
/*printf ("begin: %d\n", begin);
printf ("end: %d\n", end);*/
for (n = begin; n <= end; n++) {
temp = f3nPlus1( n, 0 );
/*printf ("temp: %d\n", temp);*/
if (temp > maximumCycle) {
maximumCycle = temp;
}
}
printf( "%d %d %d\n", begin, end, maximumCycle );
temp = 0;
maximumCycle = 0;
}
fclose(filePtr);
} else {
printf ("Arquivo nao pode ser aberto.\n");
}
return 0;
}
int f3nPlus1( int n, int count ) {
if( n == 1 ) {
return count + 1;
} else {
if(n % 2 != 0) {
n = 3 * n + 1;
f3nPlus1 ( n , count + 1);
} else {
n = n / 2;
f3nPlus1 ( n , count + 1);
}
}
}
When I compile the code with gcc -o f3nPlus1 f3nPlus1.c -lm -lcrypt -
pipe -ansi, everything's ok.
But, when I compile with gcc -O2 -o f3nPlus1 f3nPlus1.c -lm -lcrypt -
pipe -ansi, the execution give me wrong answers.
The contents of the file a.in are:
1 10
100 200
900 1000
1 10
1 11
12 13
1 10
100 200
201 210
900 1000
Thanks for the attention!
I have a problem with the flag -O2 and I would like to know why this
problem is ocurring.
I have the following code:
#include <stdio.h>
int f3nPlus1 (int n, int count);
int main(void) {
/* Declaração de variáveis */
int n = 0;
int maximumCycle = 0;
int begin = 0;
int end = 0;
int temp = 0;
FILE *filePtr;
/* Código */
filePtr = fopen("a.in", "r");
if (filePtr) {
while (!feof(filePtr)) {
fscanf(filePtr, "%d%d\n", &begin, &end);
/*printf ("begin: %d\n", begin);
printf ("end: %d\n", end);*/
for (n = begin; n <= end; n++) {
temp = f3nPlus1( n, 0 );
/*printf ("temp: %d\n", temp);*/
if (temp > maximumCycle) {
maximumCycle = temp;
}
}
printf( "%d %d %d\n", begin, end, maximumCycle );
temp = 0;
maximumCycle = 0;
}
fclose(filePtr);
} else {
printf ("Arquivo nao pode ser aberto.\n");
}
return 0;
}
int f3nPlus1( int n, int count ) {
if( n == 1 ) {
return count + 1;
} else {
if(n % 2 != 0) {
n = 3 * n + 1;
f3nPlus1 ( n , count + 1);
} else {
n = n / 2;
f3nPlus1 ( n , count + 1);
}
}
}
When I compile the code with gcc -o f3nPlus1 f3nPlus1.c -lm -lcrypt -
pipe -ansi, everything's ok.
But, when I compile with gcc -O2 -o f3nPlus1 f3nPlus1.c -lm -lcrypt -
pipe -ansi, the execution give me wrong answers.
The contents of the file a.in are:
1 10
100 200
900 1000
1 10
1 11
12 13
1 10
100 200
201 210
900 1000
Thanks for the attention!