0

My function is supposed to have a successful start sequence when ever it encounters 0 0 0 0 1 1 0 but when i input these numbers the number of successful start sequence does not change However that doesn't stop it from compiling and I can't spot where the mistake Ive made is.

main()

{
    int i,num;
    int array[300];
    i=0;
    num=0;
    while(i<100)
    {
        scanf("%d",&array[i]); //input


        i++;
        //checks for start sequences while making sure there is atleast 8 numbers input
        if((i>=8)&&((array[i-1])==0)&&((array[i-2])==1)&&((array[i-3])==1)&&((array[i-4])==0)&&     ((array[i-5])==0)&&((array[i-6])==0)&&((array[i-7])==0))
        {
            num++;//counts the number of successful startsequences

        }
    printf("Number of valid start sequences is %d\n",num);
    }
}
Natasha Dutta
  • 3,242
  • 21
  • 23
  • why would you use that many superfluous parentheses? – The Paramagnetic Croissant Dec 03 '14 at 06:32
  • 1
    Maybe change `i>=8` to `i>=7`? – JS1 Dec 03 '14 at 06:34
  • this line: 'main()' will raise a compiler warning (you should have all warnings enabled), so the code did not successfully compile. Note: the line should be: 'int main()' even with the incorrect declaration of the main function, the compiler would have raised warning about the missing 'return( intValue )' statement. – user3629249 Dec 03 '14 at 07:48
  • regarding this line: scanf("%d",&array[i]); 1) the format string needs a leading ' ' so any white space in the stdin buffer would have been consumed. otherwise, the second pass through the loop would (probably) fail. 2) the returned value from any input function (scanf) needs to be checked to assure that operation was successful – user3629249 Dec 03 '14 at 07:52

2 Answers2

4

You're facing off-by-one error.

Remember, the element number n in array is marked by n-1 the index.

for example,

if((i>=8)&&((array[i-1])==0)&&((array[i-2])==1)&&((array[i-3])==1)&&((array[i-4])==0)&&     ((array[i-5])==0)&&((array[i-6])==0)&&((array[i-7])==0))

never checksarray[0] element, does it?

Maybe, you want to start with changing if((i>=8) to if((i>=7)

Natasha Dutta
  • 3,242
  • 21
  • 23
  • 1
    @JaleelKibs: Also, after you fix the off-by-one error, consider if the following sequence should count as two successful start sequences or not: `0 0 0 0 1 1 0 0 0 0 1 1 0` (I don't know the answer to this - it depends on the requirements of your application). If you only make the simple fix mentioned in this answer then your program will count it as two start sequences even though the last zero of the first sequence is used as the first zero of the second sequence. – Michael Burr Dec 03 '14 at 06:47
1
this line that checks for the sequence,
which is probably where the problem is located
is very difficult to read.
suggest writing it like so:

if(   (i>=8)
   && (0 == array[i-1])
   && (1 == array[i-2])
   && (1 == array[i-3])
   && (0 == array[i-4])
   && (0 == array[i-5])
   && (0 == array[i-6])
   && (0 == array[i-7]))


now that the line is readable, it looks like the array offsets are not correct.
and when i = 8, then 8 items have been read, 
and the code is only checking the last 7 inputs
so to not miss the first possible matching sequence: 

I suspect the line should be:

if(   (i>=7)
   && (0 == array[i-0])
   && (1 == array[i-1])
   && (1 == array[i-2])
   && (0 == array[i-3])
   && (0 == array[i-4])
   && (0 == array[i-5])
   && (0 == array[i-6]))
user3629249
  • 16,402
  • 1
  • 16
  • 17