六个组数,第一组有3个数值,第二组有5个数值,第三组有7个数值,第四组有9个数值,第五组有11个数值,第六组有13个数值,分别输入每组的的每个数值,并求出每组数的平均值,请用C语言解决这个问题。
时间: 2024-04-09 20:29:01 浏览: 59
C语言用自己输入的数组求平均数
5星 · 资源好评率100%
Sure! Here's a C code snippet to solve your problem:
```c
#include <stdio.h>
int main() {
int numGroups = 6;
int groupSizes[] = {3, 5, 7, 9, 11, 13};
int i, j;
float sum, average;
int groups[numGroups][groupSizes[numGroups-1]]; // Define a 2D array to store the input numbers
// Input the numbers
for (i = 0; i < numGroups; i++) {
printf("Enter %d numbers for group %d:\n", groupSizes[i], i+1);
for (j = 0; j < groupSizes[i]; j++) {
scanf("%d", &groups[i][j]);
}
}
// Calculate and display the average for each group
for (i = 0; i < numGroups; i++) {
sum = 0;
for (j = 0; j < groupSizes[i]; j++) {
sum += groups[i][j];
}
average = sum / groupSizes[i];
printf("Average for group %d: %.2f\n", i+1, average);
}
return 0;
}
```
You can compile and run this code in a C compiler and input the numbers for each group. It will calculate and display the average for each group.
阅读全文