编程实现:给定一个一维数组,计算并输出此数组(长度为9)中每两个相邻元素的平均值的平方和之和
时间: 2024-05-14 22:15:10 浏览: 80
以下是Python实现:
```python
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum_of_squares = 0
for i in range(len(arr)-1):
avg = (arr[i] + arr[i+1]) / 2
sum_of_squares += avg ** 2
print(sum_of_squares)
```
输出结果为:153.5
解释:每两个相邻元素的平均值分别为1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,它们的平方和为153.5。
相关问题
c语言编程实现:给定一个一维数组,计算并输出此数组(长度为9)中每两个相邻元素的平均值的平方和之和
```c
#include <stdio.h>
#include <math.h>
int main()
{
int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
double sum = 0.0;
for (int i = 0; i < 8; i++) {
double avg = (arr[i] + arr[i+1]) / 2.0;
sum += pow(avg, 2);
}
printf("The sum of squares of averages of adjacent elements is: %lf\n", sum);
return 0;
}
```
阅读全文