c语言编程:有n(n为整数,并且5≤n≤200)名学生,已知n名同学的考试成绩x(x为整数,并且0≤x≤100),求计算机2022级的平均分、最高分和最低分。
时间: 2024-05-27 13:11:42 浏览: 13
#include <stdio.h>
int main()
{
int n, x, sum = 0, max = 0, min = 100;
float average;
printf("请输入学生人数:");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
printf("请输入第%d名学生的成绩:", i);
scanf("%d", &x);
sum += x; // 累加总分
if (x > max) // 更新最高分
{
max = x;
}
if (x < min) // 更新最低分
{
min = x;
}
}
average = (float)sum / n; // 计算平均分
printf("平均分为:%.2f\n", average);
printf("最高分为:%d\n", max);
printf("最低分为:%d\n", min);
return 0;
}
相关问题
请使用c语言写一段代码:计算机2017级有n(n为整数,并且5≤n≤200)名学生,已知n名同学的考试成绩x(x为整数,并且0≤x≤100),求低于平均分的学生人数
```c
#include <stdio.h>
int main() {
int n, sum = 0, count = 0;
float avg;
printf("请输入学生人数n: ");
scanf("%d", &n);
int scores[n];
for(int i = 0; i < n; i++) {
printf("请输入第%d位学生的成绩: ", i+1);
scanf("%d", &scores[i]);
sum += scores[i];
}
avg = (float)sum / n;
for(int i = 0; i < n; i++) {
if(scores[i] < avg) {
count++;
}
}
printf("低于平均分的学生人数为%d\n", count);
return 0;
}
```
c语言编程输入实数x和正整数n,递归方法实现计算x的n次方。
题目要求使用递归方法实现计算实数x的正整数n次方,可以按照以下步骤编写代码:
```c
#include <stdio.h>
// 递归函数实现计算x的n次方
double power(double x, int n){
if (n == 0) { // 边界条件,n为0时,任何数的0次方都等于1
return 1.0;
} else if (n > 0) { // n为正整数时
return x * power(x, n - 1); // x的n次方等于x*x的n-1次方
} else { // n为负整数时
return 1.0 / power(x, -n); // x的-n次方等于1/x的n次方
}
}
int main(){
double x; // 实数x
int n; // 正整数n
printf("请输入实数x和正整数n:");
scanf("%lf %d", &x, &n);
printf("%.2lf的%d次方为:%.2lf\n", x, n, power(x, n));
return 0;
}
```
以上代码中,递归函数`power`接收两个参数,实数`x`和正整数`n`,根据题目要求,当`n`为0时,任何数的0次方都等于1,此时递归返回1.0;当`n`为正整数时,递归调用`power`函数计算x的n-1次方,然后将结果乘以x,得到x的n次方;当`n`为负整数时,递归调用`power`函数计算1/x的-n次方,得到x的-n次方。
在主函数`main`中,首先读入实数`x`和正整数`n`,然后调用`power`函数计算x的n次方,并输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)