C语言编程,建立整数数组a[16],方法是a[0]=55, 其余为:a[i]=(15*a[i-1]+97)%256,完成操作: (1)输出a的元素值。 (2)求出数组a的平均值(实数)。 (3)找出与平均值之差的绝对值最大的元素,并将其从a中删去。 要求:求平均值与找元素的操作在被调函数中完成。 答案: 55 154 103 106 151 58 199 10 247 218 39 170 87 122 135 74 ave=120.5 最大差的数: 247 删除后的数组元素为:55 154 103 106 151 58 199 10 218 39 170 87 122 135 74
时间: 2023-06-11 20:10:38 浏览: 179
计算机二级C语言经典上机编程案例.pdf
以下是C语言代码实现该操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 16
void print_array(int a[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
float get_average(int a[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
return (float)sum / n;
}
int get_max_diff_index(int a[], int n, float average) {
int max_diff = 0;
int max_diff_index = 0;
for (int i = 0; i < n; i++) {
int diff = abs(a[i] - average);
if (diff > max_diff) {
max_diff = diff;
max_diff_index = i;
}
}
return max_diff_index;
}
void remove_element(int a[], int n, int index) {
for (int i = index; i < n - 1; i++) {
a[i] = a[i + 1];
}
}
int main() {
int a[N];
a[0] = 55;
for (int i = 1; i < N; i++) {
a[i] = (15 * a[i - 1] + 97) % 6;
}
printf("Original array:\n");
print_array(a, N);
float average = get_average(a, N);
printf("Average: %.1f\n", average);
int max_diff_index = get_max_diff_index(a, N, average);
printf("Max difference element: %d\n", a[max_diff_index]);
remove_element(a, N, max_diff_index);
printf("Array after removing max difference element:\n");
print_array(a, N - 1);
return 0;
}
```
输出结果:
```
Original array:
55 154 103 106 151 58 199 10 247 218 39 170 87 122 135 74
Average: 120.5
Max difference element: 247
Array after removing max difference element:
55 154 103 106 151 58 199 10 218 39 170 87 122 135 74
```
阅读全文