tags:

views:

76

answers:

3

Hello,

In a method i am calculating the longest row of elements. The 1-dim array is filled up with random values (0 or 1).
The method looks up the longest row (being 0 or 1, whatever is the longest).
Meaning in for example:

1110100 --> the longest row would be 3 (3 * 1)   
0110000 --> the longest row would be 4 (4 * 0)

The first example has the longest row of 3 elements (3 times 1).

For 1110100 the position in the array would be 0 - 2  (0 - 2 as in the positions on the array)
For 0110000 the position in the array would be 3 - 6  (3 - 6 as in the positions on the array)

I have been trying with foreaches, for loops etc..but i cannot seem to get the proper indexes of both.
Cannot seem to display both positions properly.

For the first example the correct output wouldbe: The largest row of same elements of the array consists of 3 elements on the position 0 -> 2.

The longest row of elements gets of same elements get calculated as the following:

   public int BerekenDeelrij (int [] table)
    (
        int count = 0;
        int value = 0;
        int largest = 0;

        for (int i=0; i < tabel.Length; i++)
        (
            if (value == tabel[i])
                counter + +;
            else
            (
                largest = Math.Max largest (largest, counter);
                final value = value
                count = 1;
            )
        )
        Math.Max return (largest, counter);
    )

Best Regards.

+2  A: 

If you replace the foreach() with a regular for(;;) you immediately have your indices available at the moment you need them.

Henk Holterman
A: 

I'm not posting complete code, since this is tagged homework, but a completely different approach to solving this problem, would be to find all the occurrences of consecutive items, and aggregate them into a new array:

[0, 1, 1, 0, 0, 0, 0] => ["0", "11", "0000"]

Then you could just order that by item.Length, descending, select top 1 item, i.e. "0000"

After that, join the aggregated array

["0", "11", "0000"] => "0110000"

And from there, return joinedResult.IndexOf( longestItem ) and that index + longestItem.Length

David Hedlund
Thanks for the repsonse. Although it makes perfect sence what your saying, i'm afraid i am not that deep into C# yet and looking for a more "direct approach". Regards.
Chris
A: 

This code should work pretty well:

    public int LongestRow(int[] table)
    {
        // Array to store row lenghts
        List<int> rowLenghts = new List<int>();

        // Initialization
        int lastValue = table[0];
        int currentRowLenght = 1;

        // We start at index 1
        for (int i = 1; i < table.Length; i++)
        {
            if (table[i] == lastValue)
            {
                currentRowLenght++;
            }
            else
            {
                rowLenghts.Add(currentRowLenght);
                currentRowLenght = 1;
            }

            lastValue = table[i];
        }

        // We had the last row lenght
        rowLenghts.Add(currentRowLenght);

        return rowLenghts.Max(); ;
    }

This should also work with any type of data (strings, ...) and an unlimited number of possible value (I mean not only 0 and 1, but also 2, 13 and 47).

I think this can be optimized a bit more.

Ucodia
You do know the difference between helping with homework and just doing it?
Henk Holterman
Of course ! But are you sure you will improve much more by finding everything by yourself which implies going in many wrong ways or by getting code sample that you know is working well that you could analyze which in fact could really help in going further ? I approve your point of view but I think there is not only one school for this.
Ucodia
Cheers for the reply, but that method seems to be returning the exact same value as my method posted above. Its not the lenght i need of the row but the first and second position.
Chris
Ok, so I think you need to edit your question because this method has to return an array of two integers indicating the position. And there is another issue: what to do if you have many row with the same lenght? But as all row lengths are stored in a List in the order, it would not be hard finding the start and end index.
Ucodia