Thread: Problems in the Code with if command.

  1. #16
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    here the 2nd if that is,if(prime) is not part of the for loop.
    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&&amp;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;
    
    }
    correct one

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i=0; i<counter&&amp;amp;prime; i++)
    Do you even bother to test, or are you just copy/pasting dodgy HTML?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by Salem View Post
    > for(i=0; i<counter&&amp;amp;prime; i++)
    Do you even bother to test, or are you just copy/pasting dodgy HTML?
    I Have given an analysis of this algorith and an addition second code and you still really believe that I am copying something to paste here?
    Did you read the entire thread before respond ?

    Anyway , thnak you guys. I will find the solution alone.
    I just asked if anyone knows the flow.. The flow is my problem , the particular flow and not
    to read general articles from Wikipedia .
    Thank you.
    Last edited by Mr.Lnx; 10-03-2011 at 05:53 AM.

  4. #19
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Because , I didn't understand the first code... I tried to make a more simple code with if's and while... (the first code that I have posted here is from BOOK it is not mine ) .

    Code:
    while(x<3)
    {
    	y=1;
    	for(; i<2; i++)
    	if(x)
    	y=0;   // 
    	printf("\n%d",y); 
    	if(y)
    	{
    	printf("\nHello FOlks");
    	printf("\nHow are you");
    }
    x++;
    }
    Because I dont understand the flow of the original algorithmo (which originates from Book and not from me) I am attempting to create a more simple code to understand and I am using a printf to show me the values... first time y->0 but second y->1... And I dont understand why.
    I tried to explain that is my original problem with the code.... (that I didn't understand why second if works ) only this.
    Last edited by Mr.Lnx; 10-03-2011 at 06:15 AM.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Basically I can't understand the flow of algorithm ,
    Well it is pretty tough, when your indentation is poor, and you're lazy with the use of braces.

    For example, this should have 3 separate indent levels, not zero.
    Code:
    	for(; i<2; i++)
    	if(x)
    	y=0;   //

    This is your original code, indented.
    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;
    }
    Notice that I changed the position of the not operator if (!(start % primes[i])). Again, this would have been so much clearer expressed as if ( start % primes[i] == 0 ).

    Now this is your code, with braces in all the places you left them out.
    This is I guess what vikasvijayan tried to do, but made a mess of it.
    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;
    }
    > // How if works ???? prime = 0
    if ( prime ) is merely short-hand for if ( prime != 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok Salem thank you.

    I just looked the second code (not the first)
    and I am trying to apply some test values cases in
    x , y variables In order to understand the flow of the code.

    The brackets has major sense! Anyway.... may I have done wrong to post originally the first code without have the sure knowledge about the if flow... the while flow... etc.... understand me????

    Sorry I am trying to improve my english simultaneously.
    I have some queries about the second code...

  7. #22
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    I am sorry but you were absolutely right .
    I read Sieve of Eratosthenes yesterday and I understand some basic concepts.

    Thank you

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