1 学生成绩管理系统V4.0(4分)
题目内容:
某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入)。参考学生成绩管理系统V3.0,用二维数组作函数参数编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号、姓名和各科考试成绩;
(2)计算每门课程的总分和平均分;
(3)计算每个学生的总分和平均分;
(4)按每个学生的总分由高到低排出名次表;
(5)按每个学生的总分由低到高排出名次表;
(6)按学号由小到大排出成绩表;
(7)按姓名的字典顺序排出成绩表;
(8)按学号查询学生排名及其各科考试成绩;
(9)按姓名查询学生排名及其各科考试成绩;
(10)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数以及所占的百分比;
(11)输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。
要求程序运行后先显示如下菜单,并提示用户输入选项:
Management for Students' scores
1.Input record
2.Caculate total and average score of every course
3.Caculate total and average score of every student
4.Sort in descending order by score
5.Sort in ascending order by score
6.Sort in ascending order by number
7.Sort in dictionary order by name
8.Search by number
9.Search by name
10.Statistic analysis
11.List record
0.Exit
Please Input your choice:
然后,根据用户输入的选项执行相应的操作。
请按照下面的定义及函数原型编程
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
#define COURSE_NUM 6 /* 最多的考试科目数 */
int Menu(void);
void ReadScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], int n, int m);
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m,
float sum[STU_NUM], float aver[STU_NUM]);
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m);
void SortbyScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],
int n, int m, int (*compare)(float a, float b));
int Ascending(float a, float b);
int Descending(float a, float b);
void SwapFloat(float *x, float *y);
void SwapLong(long *x, long *y);
void SwapChar(char x[], char y[]);
void AsSortbyNum(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],
int n, int m);
void SortbyName(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],
int n, int m);
void SearchbyNum(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],
int n, int m);
void SearchbyName(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],
int n, int m);
void StatisticAnalysis(float score[][COURSE_NUM], int n, int m);
void PrintScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float sum[], float aver[],int n, int m) ;
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
#define COURSE_NUM 6 /* 最多的考试科目数 */
int Menu(void);
void ReadScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], int n, int m);
void AverSumOfEveryStudent(float score[][COURSE_NUM], int n, int m, float sum[STU_NUM], float aver[STU_NUM]);
void AverSumOfEveryCourse(float score[][COURSE_NUM], int n, int m);
void SortByScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[],
int n, int m, int (*compare)(float a, float b));
int Ascending(float a, float b);
int Descending(float a, float b);
void SwapFloat(float *x, float *y);
void SwapLong(long *x, long *y);
void SwapChar(char x[], char y[]);
void AsSortByNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m);
void SortByName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m);
void SearchByNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m);
void SearchByName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m);
void StatisticAnalysis(float score[][COURSE_NUM], int n, int m);
void PrintScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m);
int main()
{
int n, m = 0, option;
long num[STU_NUM];
float score[STU_NUM][STU_NUM], sum[STU_NUM], aver[STU_NUM];
char name[STU_NUM][MAX_LEN];
printf("Input student number(n<30):\n");
scanf("%d", &n);
Menu();
while (scanf("%d", &option) && option != 0)
{
switch (option)
{
case 1:
printf("Input course number(m<=%d):\n", n);
scanf("%d", &m);
ReadScore(num, name, score, n, m);
break;
case 2:
if(m != 0)
{
AverSumOfEveryCourse(score, n, m);
}
break;
case 3:
AverSumOfEveryStudent(score, n, m, sum, aver);
break;
case 4:
printf("Sort in descending order by score:\n");
SortByScore(num, name, score, sum, aver, n, m, Ascending);
break;
case 5:
printf("Sort in ascending order by score:\n");
SortByScore(num, name, score, sum, aver, n, m, Descending);
break;
case 6:
printf("Sort in ascending order by number:\n");
AsSortByNum(num, name, score, sum, aver, n, m);
break;
case 7:
printf("Sort in dictionary order by name:\n");
SortByName(num, name, score, sum, aver, n, m);
break;
case 8:
SearchByNum(num, name, score, sum, aver, n, m);
break;
case 9:
SearchByName(num, name, score, sum, aver, n, m);
break;
case 10:
StatisticAnalysis(score, n, m);
break;
case 11:
PrintScore(num, name, score, sum, aver, n, m);
break;
default:
printf("Input error!\n");
}
Menu();
}
printf("End of program!\n");
return 0;
}
int Menu(void)
{
printf("Management for Students' scores\n"
"1.Input record\n"
"2.Caculate total and average score of every course\n"
"3.Caculate total and average score of every student\n"
"4.Sort in descending order by score\n"
"5.Sort in ascending order by score\n"
"6.Sort in ascending order by number\n"
"7.Sort in dictionary order by name\n"
"8.Search by number\n"
"9.Search by name\n"
"10.Statistic analysis\n"
"11.List record\n"
"0.Exit\n"
"Please Input your choice:\n");
return 0;
}
void ReadScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], int n, int m)
{
printf("Input student's ID, name and score:\n");
for (int i = 0; i < n; ++i)
{
scanf("%ld%s", &num[i], name[i]);
for (int j = 0; j < m; ++j)
{
scanf("%f", &score[i][j]);
}
}
}
void AverSumOfEveryStudent(float score[][COURSE_NUM], int n, int m, float sum[STU_NUM], float aver[STU_NUM])
{
float sum2;
for (int i = 0; i < n; ++i)
{
sum2 = 0;
for (int j = 0; j < m; ++j)
{
sum2 += score[i][j];
}
aver[i] = sum2 / m;
sum[i] = sum2;
printf("student %d:sum=%.0f,aver=%.0f\n", i + 1, sum[i], aver[i]);
}
}
void AverSumOfEveryCourse(float score[][COURSE_NUM], int n, int m)
{
float sum;
for (int i = 0; i < m; ++i)
{
sum = 0;
for (int j = 0; j < n; ++j)
{
sum += score[j][i];
}
printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / n);
}
}
void SortByScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[],
int n, int m, int (*compare)(float a, float b))
{
int flag;
for (int i = 0; i < n; ++i)
{
flag = 0;
for (int j = 0; j < n - i - 1; ++j)
{
if((*compare)(sum[j], sum[j + 1]))
{
SwapFloat(&sum[j], &sum[j + 1]);
//课程分数交换
for (int k = 0; k < m; ++k)
{
SwapFloat(&score[j][k], &score[j + 1][k]);
}
SwapFloat(&aver[j], &aver[j + 1]);
SwapLong(&num[j], &num[j + 1]);
SwapChar(name[j], name[j + 1]);
flag = 1;
}
}
if(!flag)
{
break;
}
}
PrintScore(num, name, score, sum, aver, n, m);
}
int Ascending(float a, float b)
{
return a > b ? 0 : 1;
}
int Descending(float a, float b)
{
return a < b ? 0 : 1;
}
void SwapFloat(float *x, float *y)
{
float temp;
temp = *x;
*x = *y;
*y = temp;
}
void SwapLong(long *x, long *y)
{
long temp;
temp = *x;
*x = *y;
*y = temp;
}
void SwapChar(char x[], char y[])
{
char temp[MAX_LEN];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}
void AsSortByNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m)
{
int flag;
for (int i = 0; i < n; ++i)
{
flag = 0;
for (int j = 0; j < n - i - 1; ++j)
{
if(num[j] > num[j + 1])
{
SwapFloat(&sum[j], &sum[j + 1]);
//课程分数交换
for (int k = 0; k < m; ++k)
{
SwapFloat(&score[j][k], &score[j + 1][k]);
}
SwapFloat(&aver[j], &aver[j + 1]);
SwapLong(&num[j], &num[j + 1]);
SwapChar(name[j], name[j + 1]);
flag = 1;
}
}
if(!flag)
{
break;
}
}
PrintScore(num, name, score, sum, aver, n, m);
}
void SortByName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m)
{
int flag;
for (int i = 0; i < n; ++i)
{
flag = 0;
for (int j = 0; j < n - i - 1; ++j)
{
if(strcmp(name[j], name[j + 1]) > 0)
{
SwapFloat(&sum[j], &sum[j + 1]);
//课程分数交换
for (int k = 0; k < m; ++k)
{
SwapFloat(&score[j][k], &score[j + 1][k]);
}
SwapFloat(&aver[j], &aver[j + 1]);
SwapLong(&num[j], &num[j + 1]);
SwapChar(name[j], name[j + 1]);
flag = 1;
}
}
if(!flag)
{
break;
}
}
PrintScore(num, name, score, sum, aver, n, m);
}
void SearchByNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m)
{
int number, flag = 0, i;
printf("Input the number you want to search:\n");
scanf("%d", &number);
for (i = 0; i < n; ++i)
{
if(num[i] == number)
{
flag = 1;
break;
}
}
if(flag)
{
printf("%ld\t%s\t", num[i], name[i]);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", score[i][j]);
}
printf("%.0f\t%.0f\n", sum[i], aver[i]);
}
else
{
printf("Not found!\n");
}
}
void SearchByName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m)
{
int name2[MAX_LEN];
int flag = 0, i;
printf("Input the name you want to search:\n");
scanf("%s", &name2);
for (i = 0; i < n; ++i)
{
if(strcmp(name2, name[i]) == 0)
{
flag = 1;
break;
}
}
if(flag)
{
printf("%ld\t%s\t", num[i], name[i]);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", score[i][j]);
}
printf("%.0f\t%.0f\n", sum[i], aver[i]);
}
else
{
printf("Not found!\n");
}
}
void StatisticAnalysis(float scores[][COURSE_NUM], int n, int m)
{
int a, b, c, d, e, f;
float score;
for (int i = 0; i < m; ++i)
{
a = b = c = d = e = f = 0;
for (int j = 0; j < n; ++j)
{
score = scores[j][i];
if(score == 100)
{
a++;
}
else if(score >= 90 && score < 100 )
{
b++;
}
else if(score >= 80)
{
c++;
}
else if(score >= 70)
{
d++;
}
else if(score >= 60)
{
e++;
}
else
{
f++;
}
}
printf("For course %d:\n", i + 1);
printf("<60\t%d\t%.2f%%\n", f, (float)(100 * f) / n);
printf("%d-%d\t%d\t%.2f%%\n" , 60, 69, e, (float)(100 * e) / n);
printf("%d-%d\t%d\t%.2f%%\n" , 70, 79, d, (float)(100 * d) / n);
printf("%d-%d\t%d\t%.2f%%\n" , 80, 89, c, (float)(100 * c) / n);
printf("%d-%d\t%d\t%.2f%%\n" , 90, 99, b, (float)(100 * b) / n);
printf("%d\t%d\t%.2f%%\n", 100, a,(float)(100 * a) / n);
}
}
void PrintScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float sum[], float aver[], int n, int m)
{
for (int i = 0; i < n; ++i)
{
printf("%ld\t%s\t", num[i], name[i]);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", score[i][j]);
}
printf("%.0f\t%.0f\n", sum[i], aver[i]);
}
}
结构体版
#define MAX_LEN 10 /* 字符串最大长度 */
#define STU_NUM 30 /* 最多的学生人数 */
#define COURSE_NUM 6 /* 最多的考试科目数 */
typedef struct Student
{
long num;
char name[MAX_LEN];
float scores[COURSE_NUM];
float sum;
float aver;
} STUDENT;
int Menu(void);
void ReadScore(STUDENT student[], int n, int m);
void AverSumOfEveryStudent(STUDENT student[], int n, int m);
void AverSumOfEveryCourse(STUDENT student[], int n, int m);
void BubbleSort(STUDENT student[], int n, int m, int (*compare)(STUDENT, STUDENT));
int SortByScoreDescending(STUDENT student, STUDENT student2);
int SortByScoreAscending(STUDENT student, STUDENT student2);
int SortByNum(STUDENT student, STUDENT student2);
int SortByName(STUDENT student, STUDENT student2);
void SwapFloat(float *x, float *y);
void SwapLong(long *x, long *y);
void SwapChar(char x[], char y[]);
void SearchByNum(STUDENT student[], int n, int m);
void SearchByName(STUDENT student[], int n, int m);
void StatisticAnalysis(STUDENT student[], int n, int m);
void PrintScore(STUDENT student[], int n, int m);
int main()
{
int n, m = 0, option;
STUDENT student[STU_NUM];
printf("Input student number(n<30):\n");
scanf("%d", &n);
Menu();
while (scanf("%d", &option) && option != 0)
{
switch (option)
{
case 1:
printf("Input course number(m<=%d):\n", n);
scanf("%d", &m);
ReadScore(student, n, m);
break;
case 2:
if (m != 0)
{
AverSumOfEveryCourse(student, n, m);
}
break;
case 3:
AverSumOfEveryStudent(student, n, m);
break;
case 4:
printf("Sort in descending order by score:\n");
BubbleSort(student, n, m, SortByScoreDescending);
break;
case 5:
printf("Sort in ascending order by score:\n");
BubbleSort(student, n, m, SortByScoreAscending);
break;
case 6:
printf("Sort in ascending order by number:\n");
BubbleSort(student, n, m, SortByNum);
break;
case 7:
printf("Sort in dictionary order by name:\n");
BubbleSort(student, n, m, SortByName);
break;
case 8:
SearchByNum(student, n, m);
break;
case 9:
SearchByName(student, n, m);
break;
case 10:
StatisticAnalysis(student, n, m);
break;
case 11:
PrintScore(student, n, m);
break;
default:
printf("Input error!\n");
}
Menu();
}
printf("End of program!\n");
return 0;
}
int Menu(void)
{
printf("Management for Students' scores\n"
"1.Input record\n"
"2.Caculate total and average score of every course\n"
"3.Caculate total and average score of every student\n"
"4.Sort in descending order by score\n"
"5.Sort in ascending order by score\n"
"6.Sort in ascending order by number\n"
"7.Sort in dictionary order by name\n"
"8.Search by number\n"
"9.Search by name\n"
"10.Statistic analysis\n"
"11.List record\n"
"0.Exit\n"
"Please Input your choice:\n");
return 0;
}
void ReadScore(STUDENT student[], int n, int m)
{
printf("Input student's ID, name and score:\n");
for (int i = 0; i < n; ++i)
{
scanf("%ld%s", &student[i].num, student[i].name);
for (int j = 0; j < m; ++j)
{
scanf("%f", &student[i].scores[j]);
}
}
}
void AverSumOfEveryStudent(STUDENT student[], int n, int m)
{
float sum2;
for (int i = 0; i < n; ++i)
{
sum2 = 0;
for (int j = 0; j < m; ++j)
{
sum2 += student[i].scores[j];
}
student[i].aver = sum2 / m;
student[i].sum = sum2;
printf("student %d:sum=%.0f,aver=%.0f\n", i + 1, student[i].sum, student[i].aver);
}
}
void AverSumOfEveryCourse(STUDENT student[], int n, int m)
{
float sum;
for (int i = 0; i < m; ++i)
{
sum = 0;
for (int j = 0; j < n; ++j)
{
sum += student[j].scores[i];
}
printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / n);
}
}
int SortByNum(STUDENT student, STUDENT student2)
{
return student.num > student2.num;
}
int SortByName(STUDENT student, STUDENT student2)
{
return strcmp(student.name, student2.name) > 0;
}
int SortByScoreAscending(STUDENT student, STUDENT student2)
{
return student.sum > student2.sum;
}
int SortByScoreDescending(STUDENT student, STUDENT student2)
{
return student.sum < student2.sum;
}
void SwapFloat(float *x, float *y)
{
float temp;
temp = *x;
*x = *y;
*y = temp;
}
void SwapLong(long *x, long *y)
{
long temp;
temp = *x;
*x = *y;
*y = temp;
}
void SwapChar(char x[], char y[])
{
char temp[MAX_LEN];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}
void SearchByNum(STUDENT student[], int n, int m)
{
int number, flag = 0, i;
printf("Input the number you want to search:\n");
scanf("%d", &number);
for (i = 0; i < n; ++i)
{
if (student[i].num == number)
{
flag = 1;
break;
}
}
if (flag)
{
printf("%ld\t%s\t", student[i].num, student[i].name);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", student[i].scores[j]);
}
printf("%.0f\t%.0f\n", student[i].sum, student[i].aver);
} else
{
printf("Not found!\n");
}
}
void SearchByName(STUDENT student[], int n, int m)
{
int name2[MAX_LEN];
int flag = 0, i;
printf("Input the name you want to search:\n");
scanf("%s", &name2);
for (i = 0; i < n; ++i)
{
if (strcmp(name2, student[i].name) == 0)
{
flag = 1;
break;
}
}
if (flag)
{
printf("%ld\t%s\t", student[i].num, student[i].name);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", student[i].scores[j]);
}
printf("%.0f\t%.0f\n", student[i].sum, student[i].aver);
} else
{
printf("Not found!\n");
}
}
void StatisticAnalysis(STUDENT student[], int n, int m)
{
int a, b, c, d, e, f;
float score;
for (int i = 0; i < m; ++i)
{
a = b = c = d = e = f = 0;
for (int j = 0; j < n; ++j)
{
score = student[j].scores[i];
if (score == 100)
{
a++;
} else if (score >= 90 && score < 100)
{
b++;
} else if (score >= 80)
{
c++;
} else if (score >= 70)
{
d++;
} else if (score >= 60)
{
e++;
} else
{
f++;
}
}
printf("For course %d:\n", i + 1);
printf("<60\t%d\t%.2f%%\n", f, (float) (100 * f) / n);
printf("%d-%d\t%d\t%.2f%%\n", 60, 69, e, (float) (100 * e) / n);
printf("%d-%d\t%d\t%.2f%%\n", 70, 79, d, (float) (100 * d) / n);
printf("%d-%d\t%d\t%.2f%%\n", 80, 89, c, (float) (100 * c) / n);
printf("%d-%d\t%d\t%.2f%%\n", 90, 99, b, (float) (100 * b) / n);
printf("%d\t%d\t%.2f%%\n", 100, a, (float) (100 * a) / n);
}
}
void PrintScore(STUDENT student[], int n, int m)
{
for (int i = 0; i < n; ++i)
{
printf("%ld\t%s\t", student[i].num, student[i].name);
for (int j = 0; j < m; ++j)
{
printf("%.0f\t", student[i].scores[j]);
}
printf("%.0f\t%.0f\n", student[i].sum, student[i].aver);
}
}
void BubbleSort(STUDENT student[], int n, int m, int (*compare)(STUDENT, STUDENT))
{
int flag;
for (int i = 0; i < n; ++i)
{
flag = 0;
for (int j = 0; j < n - i - 1; ++j)
{
if ((*compare)(student[j], student[j + 1]))
{
SwapFloat(&student[j].sum, &student[j + 1].sum);
//课程分数交换
for (int k = 0; k < m; ++k)
{
SwapFloat(&student[j].scores[k], &student[j + 1].scores[k]);
}
SwapFloat(&student[j].aver, &student[j + 1].aver);
SwapLong(&student[j].num, &student[j + 1].num);
SwapChar(student[j].name, student[j + 1].name);
flag = 1;
}
}
if (!flag)
{
break;
}
}
PrintScore(student, n, m);
}
2 寻找最高分成绩的学生(4分)
题目内容:
下面程序的功能是用动态数组编程输入任意m个班学生(每班n个学生)的某门课的成绩,计算最高分,并指出具有该最高分成绩的学生是第几个班的第几个学生。其中,m和n的值由用户从键盘任意输入(不限定m和n的上限值)
#include <stdio.h>
#include <stdlib.h>
void InputScore(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
int *pScore, m, n, maxScore, row, col;
printf("Input array size m,n:\n");
scanf("%d,%d", &m, &n);
___________________; /* 申请动态内存 */
if (pScore == NULL)
{
printf("No enough memory!\n");
exit(0);
}
InputScore(pScore, m, n);
maxScore = FindMax(________________);
printf("maxScore = %d, class = %d, number = %d\n", maxScore, row+1, col+1);
free(pScore); /* 释放动态内存 */
return 0;
}
/* 函数功能:输入m行n列二维数组的值 */
void InputScore(_______, int m, int n)
{
int i, j;
printf("Input %d*%d array:\n", m, n);
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", _________);
}
}
}
/* 函数功能:计算任意m行n列二维数组中元素的最大值,并指出其所在行列下标值 */
int FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
int i, j, max = p[0];
__________;
__________;
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
if (___________)
{
max = p[i*n+j];
*pRow = i; /*记录行下标*/
*pCol = j; /*记录列下标*/
}
}
}
return max;
}
修改后
#include <stdio.h>
#include <stdlib.h>
void InputScore(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
int *pScore, m, n, maxScore, row, col;
printf("Input array size m,n:\n");
scanf("%d,%d", &m, &n);
/* 申请动态内存 */
pScore = (int *) malloc(n * m * sizeof(int));
if (pScore == NULL)
{
printf("No enough memory!\n");
exit(0);
}
InputScore(pScore, m, n);
maxScore = FindMax(pScore, m, n, &row, &col);
printf("maxScore = %d, class = %d, number = %d\n", maxScore, row + 1, col + 1);
free(pScore); /* 释放动态内存 */
return 0;
}
/* 函数功能:输入m行n列二维数组的值 */
void InputScore(int *p, int m, int n)
{
int i, j;
printf("Input %d*%d array:\n", m, n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &p[i * n + j]);
}
}
}
/* 函数功能:计算任意m行n列二维数组中元素的最大值,并指出其所在行列下标值 */
int FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
int i, j, max = p[0];
*pRow = 0;
*pCol = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < p[i * n + j])
{
max = p[i * n + j];
*pRow = i; /*记录行下标*/
*pCol = j; /*记录列下标*/
}
}
}
return max;
}
3 程序改错(4分)
题目内容:
下面程序的功能是输入m个学生(最多为30人)n门课程(最多为5门)的成绩,然后计算并打印每个学生各门课的总分和平均分。其中,m和n的值由用户从键盘输入
#include <stdio.h>
#define STUD 30 /* 最多可能的学生人数 */
#define COURSE 5 /* 最多可能的考试科目数 */
void Total(int *pScore, int sum[], float aver[], int m, int n);
void Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("How many students?");
scanf("%d", &m);
printf("How many courses?");
scanf("%d", &n);
printf("Input scores:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int *pScore, int sum[], float aver[], int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
sum[i] = 0;
for (j=0; j<n; j++)
{
sum[i] = sum[i] + pScore[i*n+j];
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int *pScore, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
printf("%4d\t", pScore[i*n+j]);
}
printf("%5d\t%6.1f\n", sum[i], aver[i]);
}
}
修改后
#define STUD 30 /* 最多可能的学生人数 */
#define COURSE 5 /* 最多可能的考试科目数 */
void Total(int *pScore, int sum[], float aver[], int m, int n);
void Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("How many students?\n");
scanf("%d", &m);
printf("How many courses?\n");
scanf("%d", &n);
printf("Input scores:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int *pScore, int sum[], float aver[], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
sum[i] = 0;
for (j = 0; j < n; j++)
{
sum[i] = sum[i] + pScore[i * COURSE + j];
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int *pScore, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%4d", pScore[i * COURSE + j]);
}
printf("%5d%6.1f\n", sum[i], aver[i]);
}
}
4 矩阵转置(4分)
题目内容:
下面程序的功能是用二维数组的列指针作为函数实参,计算并输出m×n阶矩阵的转置矩阵。其中,m和n的值由用户从键盘输入。已知m和n的值都不超过10
#include <stdio.h>
#define M 10
#define N 10
void Transpose(int *a, int *at, int m, int n);
void InputMatrix(int *a, int m, int n);
void PrintMatrix(int *at, int n, int m);
int main()
{
int s[M][N], st[N][M], m, n;
printf("Input m, n:");
scanf("%d,%d", &m, &n);
InputMatrix(____, m, n);
Transpose(______________);
printf("The transposed matrix is:\n");
PrintMatrix(*st, n, m);
return 0;
}
/* 函数功能:计算m*n矩阵a的转置矩阵at */
void Transpose(int *a, int *at, int m, int n)
{
int i, j;
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
_____________;
}
}
}
/* 函数功能:输入m*n矩阵a的值 */
void InputMatrix(int *a, int m, int n)
{
int i, j;
printf("Input %d*%d matrix:\n", m, n);
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf("%d", ____________);
}
}
}
/* 函数功能:输出n*m矩阵at的值 */
void PrintMatrix(int *at, int n, int m)
{
int i, j;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("%-5d", ____________);
}
printf("\n");
}
}
修改后
#define M 10
#define N 10
void Transpose(int *a, int *at, int m, int n);
void InputMatrix(int *a, int m, int n);
void PrintMatrix(int *at, int n, int m);
int main()
{
int s[M][N], st[N][M], m, n;
printf("Input m, n:\n");
scanf("%d,%d", &m, &n);
InputMatrix(s, m, n);
Transpose(s, st, m, n);
printf("The transposed matrix is:\n");
PrintMatrix(*st, n, m);
return 0;
}
/* 函数功能:计算m*n矩阵a的转置矩阵at */
void Transpose(int *a, int *at, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
at[j * N + i] = a[i * N + j];
}
}
}
/* 函数功能:输入m*n矩阵a的值 */
void InputMatrix(int *a, int m, int n)
{
int i, j;
printf("Input %d*%d matrix:\n", m, n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i * N + j]);
}
}
}
/* 函数功能:输出n*m矩阵at的值 */
void PrintMatrix(int *at, int n, int m)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%-5d", at[i * N + j]);
}
printf("\n");
}
}
5 在升序排序的数组中插入一个元素(4分)
题目内容:
用函数编程实现在一个按升序排序的数组中查找x应插入的位置,将x插入数组中,使数组元素仍按升序排列。
提示:插入(Insertion)是数组的基本操作之一。插入法排序算法的关键在于要找到正确的插入位置,然后依次移动插入位置及其后的所有元素,腾出这个位置放入待插入的元素。插入排序的原理如图所示:
#include <stdio.h>
#include <stdlib.h>
int Find(int *p, int x, int n);
void Insert(int *p,int x, int index, int n);
int main()
{
int *p, size, x, index;
printf("Input array size:\n");
scanf("%d", &size);
p = (int*) malloc(size * sizeof(int));
if(p == NULL)
{
printf("No enough memory!\n");
exit(0);
}
printf("Input array:\n");
for (int i = 0; i < size; ++i)
{
scanf("%d", &p[i]);
}
printf("Input x:\n");
scanf("%d", &x);
size = size + 1;
p = (int*) realloc(p, size * sizeof(int));
if(p == NULL)
{
printf("No enough memory!\n");
exit(0);
}
index = Find(p, x, size);
Insert(p, x, index, size);
printf("After insert %d:\n", x);
for (int j = 0; j < size; ++j)
{
printf("%4d", p[j]);
}
free(p);
return 0;
}
int Find(int *p, int x, int n)
{
int pos = 0;
for (int i = 0; i < n; ++i)
{
if(x < p[i])
{
pos = i;
break;
}
}
return pos;
}
void Insert(int *p, int x, int index, int n)
{
int i;
//从后往前搬移数组元素
for (i = n - 1; i > index; i--)
{
p[i] = p[i - 1];
}
p[index] = x;
}
6 计算平均数、中位数和众数(4分)
题目内容:
在调查数据分析(Survey data analysis)中经常需要计算平均数、中位数和众数。用函数编程计算40个输入数据(是取值1—10之间的任意整数)的平均数(Mean)、中位数(Median)和众数(Mode)。中位数指的是排列在数组中间的数。众数是数组中出现次数最多的那个数(不考虑两个或两个以上的输入数据出现次数相同的情况)。
提示:计算中位数时,首先要调用排序函数对数组按升序进行排序,然后取出排序后数组中间位置的元素answer[n/2] ,就得到了中位数。如果数组元素的个数是偶数,那么中位数就等于数组中间那两个元素的算术平均值。众数就是40个输入数据中出现次数最多的那个数。计算众数时,首先要统计不同取值的输入数据出现的次数,然后找出出现次数最多的那个数据,这个数据就是众数(这里没有考虑两个或者两个以上的输入数据出现次数相同的情况)。
#define N 40
void BubbleSort(int a[], int n);
int main()
{
int mean, median, mode, m, sum = 0, a[N], n[10] = {0};
printf("Input the feedbacks of 40 students:\n");
for (int i = 0; i < N; ++i)
{
scanf("%d", &a[i]);
sum += a[i];
m = a[i] % 10;
n[m]++;
}
BubbleSort(a, N);
//众数
mode = 0;
for (int j = 1; j < 10; ++j)
{
if(n[mode] < n[j])
{
mode = j;
}
}
if(mode == 0)
{
mode = 10;
}
//判断数组元素个数是否为偶数
if(N % 2 == 0)
{
median = (a[N / 2] + a[N / 2 - 1]) / 2;
}
else
{
median = a[N / 2];
}
mean = sum / N;
printf("Mean value=%d\n", mean );
printf("Median value=%d\n", median);
printf( "Mode value=%d\n", mode);
return 0;
}
void BubbleSort(int a[], int n)
{
int flag, t;
for (int i = 0; i < n; ++i)
{
flag = 0;
for (int j = 0; j < n - i - 1; ++j)
{
if(a[j] > a[j + 1])
{
t = a[j + 1];
a[j + 1] = a[j];
a[j] = t;
flag = 1;
}
}
if(!flag)
{
break;
}
}
}