用c语言代码实现从键盘输入3位同学的一组信息,包括学号、姓名、数学成绩、计算机成绩,求得每位同学的平均分和总分,然后输出这些信息。(包括学号、姓名、数学成绩、计算机成绩、平均分、总分)
时间: 2023-12-22 09:28:27 浏览: 45
以下是使用C语言实现从键盘输入3位同学信息,计算平均分和总分并输出的代码:
```c
#include <stdio.h>
int main() {
int i;
char name[3][20];
int id[3], math[3], computer[3];
float avg[3], sum[3];
for (i = 0; i < 3; i++) {
printf("请输入第%d位同学的学号、姓名、数学成绩、计算机成绩:\n", i+1);
scanf("%d %s %d %d", &id[i], name[i], &math[i], &computer[i]);
sum[i] = math[i] + computer[i];
avg[i] = sum[i] / 2;
}
printf("学号\t姓名\t数学成绩\t计算机成绩\t平均分\t总分\n");
for (i = 0; i < 3; i++) {
printf("%d\t%s\t%d\t\t%d\t\t%.2f\t%.2f\n", id[i], name[i], math[i], computer[i], avg[i], sum[i]);
}
return 0;
}
```
相关问题
编写c语言代码,使用指针与函数的方式,实现从键盘输入3位同学的成绩,并输出最高分。(使用函数计算最高分)
好的,以下是使用指针与函数的方式,实现从键盘输入3位同学的成绩,并输出最高分的C语言代码:
```c
#include <stdio.h>
void inputScore(int *scores);
int findMaxScore(int *scores);
int main() {
int scores[3];
inputScore(scores);
int maxScore = findMaxScore(scores);
printf("最高分为:%d\n", maxScore);
return 0;
}
void inputScore(int *scores) {
for (int i = 0; i < 3; i++) {
printf("请输入第%d位同学的成绩:", i+1);
scanf("%d", scores+i);
}
}
int findMaxScore(int *scores) {
int maxScore = *scores;
for (int i = 1; i < 3; i++) {
if (*(scores+i) > maxScore) {
maxScore = *(scores+i);
}
}
return maxScore;
}
```
在上述代码中,`inputScore` 函数用于从键盘输入 3 位同学的成绩,并保存到 `scores` 数组中;`findMaxScore` 函数用于计算 `scores` 数组中的最高分,并返回该最高分值。在 `main` 函数中,我们调用 `inputScore` 函数输入成绩,调用 `findMaxScore` 函数计算最高分,并输出结果。
需要注意的是,在函数中使用指针访问数组元素时,需要使用 `*` 和 `[]` 运算符。`*` 运算符用于取得指针所指向的值,`[]` 运算符用于访问数组元素。例如,`*(scores+i)` 就相当于 `scores[i]`。
c语言从键盘输入1门课程3位同学成绩,从高到低进行排序,用数组函数的方式。
下面是一个示例代码,用来实现从键盘输入3位同学的成绩并进行排序:
```c
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(int*)b - *(int*)a; // 从高到低排序
}
int main() {
int scores[3];
printf("请输入3位同学的成绩:\n");
for (int i = 0; i < 3; i++) {
scanf("%d", &scores[i]);
}
qsort(scores, 3, sizeof(int), cmp); // 调用qsort函数进行排序
printf("排序后的成绩为:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}
```
上述代码中,我们先定义了一个整型数组`scores`,用于存储3位同学的成绩。然后通过`scanf`函数从键盘输入3位同学的成绩,并将其存储到数组`scores`中。
接着,我们调用了`qsort`函数对数组`scores`进行排序。`qsort`函数是C语言标准库中的一个快速排序函数,它的用法如下:
```c
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
```
其中,`base`参数表示待排序数组的首地址,`nmemb`表示数组中元素的个数,`size`表示每个元素的大小,`compar`表示用于比较两个元素的函数指针。
在本例中,我们自定义了一个比较函数`cmp`,用于比较两个成绩的大小,并按照从高到低的顺序排序。最后,我们通过循环遍历数组`scores`,并使用`printf`函数输出已经排序好的成绩。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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_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)