# Thread: Help with logic !

1. ## Help with logic !

Code:
```int SEQstart(int n){
int x=0,y=1,place=0,coun=0,result=0,start;
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. 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"? 3. 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. so it should be (x||y ==1 && x||y ==0) ?? 5. Originally Posted by laserlight 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. Originally Posted by noririco 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. Code:
```int SEQstart(int n){

int x,y=0,place=0,coun=0,result=0,start;
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. 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. 9. ( (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) 10. Looks good! You verified that it tested correctly? 11. 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;

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. 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. 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. 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. 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 