Like Tree1Likes
  • 1 Post By Salem

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. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well you could comment out bits of code, then re-run the tests to see how the final result is broken by the omissions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Tried that to no avail

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    MK27 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The only meaningful thing you haven't commented is "best_val". What is it you don't understand?
    Last edited by MK27; 10-03-2011 at 07:27 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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?
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    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. #8
    Registered User poojaraghoo's Avatar
    Join Date
    Oct 2011
    Posts
    2
    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 subscribe to a feed

Similar Threads

  1. XOR Logic Help
    By Yabssato in forum C Programming
    Replies: 8
    Last Post: 09-27-2011, 01:29 PM
  2. Better up my logic
    By C_programmer.C in forum C Programming
    Replies: 11
    Last Post: 01-10-2011, 11:48 AM
  3. i cant understanding this logic
    By nkrao123@gmail. in forum C Programming
    Replies: 6
    Last Post: 10-26-2009, 01:51 AM
  4. Understanding the pseudocode/logic for this program
    By Sholcomb1 in forum C Programming
    Replies: 11
    Last Post: 12-08-2006, 01:07 PM
  5. logic
    By nupe02 in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2003, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21