Can't figure out bug in my method.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computert3
    New Member
    • Sep 2007
    • 1

    #1

    Can't figure out bug in my method.

    This method is used to pass in an array of number and "count" the run of numbers and return the longest run. I have all of the code figured out but when I tried testing it with an array like :

    1, 2, 2, 3, 3, 3, 4, 4, 4, 4 - it returned 3 as the longest run... so after not being able to figure it out I found this website. Hope someone can help me!

    [CODE=java]public class Numbers
    {
    /**
    Computes the length of the longest run (sequence of
    adjacent repeated values) in an array.
    @param values an array of integer values
    @return the length of the longest run in values
    */
    public int lengthOfLongest Run(int[] values)
    {

    int max = 0;
    int count = 0;
    for(int i=0; i < values.length - 2; i ++)
    {


    if(values[i] == values[i+1] && (i < values.length - 1))
    {
    count ++;
    }
    else
    {
    count ++;
    }

    if(max < count)
    {
    max = count;

    }
    if(values[i] != values[i+1])
    {
    count = 0;
    }


    }

    return max;


    }
    }[/CODE]
    Last edited by Ganon11; Sep 21 '07, 10:39 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by computert3
    [CODE=java]if(values[i] == values[i+1] && (i < values.length - 1))
    {
    count ++;
    }
    else
    {
    count ++;
    }[/CODE]
    This if statement will have the same result no matter what; rethink your reasoning with the else... statement.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by computert3
      This method is used to pass in an array of number and "count" the run of numbers and return the longest run. I have all of the code figured out but when I tried testing it with an array like :

      1, 2, 2, 3, 3, 3, 4, 4, 4, 4 - it returned 3 as the longest run... so after not being able to figure it out I found this website. Hope someone can help me!
      If your array is empty (length == 0) the maximum 'run' of numbers is nul (0) of course.

      If the array is not empty think of a 'mark' index value the 'mark'ed index is where
      a new number started. At the beginning of your loop mark == 0.

      Start looping at position mark+1; the following three situations can occur:

      1) you ran off the array at this position i == array.length; so the current 'run'
      length is i-mark. Update your statistics and stop.

      2) the number is equal to the number at position 'mark'; keep looping.

      3) the number at position i is different from the number at position 'mark'. The
      length of the current run is i-mark, update your statistics and set mark=i.

      That's all there is to it.

      kind regards,

      Jos

      Comment

      Working...