calculating the results of loops

This is a discussion on calculating the results of loops within the C Programming forums, part of the General Programming Boards category; Hi.... I am studying in 10th class and also studying for Computer Olimpiad Games. In first exam, there will be ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    5

    Question calculating the results of loops

    Hi....
    I am studying in 10th class and also studying for Computer Olimpiad Games. In first exam, there will be some loop problems which i can solve in some mins but i want to make my speed faster to solve them. So i am asking your ways to solve those loops. p.s:"not the easy ones. As in the example. I can send you more types if you ask for..."

    ----------------------
    example:
    ---------------------------------
    int a,b,c,m=0;
    for(a=1;a<=62;a++)
    for(b=a;b<=62-a;b++)
    for(c=b;c<=31;c++)
    m+=a+b+c;
    ----------------------------------
    what is the last value of m?
    ----------------------
    Last edited by siliconian; 03-05-2006 at 12:08 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The code might be easier to understand if you format it properly:
    Code:
    int a,b,c,m=0;
    for(a=1;a<=62;a++)
        for(b=a;b<=62-a;b++)
            for(c=b;c<=31;c++)
                m=a+b+c;
    You might try running that code through a debugger.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    5
    sorry for not writing my code as you did. I am new at forum and didnt know that if i can do this before i sent my message. By the way, I am asking the calculating way with maths, not compiler. Because in exam, they give us some codes like that and asks:"how many time does the '....' code loop?" or "what is the final value of '...' ".

    Code:
    n=5;
    for(a=1;a<=n;a++,m+=a)
    for(b=1;b<=n;b++,m+=b)
    for(c=1;c<=n;c++,m+=c)
    for such a code, you can easily formulise the result as:
    [(n.(n+1)/2).(n.n+n+1)]

    but in complicated loops, there are more ways to do and to calculate. I am asking to everyone to find the best way at last to use. Let's begin with the loop which i wrote at the beginning of thread.
    Last edited by siliconian; 03-05-2006 at 12:28 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    I don't think I fully understand your question....but if you just look at the loops logically they'll be pretty easy to figure out. For example the one you showed m would equal 31+31+31 for its final value because if the value of c is more than 31 it will never do that final loop, and that means the highest values a and b can be for their loops in order to make the final loop trip is 31. So m's final value would be 93.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    5

    Question

    the question is that: "How can you formulise loops?" "What ways do you try?"
    For example:

    Code:
    n=10;
    m=0;
    for(a=1;a<=n;a++)
               for(b=1;b<=a;b++)
                    m++;
    for this code i do such a thing:
    --------------------------------
    a I b I m
    1 I 1 I 1
    2 I 1 I 2
    I 2 I 3
    3 I 1 I 4
    I 2 I 5
    I 3 I 6
    --------------------------------

    As i was doing such a table, usually something warns me and i can formulise the loops easily. For this quesiton it is [n.(n+1)/2]

    I am asking that if i could formulise every loop or not? And if i can, is there any easy ways to do or is there any resources helps me about this?

    Also asking; if you were me, what could you do to formulise them? What would your way be?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    5
    oh i think nobody want to discuss about it or have idea

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're not supposed to bump threads.

    That is a pretty tough looping construct. Try searching the board.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  2. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  3. strange numbers, while loops questionable?
    By exluddite in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 11:11 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

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