C语言从键盘中输入成绩的数量n,尝试使用malloc动态地创建一个具有n个元素的一维数组,从键盘中读入每个学生的成绩并存放在数组中,最后计算得到这些分数的平均分并打印到屏幕上。
时间: 2024-05-05 17:21:40 浏览: 17
下面是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
float *scores, sum = 0, avg;
printf("请输入学生的数量:");
scanf("%d", &n);
// 动态创建一维数组
scores = (float *)malloc(n * sizeof(float));
// 读入每个学生的成绩并存放在数组中
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的成绩:", i + 1);
scanf("%f", &scores[i]);
sum += scores[i];
}
// 计算平均分并打印到屏幕上
avg = sum / n;
printf("平均分为:%.2f\n", avg);
// 释放动态分配的内存
free(scores);
return 0;
}
```
在该代码中,我们先从键盘中输入了学生的数量n,然后使用malloc动态地创建一个具有n个元素的一维数组。接着,我们使用for循环读入每个学生的成绩并存放在数组中,并计算这些分数的总和。最后,我们计算出平均分并打印到屏幕上,然后释放动态分配的内存。
相关问题
二维动态数组输入m个班级每班n个学生的成绩输出平均分 C语言
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
printf("请输入班级数:");
scanf("%d", &m);
printf("请输入每个班级的学生数:");
scanf("%d", &n);
// 动态分配二维数组
int **scores = (int **)malloc(m * sizeof(int *));
for (int i = 0; i < m; i++) {
scores[i] = (int *)malloc(n * sizeof(int));
}
// 输入成绩
for (int i = 0; i < m; i++) {
printf("请输入第%d个班级的%d个学生成绩:\n", i + 1, n);
for (int j = 0; j < n; j++) {
scanf("%d", &scores[i][j]);
}
}
// 计算平均分并输出
for (int i = 0; i < m; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
sum += scores[i][j];
}
double avg = (double)sum / n;
printf("第%d个班级的平均分为%.2lf\n", i + 1, avg);
}
// 释放内存
for (int i = 0; i < m; i++) {
free(scores[i]);
}
free(scores);
return 0;
}
C语言用数组做函数参数的方法,求出一堆数组的最小值,从主函数中输入n
好的,以下是使用数组做函数参数的方法,求出一堆数组的最小值,并从主函数中输入n的C语言示例代码:
```c
#include <stdio.h>
int min_array(int arr[], int n) {
/*
求数组的最小值
*/
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int n;
printf("请输入数组的长度:");
scanf("%d", &n);
int arr[n];
printf("请输入%d个整数:", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int result = min_array(arr, n);
printf("数组中的最小值为:%d\n", result);
return 0;
}
```
在这个程序中,我们定义了一个min_array()函数,它接受一个整数数组和数组的长度作为参数,并返回数组的最小值。在主函数中,我们使用scanf()函数从用户处获取数组的长度n,然后创建一个长度为n的数组,并使用循环从用户处获取数组中的每个元素。最后,我们将数组和数组的长度作为参数传递给min_array()函数,并打印出结果。
请注意,这个程序中定义的数组是使用C99中的变长数组特性创建的。如果您的编译器不支持变长数组,您可以使用动态内存分配函数(如malloc()和free())来创建数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)