Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>

 int main()
      {
   int prime[10000];
       int i,j;
      for(i=1;i<10000;i++)
    {
   prime[i]=1;
        }
         prime[1]=0;
        for(i=2;i<=100;i++)
       {
       if (prime[i]==1){
        for (j=i;i*j<10000;j++)
         {prime[i*j]=0;}
           }
         }
     for(i=1;i<10000;i++)
            {
        if( prime[i]==1 )
         {
       printf("%4d",i  );
      }

  }
   printf("\n");
   return 0;
}


first the program set the array 1 until 10000 to value 1 then set prime[1] to 0
then this code :
C++
for(i=2;i<=100;i++)
 {
     if (prime[i]==1){
       for (j=i;i*j<10000;j++) {prime[i*j]=0;}
     }


should eliminate and make all the of multiple number
for example 2*2 = 4 and make prime[4] in array become 0
but why the prime[6] also become 0?

why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?

What I have tried:

why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?
Posted
Updated 14-May-17 6:14am
v3

Because you start with index 1 - and it's zero, you set it that way. That means it's "not prime" so you ignore it.
When you enter loop, your index is 2 - and that's still one, so it's prime.
You enter the inner loop, which sets every multiple of the index to zero to indicate they are not prime. Because 2 is the first even number (by definition) that removes all even numbers from your sieve when you set prime[i*j]=0;
So after the first iteration round your outer loop, 1 is not prime, 2 is prime, 3 is prime, 4 isn't, 5 is, and so on. Which is the pattern you are seeing.


I dont understand the inner loop here in first iteration it make all even number become zero? For(j=i;i*j<10000;j++) {prime[i*j]=0} Index i=2 when enter the loop I=j=2 2*2=4<10000 J++ This mean start j=2 then multiply it j=2*2 if it under 10000 then increment j with 1, so after it is 3*3=9(?) i dont understand this part!!

When you write a for loop, it has four parts:
C++
for (initialization; condition; modification)
   execution statement
The execution statement is obvious - it's the code that is executed each time you go round the loop. In your case, it's this:
C++
{prime[i*j]=0;}

The initialization lets you set up starting values for the loop:
C++
j = 1

The condition if tested each time you are about to enter the loop, and if it is true, the loop continues. If it is false, the loop terminates without executing the execution statement again.
i*j<10000
The modification if executed at the end of each execution of the execution statement to set the loop up for the next iteration:
C++
j++

So you start by setting j to the value of i, and repeat the loop until i * j exceeds (or equals) 10000.

Make sense?


ah got it!!!! it means i=2 because the initialization and j =2 because the condition is true 4<10000 it will execute the execution statement then j will be incremented to 3 so i*j<10000 -> 6 <10000 that is why all the value of even number in array become 0 then for i=j=3 it will make the value of multiple 3 become 0 in array and so on?

Dat's de bunny! :thumbsup:


thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!! for the detailed explanation!!!!!!!!!!!!!! hope to find someone like you again everytime im confuse!!!lol
You're welcome!
 
Share this answer
 
v4
Comments
Member 13188016 14-May-17 8:04am    
I dont understand the inner loop here in first iteration it make all even number become zero?
For(j=i;i*j<10000;j++) {prime[i*j]=0}
Index i=2 when enter the loop
I=j=2
2*2=4<10000
J++
This mean start j=2 then multiply it j=2*2 if it under 10000 then increment j with 1, so after it is 3*3=9(?) i dont understand this part!!
Member 13188016 14-May-17 8:09am    
I dont understand the inner loop part
For(j=i;j*i<100000;j++)
{prime[i*j]=0}
It start with i=2
J=2;2*2<10000(is this mean if 4<10000 true will process the code or is it mean that it will increment until reach 10000?); j++
How it remove the even number still dont get it
Member 13188016 14-May-17 8:14am    
Still dont understand the inner loop part
For (j=i;i*j<10000;j++) {prime(i*j)=0}
Means i=j=2
2^2=<10000(means if the condition true will process the latter or will increment j++ until it reach 10000?);j++
And if j is incremented by 1 it should be 3*3=9 so why the even number all was eliminated? Is the elimination occur in the first iteration?
Member 13188016 14-May-17 8:33am    
ah got it!!!!
it means i=2 because the initialization
and j =2
because the condition is true 4<10000 it will execute the execution statement
then j will be incremented to 3
so i*j<10000 -> 6 <10000 that is why all the value of even number in array become 0
then for i=j=3 it will make the value of multiple 3 become 0 in array
and so on?
Member 13188016 14-May-17 8:46am    
thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!! for the detailed explanation!!!!!!!!!!!!!! hope to find someone like you again everytime im confuse!!!lol ><
First of all, indent your code properly, it help to read.
C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int prime[10000];
	int i,j;
	for(i=1;i<10000;i++)
	{
		prime[i]=1;
	}
	prime[1]=0;
	for(i=2;i<=100;i++)
	{
		if (prime[i]==1){
			for (j=i;i*j<10000;j++)
			{
				prime[i*j]=0;
			}
		}
	}
	for(i=1;i<10000;i++)
	{
		if( prime[i]==1 )
		{
			printf("%4d",i  );
		}

	}
	printf("\n");
	return 0;
}

Quote:
why when i debug it for the first time why suddenly all the value of the array become 1 for odd and 0 for even number?? though already set it to 1?? what makes this become 101010 in array?

I don't know what you call 'debug', but using the debugger would show how your code is doing what you see.
it look like when you remove multiples from the of primes, you also remove that prime.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900