Thread: Problems in the Code with if command.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Problems in the Code with if command.

    Hello everybody. I have difficulties with a C code

    Code:
     #include<stdio.h>
    
    int main()
    
    {
    int primes[4];
    int counter=1;
    int start=3;
    int prime;
    int i;
    
    primes[0]=2;
    printf("2");
    
    while(counter<4)
    {
    	prime=1;
    	for(i=0; i<counter&&prime; i++)
    	if((!start%primes[i])) 
    		prime=0;   // here is my problem 
    	if(prime)      // How if works ???? prime = 0 
    	{
    		printf("\n%d",start);
    		primes[counter++]=start;  
    	}                            
    	start++;
    }
    printf("\n");
    
    return 0;
    }
    I pose/put the value 0 in prime variable and after that I put it into if
    how works??? From theory I knew that if command stops to work when find a zero value. And now the prime is 0!!!!!

    Thank in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to fix your indentation.
    Code:
    while(counter<4)
    {
        prime=1;
        for(i=0; i<counter && prime; i++)
            if((!start%primes[i]))
                prime=0;   // here is my problem
        if(prime)      // How if works ???? prime = 0
        {
            printf("\n%d",start);
            primes[counter++]=start; 
        }                           
        start++;
    }
    The orange is part of the for loop. The blue is not. Loop through counter times, as long as prime is not zero. After your loop breaks, see if prime is zero or not.

    One thing you should pay attention to is precedence. ! happens before %:
    Code:
    if((!start%primes[i]))
    So what you are really doing is:
    Code:
     if( 0 % primes[ i ] )
    or
    Code:
    if( 1 % primes[ i ] )
    Is that really what you want?


    Quzah.
    Last edited by quzah; 10-01-2011 at 05:11 PM. Reason: color fix
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quzah could I ask you something else first???

    Originally I have found this exercise from one book of Unix ok.

    If the if gives not zero value (the first if) we don't pose (prime=0) ?

    For instance (with pencil and paper)

    before while print -> 2 Hence the first output number is 2!

    Now.... if the first if gives not Zero result => start / primes[i] => start / primes [0 ] => 3/2 = 1 Hence if (!1) => if(0) => gives 0 Hence we dont pose 0 into prime... and we the code will go on this line to elaborate
    =>
    Code:
    if(prime)      
    	{
    		printf("\n%d",start);
    		primes[counter++]=start;  
    	}                            
    	start++;
    Am I right on this thought ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    start = 3
    prime[0] = 2
    i = 0
    
    for( i = 0; i < something && prime != 0; i ++ )
        if( !3 % prime[ 0 ] )
    Turns into:
    Code:
    for( ... )
        if( 0 % 2 )
    !3 happens before %2 does, like I said. C Operator Precedence Table


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    awwww sorry you are right!!!!! The symbol "!"

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Finally i dont understand how works this

    Code:
     
    
    while(counter<3)
    {
    	prime=1;
    	for(i=0; i<counter&&prime; i++)
    	if(!(start%primes[i])) 
    		prime=0;    
    	if(prime)  // second if      
    	{
    		printf("\n%d",start);
    		primes[counter++]=start;  
    	}                            
    	start++;
    }
    My analysis is
    ===========================================
    Originally print the number 2.

    AFter that

    1st (while loop) =>

    prime = 1
    for ( i = 0; i< 1 && 1 ; i ++ )
    if ( ! ( start % primes[0] ) ) => if ( ! ( 3 % 2 ) ) => if ( !(1)) => if (0) (so , we dont pose 0 into prime variable below)

    Hence (in the second if ) => if( 1 )
    print => start => print 3
    primes[1] = 3;
    counter -> 2
    start -> 4

    2nd (while loop)
    prime 1
    (now i = 1 into for loop)
    so for ( i = 1 ; i< 2 && 1 ; i++)
    if ( ! ( start % primes[1] ) ) => if ( ! ( 4 % 3 ) ) => if ( !(1)) => if (0) (so , we dont pose 0 into prime variable below)
    Hence (in the second if ) => if( 1 )
    print => start => print 4
    primes[2] = 4;
    counter -> 3
    start -> 5
    ==========================================

    But I am wrong because the actual output is
    => 2 3 5
    not
    => 2 3 4 :/

    What is wrong in my thought???? :/

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You forgot about 4 % 2.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    You forgot about 4 % 2.


    Quzah.
    4%2 ???

    But primes[1]=3 . Isn't it???
    For the 2nd pass into while loop

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.Lnx View Post
    4%2 ???

    But primes[1]=3 . Isn't it???
    For the 2nd pass into while loop
    Why do you think it would get to the second pass?
    Code:
        for(i=0; i<counter&&prime; i++)
        if(!(4%2))
            prime=0; 

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    Why do you think it would get to the second pass?
    Code:
        for(i=0; i<counter&&prime; i++)
        if(!(4%2))
            prime=0; 

    Quzah.
    Hmmmm because I have created this code before
    Code:
    #include <stdio.h>
    int main()
    {
    
    int i , x , y ;
    i=0 , x=0 , y=1 ;
    
    while(x<3)
    {
    	
    	for(; i<3; i++)
    	{
    	if(i==2)
    	y=0;  
    	if(y)
    	printf("\nHello FOlks");
    	else
    	printf("\nHa");
    }
    	x++;
    }
    	
    	return 0;
    	
    }
    So , while x=0 .....
    for i=0
    while x=1
    for i=1
    etc.......

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.Lnx View Post
    Hmmmm because I have created this code before
    Code:
    ...completely unrelated code...
    What's that have to do with anything in this discussion? You asked why the output wasn't what you expected, I showed you what you weren't seeing. Whatever you are posting now has nothing to do with this discussion.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    What's that have to do with anything in this discussion? You asked why the output wasn't what you expected, I showed you what you weren't seeing. Whatever you are posting now has nothing to do with this discussion.


    Quzah.
    I agree. But the second code it was only for my personal testing before look a more "advanced" example such as the first code that I posted here .
    Anyway I dont understand the 4 % 2. Where do you find it?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First post, line 12. Sixth post, line five. Step through your code.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    First post, line 12. Sixth post, line five. Step through your code.


    Quzah.
    Basically I can't understand the flow of algorithm , if you have time look my analysis and show me there where is my fault :/ Because I am using paper and pencil to understand the logic . The original example is more complexity than others (for instance the second code) for me because it has while , for and two if's simultaneously

    thank in advance

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Line problems
    By jiahwa008 in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2011, 02:41 PM
  2. Run command line code from within my c code.
    By baikal_m in forum C Programming
    Replies: 6
    Last Post: 01-28-2010, 03:58 PM
  3. C code, don't understand a command
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 06-11-2009, 09:41 AM
  4. Command Line Code
    By smitsky in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2004, 07:35 AM
  5. issuing a command using c code
    By DMaxJ in forum Linux Programming
    Replies: 6
    Last Post: 06-28-2003, 01:57 AM