@cww97
2018-04-04T23:18:47.000000Z
字数 1087
阅读 833
可信软件
Weiwen Chen 10152510217
- 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
#include<stdio.h>
int sum(const int &n){
int sum = 0;
for (int i = 0; i <= n; i++){
sum += i;
}
}
int main(){
printf("%d\n", sum(10));
return 0;
}
// small Language
sum
sum := 0;
i:=1;
while i<=n do
res := res + a;
a := a + 1;
end
- 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
#include<stdio.h>
int f[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sumarray(const int &m){
int res = 0;
for (int i = 0; i < m; i++){
res += f[i];
}
return res;
}
int main(){
printf("%d\n", sumarray(10););
return 0;
}
- 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} = ?