This program accepts as an input a txt file which I explain below and tries to find the lcm. everytime it excludes an i and tries to find the minimum lcm, for every i I exclude. I exclude an i each time and needs to find the minimum lcm.
Here is a c program I have to run using a terminal in linux. It takes as an input a txt file. for example if I name the c file example.c, i use the command
./example txt_file.txt to run it. the txt file has in the first line the number of integers the txt file is going to have. After the first line I list all the integers. If I create for example a txt file
10
1 2 3 4 5 6 7 8 9 10 or something familiar (with the command cat in linux) it runs fine. However, with a testcase of 50000 numbers in total
50000
744 3443 ..... 938 for example, when I run it I get an error.
Floating Point Exception. I tried everything for a couple of days and still cant find why this happends. I reckon no divison with 0 happends. I use int64_t cause I have to deal with very large numbers. Unfortunately, I cant include the txt file with 50000 numbers here. Can you please check Why this error happens? I tried everything and I cant get it on my own.
You seem to be under the misapprehension that C array indices start at 1 and go to the SIZE of the array, but they actually start at 0 and go to SIZE-1.
So this is a problem
1 2
int64_t array[N];
for (k = 1; k <= N; k++)
C array indices start at 0 and go up to N-1.
for (k = 0; k < N; k++)
You're basically doing the same thing in findlcm. You pass N in as n and then start the second loop at n, but the highest index is n - 1. And don't forget to start at 0!
Get rid of the VLA. malloc the array instead so it is allocated on the heap instead of the stack (which is much more limited). Don't use new! This is obviously a C program so you need to use malloc. You should also free() the array at the end of the program.
inttypes.h includes stdint.h, so you only need to include inttypes.h.
You should use SCNd64 in the scanf (not that it will make a difference, but it's the proper thing to do).
I am unable to replicate the error with random numbers in place of your file.
The code runs fine and generates output etc.
Conclusion: there is data in your file that makes it crash sometimes.
Solution: print the current data on the screen with a flush until it crashes. Then debug LCM in a stub program using that input.
@maryt,
Do you have any idea how large the lcm of 50000 numbers can be?
If I take just 3, 5, 7, 11 the lowest common multiple is already 1155. Do you really have large enough integers?
I totally agree, but that would not trigger a FP exception. Integer overflow, yes, but FP? And overflow is default a warning, I think, and usually runs happily to give the wrong answer...
However, with a testcase of 50000 numbers in total
Just how big do you expect that number to be? The LCM of the numbers from 1 to 43 is too big to fit in a 64 bit integer. The LCM of the numbers 1-300 is 431 digits long.
What is the actual problem you're trying to solve? There is probably an easier way.
Do I get this error because my algorithm isnt the best afterall?
I tried to divide the findlcm into 3 smaller functions and i still get the same error.
Lets say I have 10 integers
1 2 3 4 5 6 7 8 9 10 which i store in an array. i traverse the array both ways (because i want my program to be fast otherwise it runs extremely slow for big numbers). Each time for some i i exclude an element. lets say if i exclude 7 i get the minimum lcm than any other number i exclude
If i send u via mail the testcase with 50000 numbers can u find the bug for me?
@dhayden
i think the problem is that my algorithm is unefficient for 50000 input
.for 1 to 43 it is to big but can be calculated perfectly same for 100.
So i need to find a better algorithm. I assume i need again both 2 traversal for the matrix of elements (one from the first and one from the last element ----> 2 loops) and maybe store the lcm values in another element to find the minimum lcm.