我在学校学习C ++课程,并决定自己学习C语言课程。试图制作一个小程序,计算我班级的平均成绩,并显示学生的字母等级。当我输入学生的分数时,我输入4个值并显示分段错误后代码停止。 我很确定这是原因,但不确定如何修复它。 我将不胜感激任何帮助。
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
double Average(double * scores, int N) {
int i;
double total = 0;
for (i = 0; i < N; i++) {
total = total + scores[i];
}
return total / N;
}
int Agrade(double * scores, int N) {
int i;
int count = 0;
for (i = 0; i < N; i++) {
if (scores[i] >= 90 && scores[i] <= 100) count++;
}
return count;
}
int Bgrade(double * scores, int N) {
int i;
int count = 0;
for (i = 0; i < N; i++) {
if (scores[i] >= 80 && scores[i] < 90) count++;
}
return count;
}
int Cgrade(double * scores, int N) {
int i;
int count = 0;
for (i = 0; i < N; i++) {
if (scores[i] >= 70 && scores[i] < 80) count++;
}
return count;
}
int Dgrade(double * scores, int N) {
int i;
int count = 0;
for (i = 0; i < N; i++) {
if (scores[i] >= 60 && scores[i] < 70) count++;
}
return count;
}
int Fgrade(double * scores, int N) {
int i;
int count = 0;
for (i = 0; i < N; i++) {
if (scores[i] < 60) count++;
}
return count;
}
int main() {
int i;
int N;
double * scores;
printf("How many test scores? ");
scanf("%d", & N);
if (N < 1) {
printf("Invalid input. try again");
} else if (N > 25) {
printf("1-25 only.");
} else if (N > 0 && N < 25) {
printf("Total number of test is: %d\n", N);
}
double * scores = malloc(N * sizeof(double)); /* allocate the memory for N students */
for (i = 0; i < N; i++) {
printf("Enter the score of students: ");
scanf("%lf", & scores[i]);
}
double averagescore = Average(scores, N);
int scoreAcount = Agrade(scores, N);
int scoreBcount = Bgrade(scores, N);
int scoreCcount = Cgrade(scores, N);
int scoreDcount = Dgrade(scores, N);
int scoreFcount = Fgrade(scores, N);
printf("The average test score : %lf", averagescore);
printf("The number of A grades : %d\n", scoreAcount);
printf("The number of B grades : %d\n", scoreBcount);
printf("The number of C grades : %d\n", scoreCcount);
printf("The number of D grades : %d\n", scoreDcount);
printf("The number of F grades : %d\n", scoreFcount);
return 0;
}
答案 0 :(得分:3)
请参阅以下代码的代码块。
for(i = 0; i < sizeof(N); i++) { /* sizeof(N) is wrong */
printf("Enter the score of students: ");
scanf("%d",&N);/* putting data into N ? its wrong */
}
double averagescore = Average(scores, N); /* calling averagescore() with scores which is uninitialized & contain nothing ? */
应该是
double *scores = malloc(N * sizeof(double));/* allocate the memory for N students */
for(i = 0; i < N; i++) {
printf("Enter the score of students: ");
scanf("%lf",&scores[i]);
}
这是工作中的
int main(){
int i;
int N;
printf("How many test scores? "); //Number of students. Trying to calculate up to 25 students
scanf("%d",&N);
if(N<1){
printf("Invalid input. try again");
}
else if(N>25)
{
printf("1-25 only.");
}
else if(N>0 && N<25){
printf("Total number of test is: %d\n", N);
}
double *scores = malloc(N * sizeof(double));/* allocate the memory for N students */
for(i = 0; i < N; i++) {
printf("Enter the score of students: ");
scanf("%lf",&scores[i]);
}
double averagescore = Average(scores, N);
int scoreAcount = Agrade(scores, N);
int scoreBcount = Bgrade(scores, N);
int scoreCcount = Cgrade(scores, N);
int scoreDcount = Dgrade(scores, N);
int scoreFcount = Fgrade(scores, N);
printf("The average test score : %f\n", averagescore);
printf("The number of A grades : %d\n", scoreAcount);
printf("The number of B grades : %d\n", scoreBcount);
printf("The number of C grades : %d\n", scoreCcount);
printf("The number of D grades : %d\n", scoreDcount);
printf("The number of F grades : %d\n", scoreFcount);
return 0;
}
了解如何调试小代码。
答案 1 :(得分:0)
函数sizeof返回变量&#39; N&#39;的字节数。包含的内容。你不想要那个!您应该将sizeof(N)替换为N,以便从输入的N个数字中读取。你也应该在循环中检查你的代码,因为它不对!
答案 2 :(得分:0)
sizeof(N)
这是错误的。 N
已经是一个整数,所以要求大小是没用的,除非你试图分配一些内存来保存一个int,你不是。