One way to find the longest sequence of the same value in an array and report the value is to use a while
loop along with the max
function. Here's an example code:
main.m882 chars32 lines
In this code, we initialize three variables: max_value
, max_count
, and count
. We then traverse the array using a while
loop, and count the length of each sequence of the same value. When we encounter a different value, we compare the current sequence count with the previous maximum count, and update the maximum count and value if the current sequence is longer. Finally, we display the maximum sequence value and count using the fprintf
function.
For the input array [1 1 1 2 2 3 3 3 3 4 5 5 5]
, the output would be:
main.m57 chars2 lines
gistlibby LogSnag