Results 1 to 4 of 4

Math Help - Algorithm Trace

  1. #1
    Newbie
    Joined
    May 2007
    Posts
    23

    Algorithm Trace

    In the following algorithm, how many times is the line "z := z + 1" executed if n = 4 and z is an integer?
    procedure algor(z,n)
    i = 1
    while i <= 4 do
    begin
    z = z +1
    n = n – 2
    if n > 0 then
    algor(z,n)
    i = 1 + 1
    end
    return(z)
    end algor

    Thanks for the help.
    Pwr
    Last edited by pwr_hngry; May 22nd 2007 at 05:32 PM. Reason: Image did not paste correctly
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor

    Joined
    Aug 2006
    Posts
    18,607
    Thanks
    1574
    Awards
    1
    Quote Originally Posted by pwr_hngry View Post
    In the following algorithm, how many times is the line "z := z + 1" executed if n = 4 and z is an integer?

    Thanks for the help.
    Pwr
    As shown, the answer is zero.
    There is no n in the problem.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    May 2007
    Posts
    23
    Sorry about that. For some reason the image showed up in the Preview but never made it to the final post. Sorry.

    Pwr
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by pwr_hngry View Post
    In the following algorithm, how many times is the line "z := z + 1" executed if n = 4 and z is an integer?
    procedure algor(z,n)
    i = 1
    while i <= 4 do
    .. begin
    .... z = z +1
    .... n = n – 2
    ...... if n > 0 then
    ........ algor(z,n)
    ...... i = 1 + 1
    .. end
    return(z)
    end algor

    Thanks for the help.
    Pwr
    The loop is executed exactly four times and the line in question executed once per trip around the loop with n decremented by 2 each time.

    The line "if n > 0 then" is arrived at with n=2, 0, -2, -4 and so "algor(z,n)" is executed once with n = 2, and in executing this with n=2 the line in question is executed twice, and the line "algor(z,n)" is never reached.

    Hence the line in question is executed 6 times.

    RonL
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Trace problem
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: April 4th 2011, 04:28 PM
  2. trace
    Posted in the Advanced Algebra Forum
    Replies: 2
    Last Post: December 1st 2010, 02:27 PM
  3. determinant and trace
    Posted in the Advanced Algebra Forum
    Replies: 0
    Last Post: February 7th 2010, 05:44 AM
  4. Trace
    Posted in the Advanced Algebra Forum
    Replies: 0
    Last Post: December 14th 2009, 12:08 PM
  5. Proving adj(a^trace)=[adj(a)]^trace?
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: September 24th 2009, 10:55 PM

Search Tags


/mathhelpforum @mathhelpforum