Originally Posted by
whiteflags
Except that's not the problem.
The problem with invoking undefined behavior is that you could "fix" something entirely unrelated, and get positive results. Commontater's access violation message is more typical of out of bounds array access.
What do we have in the program that looks like that?
Code:
a[1]=1;
for(i=2; i<1000000; i++){
The acceptable range for an array is from 0 to n-1, inclusive. In this case, n is one million.
In this case if he starts the *calculations* at 0 or 1 it just hangs.
As for the array... totally unnecessary... I elminated that from the program and got this...
Attachment 10357
Code:
#include "global.h"
#include <errorx.h>
#includ <windows.h>
int _cdecl main(int argc, char *argv[])
{
unsigned int seq, maxseq=0, i, j, num=0;
unsigned int ticks;
ticks= GetTickCount();
for(i=2; i<1000000; i++){
seq=0;
j = i;
while(j >= i){
if(j & 1)
j=(j * 3) + 1;
else
j /= 2;
seq++;
}
if(seq>maxseq){
maxseq=seq;
num=i;
}
}
ticks = GetTickCount() - ticks;
printf("\nThe longest sequence results from %d, taking %d iterations\n",num,maxseq);
printf("Runtime = %d milliseconds\n\n",ticks);
return 0; }