Greetings-
I typed in the following lines of code, which is the answer to one of the questions in Let us C. I think the code was meant for a turbo compiler, and I am using gcc. So I got a core dumped message when I ran it. And then I used GDB to see if I can fix it. Here is the output:
Code:
Reading symbols from Dc...done.
(gdb) break 2
Breakpoint 1 at 0x80484af: file Dc.c, line 2.
(gdb) run
Starting program: /home/ghostrider/programing/Dc
Breakpoint 1, main () at Dc.c:3
3 {
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x08048548 in main () at Dc.c:16
16 num[j] = 0;
So there is something wrong in line 16. Right? But what? Nothing stands out to me from looking at this. I did Google the error, and did not find anything.
Here is the entire code:
Code:
#include <stdio.h>
main ()
{
int num[100], i, j, k, step;
for ( i=0; i<= 99; i++)
num[i] = i+1;
for (i=1; i<=99; i++)
{
if (num[i] != 0)
{
k = num [i] * 2 -1;
step = num[i];
for (i=k; j<=99; j=j+step )
num[j] = 0;
}
}
printf ("\nPrime number between 1 & 100 are \n");
for (i = 0; i<=99; i++)
{
if (num[i] != 0)
printf("%d\t", num[i]);
}
printf ("\n\n\n\n\n\n\nThe end ....");
}