Here is a program i am trying to figure out the logic. I can understand the commented lines but then the rest i do not comprehend. Could someone shine some lights.
Code:
/* Title: Write a program that reads in a sequence of positive integers and prints out the longest
streak of the same value. E.g., a sample run would be as follows:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.
*/
#include <stdio.h>
int main(void)
{
int val; /* Integer input */
int prev = 0; /* previous integer */
int streak = 0; /* streak length of current integer */
int best_val = 0; /* value of best streak */
int best_streak = 0; /* streak length of best integer */
printf("Input numbers: \n");
while (scanf("%d", &val) != EOF) /* Use Ctrl + Z to stop list */
{
if (prev == val) //If previous values are same then start counting
streak++;
else
streak = 1; //If not same, then assume it is only 1
if (streak > best_streak) // The counting value represented by best_streak
{
best_streak = streak;
best_val = val;
}
prev = val;
}
printf("Streak of %d %d's in a row.\n", best_streak, best_val); //prints the count for long streak and its value.
return 0;
}
thanks