将下列PDL语言:PROCEDUREaverage; *最多输人N个值(以一999为输人结束标志),计算位于给定范围内的那些值 (称为有效输人值)的平均值,以及输人值的个数和有效值的个数* INTERFACE RETURNS average,total.input,total.valid; INTERFACE ACCEPTS n,value,minimum,maximum; TYPE value[1,n]IS SCALAR ARRAY; TYPE average,total.input,total.valid,minimum,maximum,sum IS SCALAR,; TYPE n,iIS INTEGER; i=1; total.input=total.valid=0; sum=0; DO WHILE value[i]<>-999 and total.input<n increment total.input by 1; IF value[i]>=minimum AND value[i]<=maximum THEN increment total.valid by 1; sum=sum+value[ i] ELSE skip ENDIF increment iby 1; ENDDO IFtotal.valid>0 THEN average-sum/total.valid; ELSE average--999; ENDIF END average 转换成C语言
时间: 2024-03-06 09:51:53 浏览: 143
pdl语言讲解(设计性程序语言)
以下是将PDL语言转换成C语言的代码:
```c
#include <stdio.h>
void average(int n, int value[], int minimum, int maximum, float *average, int *total_input, int *total_valid) {
int i = 0;
float sum = 0;
*total_input = 0;
*total_valid = 0;
while (value[i] != -999 && *total_input < n) {
(*total_input)++;
if (value[i] >= minimum && value[i] <= maximum) {
(*total_valid)++;
sum += value[i];
}
i++;
}
if (*total_valid > 0) {
*average = sum / *total_valid;
} else {
*average = -999;
}
}
int main() {
int n = 10;
int value[] = {2, 5, 7, 8, 10, -999};
int minimum = 3;
int maximum = 8;
float avg;
int total_input;
int total_valid;
average(n, value, minimum, maximum, &avg, &total_input, &total_valid);
printf("Average: %.2f\n", avg);
printf("Total Input: %d\n", total_input);
printf("Total Valid: %d\n", total_valid);
return 0;
}
```
注意,C语言中没有PDL语言中的INTERFACE和TYPE关键字,需要用其他方式来实现函数参数和类型定义。此外,PDL语言中的increment和-=操作符在C语言中没有直接对应的形式,需要通过+=和-=来实现。
阅读全文