# Understanding some Logic

This is a discussion on Understanding some Logic within the C Programming forums, part of the General Programming Boards category; Here is a program i am trying to figure out the logic. I can understand the commented lines but then ...

1. ## Understanding some Logic

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

2. Well you could comment out bits of code, then re-run the tests to see how the final result is broken by the omissions.

3. Tried that to no avail

4. So you didn't comment out line 24, and see something odd happen?

\$ ./a.out
Input numbers:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 8 6's in a row.

Or comment out line 22

\$ ./a.out
Input numbers:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 1 2's in a row.

Yeah you're right - it's hopeless isn't it.

> I can understand the commented lines but then the rest i do not comprehend.
Add some more comments, post them here, then we can tell you whether you really understand what is going on, or are just able to parse English sentences.

5. The only meaningful thing you haven't commented is "best_val". What is it you don't understand?

6. OK, to tell you that I saw eleven VW beetles on the street in a row today, I need to know there were eleven of them... and what else?

7. That there were eleven cars BUT in a consecutive row? right? Line 32 confirms if they are counted in a consecutive way. Am i right?

8. hello i have the same number to do as a small assignment but i am unable to write it. here i can c the solution but i dnt understand the logic or how u reached to the result..can u please help me out...?

Popular pages Recent additions