C program - find largest sequence in array

trying to learn

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;
}
twain249

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the largest sequence in a given array of numbers

From Dev

C program to find missing integer in a sequence of numbers

From Dev

C program to find missing integer in a sequence of numbers

From Dev

Find largest sequence within an arraylist

From Dev

Find the largest sequence of numbers in an integer arraylist

From Dev

How to find largest sequence of a given number in excel?

From Dev

How to find occurrences of the largest integer in C without array?

From Dev

How to find 3 largest even numbers in an array with C?

From Dev

C find the 2 largest numbers and 2 smallest numbers in float array

From Dev

Find largest and smallest number in array using pointers in C

From Dev

C Program to find Windowed Average of a sequence of values, given their timestamps

From Dev

C Program to find Windowed Average of a sequence of values, given their timestamps

From Java

Write a program to find 100 largest numbers out of an array of 1 billion numbers

From Dev

How to find the Largest Difference in an Array

From Dev

Find the Kth largest int in array

From Dev

Find second largest value in array

From Dev

Find largest and smallest numbers in an array

From Dev

C program for child process to find sum of array

From Dev

TSQL program to find the largest prime number

From Dev

C++: Program to find the largest prime factor of a number, what is wrong in my code?

From Dev

C-Program Determine the largest substring in a char array without 'e' or 'E'

From Dev

Quick Way to Find the Largest Array in a Multidimensional Array?

From Dev

Find the beginning and end of largest contiguous sub-sequence sum

From Dev

How do I find largest valid sequence of parentheses and brackets in a string?

From Dev

How to find the largest element of an array of known size?

From Dev

Find the largest value for an array of hashes with common keys?

From Dev

How to find array index of largest value?

From Dev

Find the largest number and its index in the random array

From Dev

Find the smallest and largest value in an array with JavaScript

Related Related

  1. 1

    Find the largest sequence in a given array of numbers

  2. 2

    C program to find missing integer in a sequence of numbers

  3. 3

    C program to find missing integer in a sequence of numbers

  4. 4

    Find largest sequence within an arraylist

  5. 5

    Find the largest sequence of numbers in an integer arraylist

  6. 6

    How to find largest sequence of a given number in excel?

  7. 7

    How to find occurrences of the largest integer in C without array?

  8. 8

    How to find 3 largest even numbers in an array with C?

  9. 9

    C find the 2 largest numbers and 2 smallest numbers in float array

  10. 10

    Find largest and smallest number in array using pointers in C

  11. 11

    C Program to find Windowed Average of a sequence of values, given their timestamps

  12. 12

    C Program to find Windowed Average of a sequence of values, given their timestamps

  13. 13

    Write a program to find 100 largest numbers out of an array of 1 billion numbers

  14. 14

    How to find the Largest Difference in an Array

  15. 15

    Find the Kth largest int in array

  16. 16

    Find second largest value in array

  17. 17

    Find largest and smallest numbers in an array

  18. 18

    C program for child process to find sum of array

  19. 19

    TSQL program to find the largest prime number

  20. 20

    C++: Program to find the largest prime factor of a number, what is wrong in my code?

  21. 21

    C-Program Determine the largest substring in a char array without 'e' or 'E'

  22. 22

    Quick Way to Find the Largest Array in a Multidimensional Array?

  23. 23

    Find the beginning and end of largest contiguous sub-sequence sum

  24. 24

    How do I find largest valid sequence of parentheses and brackets in a string?

  25. 25

    How to find the largest element of an array of known size?

  26. 26

    Find the largest value for an array of hashes with common keys?

  27. 27

    How to find array index of largest value?

  28. 28

    Find the largest number and its index in the random array

  29. 29

    Find the smallest and largest value in an array with JavaScript

HotTag

Archive