1.测试如下程序段: main() { int i=1;n1=n2=0; float sum=0; float average; float score[100]; while(score[i]!=-1&&n2<50) { n2=n2+1; if(score[i]>0&&score[i]<100) { n1=n1+1; sum=sum+score[i]; } i=i+1; } if(n1>0) average=sum/n1; else average=-1; printf(“n1=%d,sum=%f,average=%f\n”,n1,sum,average); } 要求:①画出这段代码的程序流程图和控制流程图;②计算环路复杂度;③列出独立路径;④设计测试用例。
时间: 2023-12-04 10:02:52 浏览: 169
1. 程序流程图如下:
```
+--------------+
| 初始化 i, n1, |
| n2, sum |
+--------------+
|
v
+--------------+
| 初始化 average|
+--------------+
|
v
+--------------+ +----------------+
| 初始化 score | | score[i] != -1 |
+--------------+ | && n2 < 50 |
| +----------------+
v |
+--------------+ |
| while循环 | |
+--------------+ |
| |
v v
+--------------+ +----------------+
| 判断 | | score[i] > 0 && |
| score[i] |-----> | score[i] < 100 |
| 是否 | +----------------+
| 合法 | |
+--------------+ v
| +----------------+
v | n1 = n1 + 1 |
+--------------+ +----------------+
| 计算总和 | |
+--------------+ |
| v
v +----------------+
+--------------+ | sum = sum + |
| 计算平均值 |-----> | score[i] |
+--------------+ +----------------+
| |
v v
+--------------+ +----------------+
| 判断是否有 | | i = i + 1 |
| 合法数据 | +----------------+
+--------------+ |
| v
v +----------------+
+--------------+ | 循环结束 |
| 计算平均值 | +----------------+
+--------------+ |
| v
v +----------------+
+--------------+ | 计算 average |
| 打印结果 | +----------------+
+--------------+
```
2. 控制流程图如下:
```
+--------+ +---------+
| 开始 |----------> 初始化 |
+--------+ +---------+
| |
v |
+--------------+ |
| 初始化 average| |
+--------------+ |
| |
v |
+--------------+ |
| 初始化 score | |
+--------------+ |
| |
v |
+--------------+ |
| while循环 | |
+--------------+ |
| |
v |
+--------------+ |
| 判断 | |
+--------------+ |
| |
v |
+--------------+ |
| 计算总和 | |
+--------------+ |
| |
v |
+--------------+ |
| 计算平均值 | |
+--------------+ |
| |
v |
+--------------+ |
| 打印结果 | |
+--------------+ |
| |
v |
+--------+ +---------+
| 结束 |<---------| 退出程序|
+--------+ +---------+
```
3. 环路复杂度为 6。
4. 独立路径:
- 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 → 15 → 16 → 17 → 18
- 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 → 16 → 17 → 18
- 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 → 16 → 15 → 10 → 11 → 12 → 13 → 14 → 15 → 16 → 17 → 18
- 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 → 16 → 15 → 10 → 11 → 12 → 13 → 14 → 16 → 17 → 18
注:路径中的数字表示程序流程图中的节点编号。
5. 设计测试用例:
- 测试用例1:
输入:score = {-1}
输出:n1 = 0, sum = 0.000000, average = -1.000000
- 测试用例2:
输入:score = {101}
输出:n1 = 0, sum = 0.000000, average = -1.000000
- 测试用例3:
输入:score = {80, 90, 95, 85, -1}
输出:n1 = 4, sum = 350.000000, average = 87.500000
- 测试用例4:
输入:score = {80, 90, 95, 101, -1}
输出:n1 = 3, sum = 265.000000, average = 88.333336
- 测试用例5:
输入:score = {80, 90, 95, 85, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, -1}
输出:n1 = 8, sum = 550.000000, average = 68.750000
阅读全文