Results 1 to 3 of 3

Math Help - Fixing The Program (Program Correctness)

  1. #1
    Newbie
    Joined
    Mar 2009
    Posts
    6

    Exclamation Fixing The Program (Program Correctness)

    Consider the following buggy program to compute the sum of a sequence of integers a1,...,an with respect to the initial assertion n>=2 and the final assertion that ans = a1+...+1n. It was designed using the loop invariant pans=a1+...+ai) ^ (i <= n).

    Modify the program so that p is truly a loop invariant and then prove it is correct.



    Procedure sum({a1,...,an})
    ans = a1 + a2

    while i <= n - 1
    ans = ans+ ai
    i = i+1
    return ans
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Grandad's Avatar
    Joined
    Dec 2008
    From
    South Coast of England
    Posts
    2,570
    Thanks
    1

    Program correction

    Hello fusion1455

    I'm not quite sure what this means
    Quote Originally Posted by fusion1455 View Post
    ... the loop invariant pans=a1+...+ai) ^ (i <= n).
    and I'm also not too sure how your pseudocode is usually set out, but in this code:
    Procedure sum({a1,...,an})
    ans = a1 + a2

    while i <= n - 1
    ans = ans+ ai
    i = i+1
    return ans
    it seems to me that (a) you haven't initialised i to the value 3, and (b) you haven't indicated where the end of the while loop is.

    Grandad
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Grandad View Post
    Hello fusion1455

    I'm not quite sure what this means
    and I'm also not too sure how your pseudocode is usually set out, but in this code:it seems to me that (a) you haven't initialised i to the value 3, and (b) you haven't indicated where the end of the while loop is.

    Grandad
    It is also not known what value n takes.

    Presumably {a1, ... , an} denotes an arbitary list, but it still seems ill defined, something like:

    Procedure sum(a[],n)

    would be better, and referenced as a[i] , depending on the natrue of the psuedo-code you are using.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Program Correctness Problem
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: March 18th 2009, 06:07 PM
  2. Program Correctness Proof
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: February 24th 2009, 10:12 PM
  3. TI program
    Posted in the Calculators Forum
    Replies: 0
    Last Post: December 22nd 2008, 03:20 AM
  4. TI Program
    Posted in the Calculators Forum
    Replies: 6
    Last Post: May 10th 2008, 04:40 AM
  5. program on TI-84
    Posted in the Calculators Forum
    Replies: 7
    Last Post: August 29th 2006, 06:05 AM

Search Tags


/mathhelpforum @mathhelpforum