0

Given this example :

int arr[3][7] = {       {1,0,0,1,1,1,1},  //should output 4
                        {0,1,1,1,1,0,1},  //should output 4
                        {0,0,1,1,1,1,1}}; //should output 5

Find the largest sequence containing number 1, and print line index and number of 1.

Do not count total numbers of 1 in each line. Only if they are one after another.

here is my approach :

int main(){
    int i,j,c=0,count=0;

    int arr[3][7] = {   {1,0,0,1,1,1,1},  //output 4
                        {0,1,1,1,1,0,1},  //output 4
                        {0,0,1,1,1,1,1}}; // output 5

    for(i=0; i<3; i++){
        for(j=0; j<7; j++){
            if(arr[i][j] == 1){
                c++;
            } else if( arr[i][j] == 0 && c > count ) {
                count = c;
                c = 0;
            }
        }
        printf("%d\n", count);
    }

  return 0;
}

What i want to get as output now is 4,4,5 but i am getting 1,4,5.

SOLUTION thanks to https://stackoverflow.com/users/1228887/twain249

int main(){
    int i,j,c=0,count=0;

    int arr[3][7] = {   {1,1,0,1,1,1,1},  //output 4
                        {0,1,1,1,1,0,1},  //output 4
                        {0,0,1,1,1,1,1}}; // output 5

    for(i=0; i<3; i++){
        for(j=0; j<7; j++){
            if(arr[i][j] == 1){
                c++;
            } else {
                count = c;
                c = 0;
            }
        }
        if(c > count){
            count = c;
        }
        printf("%d\n", count);
        c=0;
    }
    return 0;
}
5
  • Learn how to debug your small programs ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Feb 25, 2018 at 23:32
  • @zerkms i am beginner in C language and do not know how to write test functions properly. Commented Feb 25, 2018 at 23:38
  • The article also mentions a debugger. If you tried to debug your code you'd spot the problem instantly. Commented Feb 25, 2018 at 23:45
  • Your solution fails case like this { 1,1,1,0,1,1,0 } returns 2 instead of 3 Commented Feb 25, 2018 at 23:51
  • @KillzoneKid yes, the answer below fixed the case Commented Feb 25, 2018 at 23:52

1 Answer 1

1

You forgot to handle the case where the longest sequence is the end of the list

after the inner j loop add the following

if (c > count) {
    count = c;
}

Also you forgot to add a clear after each check.

After the printout add

c = clear = 0;

EDIT: 1 more error. You need to reset c even if the new sequence isn't the longest.

Change the else if into

else if (arr[i][j] == 0) { // If isn't necessary if 0/1 are your only options
{
    if (c > count) {
        count = c;
    }
    c = 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i think if statement inside else is not neccessary since c variable is reseted if it catches an 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.