C语言求平均成绩,最低分,最高分的代码
时间: 2023-05-20 08:07:19 浏览: 164
以下是C语言的求平均成绩,最低分和最高分的代码:
```c
#include <stdio.h>
int main() {
int n,i,min,max;
float sum=0,avg;
printf("请输入学生人数:");
scanf("%d",&n);
int score[n];
printf("请依次输入学生成绩:");
for(i=0; i<n; i++) {
scanf("%d",&score[i]);
if(i==0) {
max=score[0];
min=score[0];
}
if(score[i]>max) {
max=score[i];
}
if(score[i]<min) {
min=score[i];
}
sum += score[i];
}
avg=sum/n;
printf("平均成绩为:%.2f\n", avg);
printf("最高分为:%d\n", max);
printf("最低分为:%d\n", min);
return 0;
}
```
希望对您有所帮助!
相关问题
输入8名学生成绩 求平均分最高分最低分C语言数组代码
以下是一种可能的解法:
```c
#include <stdio.h>
int main() {
int scores[8];
int sum = 0;
int min = 100;
int max = 0;
for (int i = 0; i < 8; i++) {
printf("请输入第%d名学生的成绩:", i + 1);
scanf("%d", &scores[i]);
sum += scores[i];
if (scores[i] < min) {
min = scores[i];
}
if (scores[i] > max) {
max = scores[i];
}
}
float avg = (float)sum / 8;
printf("平均分为:%.2f\n", avg);
printf("最低分为:%d\n", min);
printf("最高分为:%d\n", max);
return 0;
}
```
这段代码会提示用户输入8名学生的成绩,并且使用一个数组来保存这些成绩。在输入成绩的同时,程序还会计算总分、最低分和最高分。最后,程序会计算平均分并输出结果。注意,这种解法没有处理输入不合法的情况,例如输入非数字字符的情况。如果需要更严格的输入检查,请在代码中进行相应的修改。
需要实现显示学生成绩信息,显示C语言的最高分,显示C语言的最低分,显示C语言的平均分,从高到底显示C语言成绩,按学号查找C语言成绩,按姓名查找C语言成绩等功能的代码
下面是一个简单的学生成绩管理系统的代码,实现了显示学生信息、C语言成绩的最高分、最低分、平均分、从高到低排序、按学号查找和按姓名查找等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100
struct Student {
char name[20];
int id;
float c_score;
};
void display_student(struct Student s);
float get_c_highest(struct Student students[], int n);
float get_c_lowest(struct Student students[], int n);
float get_c_average(struct Student students[], int n);
void sort_by_c_score_desc(struct Student students[], int n);
void search_by_id(struct Student students[], int n, int id);
void search_by_name(struct Student students[], int n, char name[]);
int main() {
struct Student students[MAX_STUDENT];
int n, i, choice, id;
char name[20];
printf("Enter the number of students (max %d): ", MAX_STUDENT);
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the name, id, and C score of student %d: ", i+1);
scanf("%s %d %f", students[i].name, &students[i].id, &students[i].c_score);
}
while (1) {
printf("\n1. Display all students\n");
printf("2. Display C highest score\n");
printf("3. Display C lowest score\n");
printf("4. Display C average score\n");
printf("5. Sort by C score (descending order)\n");
printf("6. Search by id\n");
printf("7. Search by name\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\n%-20s%-10s%-10s\n", "Name", "ID", "C score");
for (i = 0; i < n; i++) {
display_student(students[i]);
}
break;
case 2:
printf("\nC highest score: %.2f\n", get_c_highest(students, n));
break;
case 3:
printf("\nC lowest score: %.2f\n", get_c_lowest(students, n));
break;
case 4:
printf("\nC average score: %.2f\n", get_c_average(students, n));
break;
case 5:
sort_by_c_score_desc(students, n);
printf("\n%-20s%-10s%-10s\n", "Name", "ID", "C score");
for (i = 0; i < n; i++) {
display_student(students[i]);
}
break;
case 6:
printf("\nEnter student id to search: ");
scanf("%d", &id);
search_by_id(students, n, id);
break;
case 7:
printf("\nEnter student name to search: ");
scanf("%s", name);
search_by_name(students, n, name);
break;
case 8:
exit(0);
default:
printf("\nInvalid choice!\n");
}
}
return 0;
}
void display_student(struct Student s) {
printf("%-20s%-10d%-10.2f\n", s.name, s.id, s.c_score);
}
float get_c_highest(struct Student students[], int n) {
int i;
float highest = students[0].c_score;
for (i = 1; i < n; i++) {
if (students[i].c_score > highest) {
highest = students[i].c_score;
}
}
return highest;
}
float get_c_lowest(struct Student students[], int n) {
int i;
float lowest = students[0].c_score;
for (i = 1; i < n; i++) {
if (students[i].c_score < lowest) {
lowest = students[i].c_score;
}
}
return lowest;
}
float get_c_average(struct Student students[], int n) {
int i;
float sum = 0;
for (i = 0; i < n; i++) {
sum += students[i].c_score;
}
return sum / n;
}
void sort_by_c_score_desc(struct Student students[], int n) {
int i, j;
struct Student temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (students[i].c_score < students[j].c_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void search_by_id(struct Student students[], int n, int id) {
int i;
for (i = 0; i < n; i++) {
if (students[i].id == id) {
printf("\n%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].c_score);
return;
}
}
printf("\nStudent with id %d not found!\n", id);
}
void search_by_name(struct Student students[], int n, char name[]) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("\n%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].c_score);
}
}
}
```
阅读全文