用C语言求一个数组内数据的平方和
时间: 2024-04-08 21:33:06 浏览: 12
Sure! Here is a simple C program that calculates the sum of squares of elements in an array:
```c
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Sample array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i] * arr[i]; // Square each element and add it to the sum
}
printf("Sum of squares: %d\n", sum);
return 0;
}
```
In this example, we have an array `arr` of size 5 with some sample values. We then iterate over each element of the array, square it using the `arr[i] * arr[i]` expression, and add it to the `sum` variable. Finally, we print the sum of squares.
You can modify the array `arr` and its size as per your requirements.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)