My teacher asked me to create a loop that will be executed 1M times.
I tried for(long i=0;i<1000000;i++), but the program is crashing. The maximum number that the program accepts is 10.000.
Any ideas? Is this possible?
Also he asked me to create a random number >100.000. I am using rand();. Should I use a different method?
this is the code:
start_time = clock();
for(long i=0;i<1000000;i++){
num1 = rand();
num2=rand();
gcd1(num1,num2);
}
end_time = clock();
elapsed_time = (end_time - start_time) / CLOCKS_PER_SEC;
printf( "time is %.3f seconds\n", elapsed_time );
system("pause");
This the gcd:
int gcd1(int x, int y){
int z;
if (x<y)
z=x+1;
else if (y<x)
z=y+1;
do{
z=z-1;
}
while((x%z!=0) or (y%z!=0));
return z;
}
}
Try your loop as
rand()can return 0. When it does that (after more than 10000 loops), your program will setzto 0 and attempt to divide by 0.