Preview

Concept Programing

Good Essays
Open Document
Open Document
443 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Concept Programing
1. (15%) Prove the following program is correct:
{n > 0} count = n; sum = 0;
P: {n>0 and count = n and sum = 0} while count 0 do sum = sum + count; count = count 1; end Q: {sum = 1 + 2 +… + n}
Please note that you need to do this question in the following steps:
a) Find a candidate loop invariant I. Show the induction steps using which you find I.

Σn k=1 k - Σn k=1k = 0
c)Show {I and B} S {I}
P: {sum + count = n + (n-1) +…. + (count+1) + count} => I: {sum = n + (n-1) +…. + (count+1)} sum = sum + count;
Q: {sum = n + (n-1) +…. + (count+1) + count}
P: {sum = n + (n-1) +…. + ((count-1)+2) + ((count-1)+1) => {sum = n + (n-1) +…. + (count+1) + count} count = count 1;
I: {sum = n + (n-1) +…. + (count+2) + (count+1)}
d)Show (I and (not B)) => Q
(sum = n + (n-1) +…. + (count+2) + (count+1)) and (count = 0) => (sum = n + (n-1) +…. + 2 + 1) = Q
e) Argue that the loop terminates.
Since
1) n > 0 and count = n => count > 0 before the while loop
2) In the while loop, each iteration decrements count by 1
Therefore, count’s value will become 0 after n iterations.
2. Write a denotational semantics mapping function for the following statements in Java
a) (5%) Mi++(++, S)
Mi++(++, S) =
If VARMAP(id, S) = undefined
Then error
Else (V, S’)
Where
V = VARMAP(id, S)
S’ = S[V+1 / id]
b) (5%) M++i(++, S) =
If VARMAP(id, S) = undefined
Then error
Else (V, S’)
Where
V = VARMAP(id, S) + 1
S’ = S[V / id]
c)(10%) Mfor(for ( ; ; ) {}, S)
Assume the semantics mapping functions Massign, Mbexpr, are Mstmts given.
Mfor(for ( ; ; ) {}, S) =
If Massign(, S) = error then error else if Mbexpr(, S’) = error then error else if Mbexpr(, S’) = false then S’ else if Mstmts(, S’) = error then error else if Massign(, S’’) = error then error else Mfor(for( ; ; ) {}, S’’’) where S’ = Massign(, S)
S’’ = Mstmts(, S’)
S’’’ = Massign(, S’’)
3. (15%) Design a state diagram to recognize one form of the comments of the C-based programming languages,

You May Also Find These Documents Helpful

  • Satisfactory Essays

    PT1420 U8 Homework

    • 431 Words
    • 4 Pages

    6. a loop that never hits a max value or that never turns true is infinite…

    • 431 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    1. Explain in words why the equation {n \choose k} = {n \choose n -k} is true.…

    • 527 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Pt1420 Unit 7 Study Guide

    • 582 Words
    • 3 Pages

    17.Write a program to read a binary input from the keyboard and display it. Page: 130…

    • 582 Words
    • 3 Pages
    Satisfactory Essays
  • Powerful Essays

    6. Write a pseudocode statement that declares the variable total so it can hold integers. Initialize the variable with the value 0.…

    • 748 Words
    • 3 Pages
    Powerful Essays
  • Good Essays

    Then comes a number, the number three and given that at the end of the…

    • 660 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    Using Execel Solver

    • 2737 Words
    • 11 Pages

    2 2 2 2 = x 1σ 1 + x 2σ 2 + x 3σ 3 + 2 x 1 x 2σ 12 + 2 x 1 x 3σ 13 + 2 x 2 x 3σ 23 2 2…

    • 2737 Words
    • 11 Pages
    Powerful Essays
  • Satisfactory Essays

    Series and Ln

    • 355 Words
    • 2 Pages

    1 1 1 1 1 + + + :::: 3 2 4 3 5 1 1 1 1 1 + + + k 2 k k 1 k+1 k 1 1 1 = 1+ + 2 k+1 k+2 1 ! 3 2…

    • 355 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    log (n!) = log 1 +log 2 +... + log n log (n/2)+ log (n/2+1)+...+log (n)…

    • 350 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Fermat's Little Theorem

    • 488 Words
    • 2 Pages

    This is a generalization of the familiar binomial theorem to the case where the sum of n terms ( a1 + a2 + + an ) is raised to the power p. In (1), the sum is taken over all , kn such that k1 + k2 + + kn = p .…

    • 488 Words
    • 2 Pages
    Good Essays
  • Better Essays

    Boolean Expression

    • 1050 Words
    • 5 Pages

    = x1 x2 x3 + x1 x2 x3 + x1 x2 x3 + x1 x2 x3 + x1 x2 x3 + x1 x2 x3…

    • 1050 Words
    • 5 Pages
    Better Essays
  • Good Essays

    Palindrome

    • 1937 Words
    • 8 Pages

    PALINDROME #include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) = = 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; } PRIME NUMBER OR NOT #include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n%c == 0 ) { printf("%d is not prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); return 0; } ARMSTRONG OR NOT #include <stdio.h> int main() { int number, sum = 0, temp, remainder; printf("Enter an integer\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number.\n"); else printf("Entered number is not an armstrong number.\n"); return 0; } OR #include<stdio.h> void main() { int num,num1,arms=0,rem; printf("Enter the number:\n"); scanf("%d",&num); num1=num; while(num>0) { rem=num%10; arms=arms+rem*rem*rem; num=num/10; } if(num1==arms) { printf("…

    • 1937 Words
    • 8 Pages
    Good Essays
  • Good Essays

    1. Write a program in Java that allows the user to enter a number and then display its Pascal’s Triangle -The triangle is bordered by ones on the right and left sides, and each interior entry is the sum of the two entries above.…

    • 1391 Words
    • 6 Pages
    Good Essays
  • Satisfactory Essays

    Medagenda

    • 385 Words
    • 4 Pages

    REM a, b, n parameters represents first term, second term and number of terms respectively…

    • 385 Words
    • 4 Pages
    Satisfactory Essays
  • Powerful Essays

    Stellar Numbers

    • 2893 Words
    • 12 Pages

    7 + (1 + 2 + 3 + 4 + 5 + 6) = 28…

    • 2893 Words
    • 12 Pages
    Powerful Essays
  • Satisfactory Essays

    CS202 Lecture Notes

    • 302 Words
    • 2 Pages

    • An algorithm is a set of instructions to be followed to solve a problem.…

    • 302 Words
    • 2 Pages
    Satisfactory Essays