Thread: Help with logic !

  1. #1
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90

    Help with logic !

    Code:
    int SEQstart(int n){
     int x=0,y=1,place=0,coun=0,result=0,start;
     printf("Please enter binary seq size:%d\n",n);
     while (place < n)   
     {
      while (x&&y==1 || x&&y==0)  //// x,y can be only 1,0  
      {
       if (x==y) coun++;    //// coun=0 first itration
       else coun=0;         ////
        
          scanf("%d",&x);      //// getting REAL x for first time so place ++
       place++;
       
       if (coun>result)     //// (0) first itration
       {
        result=coun;    
        start=place-1;
       }
       scanf("%d",&y);      //// getting REAL y first time so place ++ and now 2
       place++;
      }
     }
     return start;
    }
    int main (){
     int n=12;
     printf("%d\n",SEQstart(n));
     return 0;
    }
    I think i might have a problem with logic... any help ?

    input: binary seq size n (110001111010,n=12)
    output: staring place of biggest == seq (110001111010) -->place:6

    no arrays allowed.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is SEQstart supposed to do? I see that you wrote that the output should be "staring place of biggest == seq", but that doesn't make sense to me, even if I assume that by "staring" you meant "starting". What is the "staring place of biggest == seq"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    while (x&&y==1 || x&&y==0)  //// x,y can be only 1,0
    This line of code isn't doing what you expect. The logic will always evaluate to true.

    Logical operators (e.g. ||) only see two values - false (zero) or true (non-zero). They return false (zero) or true (one) depending on the outcome.

  4. #4
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    so it should be (x||y ==1 && x||y ==0) ??

  5. #5
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    Quote Originally Posted by laserlight View Post
    What is SEQstart supposed to do? I see that you wrote that the output should be "staring place of biggest == seq", but that doesn't make sense to me, even if I assume that by "staring" you meant "starting". What is the "staring place of biggest == seq"?
    biggest seq. 110011110 <------biggest seq start there

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by noririco View Post
    so it should be (x||y ==1 && x||y ==0) ??
    Same issue, except now it will always evaluate to false.

    Code:
    /*
        't' means true
        'f' means false
    */
    
    /*--------*/
    
    x == false
    y == false
    
    (x||y == 1 && x||y == 0)
    (f||f == 1 && f||f == 0)
    (f == 1 && f == 0)
    (f && t)
     = false
    
    /*--------*/
    
    x == false
    y == true
    
    (x||y == 1 && x||y == 0)
    (f||t == 1 && f||t == 0)
    (t == 1 && t == 0)
    (t && f)
     = false
    
    /*--------*/
    
    x == true
    y == false
    
    (x||y == 1 && x||y == 0)
    (t||f == 1 && t||f == 0)
    (t == 1 && t == 0)
    (t && f)
     = false
    
    /*--------*/
    
    x == true
    y == true
    
    (x||y == 1 && x||y == 0)
    (t||t == 1 && t||t == 0)
    (t == 1 && t == 0)
    (t && f)
     = false
    You need to check each variable for each possible expected value. Two variables, with two possible allowable values, means you'd need four checks, cemented with the appropriate logic.

  7. #7
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    Code:
    int SEQstart(int n){
    
    	int x,y=0,place=0,coun=0,result=0,start;
    	printf("Please enter binary seq size:%d\n",n);
    	scanf("%d",&x);      
    	place++;
    	while (place < n)   
    	{
    		while ( (x&&y) == (1||0) )  //// x,y can be only 1,0  
    		{
    			scanf("%d",&y);      
    			place++;
    
    
    			if (x==y) coun++;    
    			else coun=0;         
    				
    			if (coun>result)     
    			{
    				result=coun;    
    				start=place-coun;
    			}
    
    
    			x=y;
    		}
    	}
    	return start;
    }
    
    
    int main (){
    
    
    	int n=12;
    	printf("%d\n",SEQstart(n));
    	return 0;
    }
    fixed a little bit ... what is the problem now ?
    i can insert numbers but when finishing loop nothing happen.. like no return

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The logic in the argument to your "while()" is still incorrect (note I haven't checked the rest of your code yet).

    Let's assume we just want to check the value of 'x'. Say it out loud: "We want the loop to execute while x is equal to zero or x is equal to one". Can you put that into code? If so, can you expand it to also check the value of 'y'?

    If you're not certain, it is a good idea to create a little "scrap" program to test these things. You can try out the logic and fix it until it works. Then you can use that tested logic in your real program.

    Something like this, perhaps:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0;
        int y = 0;
    
        while(/* logic here */)
        {
            printf("\nboth 'x' and 'y' are valid.\n");
            printf("enter a new 'x' and 'y' (separated by spaces)\n");
            scanf("%d%d",&x,&y);
        }
    
        printf("\nthe value of 'x' and/or 'y' is not valid\n");
    
        return 0;
    }
    Fill in the blank and test your logic with different numbers (I omitted error-checking, so don't enter any letters!) and see the results.
    Last edited by Matticus; 12-03-2013 at 09:54 AM.

  9. #9
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    ( (x==0 || x==1) && (y==0 || y==1) )

    x can be 0||1 AND y can be 0||1

    by the way, this while loop is inside a bigger while loop so it is suppose to end when (place>=n)
    Last edited by noririco; 12-03-2013 at 10:20 AM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Looks good! You verified that it tested correctly?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Alright, the next step is to clean up your code a bit so it's readable. In your function, I placed each variable declaration on its own line - the way you did it was not wrong, I just personally find it more readable this way. Also, I changed the variable "coun" to "count" - the gains of shortening it by one letter is not worth the the sacrifice of clarity. And, of course, I improved the indentation.

    Code:
    #include <stdio.h>
    
    int SEQstart(int n)
    {
        int x = 0;
        int y = 1;
        int place = 0;
        int count = 0;
        int result = 0;
        int start;
    
        printf("Please enter binary seq size:%d\n",n);
    
        while (place < n)
        {
            while ((x==0 || x==1) && (y==0 || y==1))  //// x,y can be only 1,0
            {
                if (x==y) count++;    //// coun=0 first itration
                else count=0;         ////
    
                scanf("%d",&x);      //// getting REAL x for first time so place ++
                place++;
    
                if (count>result)     //// (0) first itration
                {
                    result = count;
                    start = place-1;
                }
    
                scanf("%d",&y);      //// getting REAL y first time so place ++ and now 2
                place++;
            }
        }
    
        return start;
    }
    
    int main ()
    {
        int n = 12;
    
        printf("%d\n",SEQstart(n));
    
        return 0;
    }
    You have some fundamental problems with your program. For instance, the "while()" loop that verifies the input also acts on the input before it is verified. Also, you ask the user to enter a binary length, but have that value hard-coded in the program (unless that's only for testing purposes). In general, it seems very "all over the place" - I'm having trouble interpreting your intent.

    It might be worthwhile to start over, but this time, don't start by writing code. Think of the necessary steps you need to take to solve the problem. Use a pencil and paper to help visualize the process. Write down each step you would have to take to arrive at a solution. These steps will then form the foundation of your logic. They can be translated into pseudo-code, and then into code. When you start writing your code, just write a little bit at a time (5 - 10 lines), compile and test. Add some more code, compile and test. Use a methodical approach to help ensure clean, correct code.

    Can you explain, in words, how you would go about solving this problem?

  12. #12
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    yes it is good but it seems the loop acts forever ...

    if i have a while loop inside a while loop so the outer loop> inner loop.

    place counter increases but the loop go forever .... :/ WTF?

  13. #13
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    Code:
    int SEQstart(int n){
    
    	int x;
    	int y=0;
            int index=1;
    	int count=1;
    	int result=1;
    	int start=0;
    	
    	printf("Please enter binary seq of %d digits\n",n);
    	scanf("%d",&x);      
    	while (index < n)   
    	{
    		while ( (x==0 || x==1) && (y==0 || y==1) )  //// x,y can be only 1,0  
    		{
    			scanf("%d",&y);      /// now I have the users x,y
    			index++;             /// index == 2
    			if (x==y) count++;   /// if 11 count == 2
    			else count=1;        /// if 10 count == 1
    				
    			if (count>result)    /// happens only if i have greater count than before  
    			{
    				result=count;    
    				start=index-count+1;  /// 2-2+1 ---> staring place of seq is at index 1
    				x=y;  /// y become first number
    			}
    
    
    			x=y;
    		}              //// we go back and scan a number to y
    	}
    	return start;
    }
    
    
    int main (){
    
    
    	int n=12;
    	printf("%d\n",SEQstart(n));
    	return 0;
    }

  14. #14
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    Code:
    int SEQstart(int n){
    
    	int x;
    	int y=0;
        int index=1;
    	int count=1;
    	int result=1;
    	int start=0;
    	
    	printf("Please enter binary seq of %d digits\n",n);
    	scanf("%d",&x);      
    	while (index < n)   
    	{
    		while ( (x==0 || x==1) && (y==0 || y==1) )  //// x,y can be only 1,0  
    		{
    			scanf("%d",&y);      /// now I have the users x,y
    			index++;             /// index == 2
    			if (x==y) count++;   /// if 11 count == 2
    			else
    				{
    				  if (count>result)    /// happens only if i have greater count than before  
    		        	{
    				       result=count;    
    				       start=index-count+1;  /// 2-2+1 ---> staring place of seq is at index 1
    				    }	
    				  count=1;
    			    }
    			  x=y;
    		}              //// we go back and scan a number to y
    	}
    	return start;
    }
    
    
    int main (){
    
    
    	int n=12;
    	printf("%d\n",SEQstart(n));
    	return 0;
    }

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I pretty much stop responding when you don't heed advice and instead continuously spew broken code.

    For whatever it's worth, I'll offer one more thing for your consideration.

    If you're entering the entire binary number on a single line with no spaces (i.e. 110001111010), what do you think scanf("%d",...) will try to read?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better up my logic
    By C_programmer.C in forum C Programming
    Replies: 11
    Last Post: 01-10-2011, 12:48 PM
  2. What is the logic?
    By beycan in forum C Programming
    Replies: 3
    Last Post: 10-23-2010, 05:10 AM
  3. need help with logic
    By happyclown in forum C Programming
    Replies: 5
    Last Post: 02-11-2009, 05:12 AM
  4. Some logic help please
    By Herz in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2008, 07:48 AM
  5. Logic???
    By planet_abhi in forum Game Programming
    Replies: 1
    Last Post: 08-08-2003, 07:59 AM