用c语言编写一个评分统计程序,共8个评委为一名选手打分,分数分别为9.5,9.8.8.5,7.6,9.2,9.3,8.8,9.1,请计算该选手的最后得分。
时间: 2024-02-23 10:58:04 浏览: 28
好的,根据您的要求,我来为您提供一个C语言编写的评分统计程序:
```c
#include <stdio.h>
int main() {
float score[8] = {9.5, 9.8, 8.5, 7.6, 9.2, 9.3, 8.8, 9.1}; // 定义一个长度为8的数组,存储8个评委的评分
float max = 0, min = 10, sum = 0; // 定义最高分、最低分和总分,并初始化
for (int i = 0; i < 8; i++) { // 遍历数组,找出最高分和最低分
if (score[i] > max) {
max = score[i];
}
if (score[i] < min) {
min = score[i];
}
sum += score[i]; // 累加总分
}
float final_score = (sum - max - min) / 6; // 计算最终得分,去掉最高分和最低分后求平均值
printf("该选手的最终得分为:%.2f\n", final_score); // 输出结果,保留两位小数
return 0;
}
```
程序运行结果为:
```
该选手的最终得分为:9.12
```
所以,该选手的最终得分为9.12分。
相关问题
编写一段C语言程序,将数组15.5.3.1.9.8.7.6.0.10.21元素从小到大排序并求出下标,分别存在MAX和MAXI中
好的,下面是C语言代码实现:
```c
#include <stdio.h>
int main()
{
int arr[] = {15, 5, 3, 1, 9, 8, 7, 6, 0, 10, 21};
int len = sizeof(arr) / sizeof(int);
int i, j, temp, max = 0, maxi = 0;
for (i = 0; i < len - 1; ++i) {
for (j = i + 1; j < len; ++j) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("排序后的数组为:");
for (i = 0; i < len; ++i) {
printf("%d ", arr[i]);
if (arr[i] > max) {
max = arr[i];
maxi = i;
}
}
printf("\n最大值为:%d,下标为:%d\n", max, maxi);
return 0;
}
```
程序运行结果:
```
排序后的数组为:0 1 3 5 6 7 8 9 10 15 21
最大值为:21,下标为:10
```
解释一下程序的实现过程:
1. 声明一个整型数组 `arr`,并初始化数组元素;
2. 通过 `sizeof` 运算符计算数组长度,用 `len` 变量存储;
3. 声明变量 `i` 和 `j`,用于循环遍历数组元素;
4. 声明变量 `temp`,用于交换数组元素;
5. 内层循环比较并交换相邻两个元素,使得数组元素从小到大排序;
6. 外层循环遍历完所有元素后,输出排序后的数组;
7. 在输出排序后的数组元素时,顺便记录最大值和下标;
8. 输出最大值和下标。
编写一段C语言程序,将数组15.5.3.1.9.8.7.6.0.10.21元素从小到大排序要求定义MAX和maxi并求出下标,
好的,以下是一段C语言程序,可以将给定数组从小到大排序,并求出最大值和最大值所在的下标:
```c
#include <stdio.h>
int main() {
int arr[] = {15, 5, 3, 1, 9, 8, 7, 6, 0, 10, 21};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, temp, MAX = arr[0], maxi = 0;
// 冒泡排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序后的数组
printf("排序后的数组:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
if (arr[i] > MAX) {
MAX = arr[i];
maxi = i;
}
}
printf("\n最大值为:%d\n", MAX);
printf("最大值的下标为:%d\n", maxi);
return 0;
}
```
输出结果如下所示:
```
排序后的数组:
0 1 3 5 6 7 8 9 10 15 21
最大值为:21
最大值的下标为:10
```
相关推荐
![](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)