[关闭]
@cww97 2018-04-04T15:18:47.000000Z 字数 1087 阅读 741

Exercise

可信软件


Weiwen Chen 10152510217

Exercise 1

  • Write a program in our language to compute the sum of natural numbers from 0 to n
  • Write the corresponding program in C (with n = 10)
  • Write the complete program in C, printing result and executing it.
  • Use an approach similar to the one used in the example below
  1. #include<stdio.h>
  2. int sum(const int &n){
  3. int sum = 0;
  4. for (int i = 0; i <= n; i++){
  5. sum += i;
  6. }
  7. }
  8. int main(){
  9. printf("%d\n", sum(10));
  10. return 0;
  11. }

// small Language

  1. sum
  2. sum := 0;
  3. i:=1;
  4. while i<=n do
  5. res := res + a;
  6. a := a + 1;
  7. end

Exercise 2

  • Translate "sumarray" in C using the same approach as below
  • Take m = 10
  • Initialise the array f with {1,2,3,4,5,6,7,8,9,10}
  • Execute your C program
  1. #include<stdio.h>
  2. int f[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  3. int sumarray(const int &m){
  4. int res = 0;
  5. for (int i = 0; i < m; i++){
  6. res += f[i];
  7. }
  8. return res;
  9. }
  10. int main(){
  11. printf("%d\n", sumarray(10););
  12. return 0;
  13. }

Exercise 3

  • Let f be this function:
    f = {0 7→ 1,1 7→ 2,2 7→ 3,3 7→ 4,4 7→ 4}
  • Give the values of:
    ran(f) = ?
    f[0 .. 4] = ?
    {3} ? f = ?
    f ? {4} = ?
    {3} ? − f = ?
    f ? − {4} = ?

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注