不建立动态数组,像这样会报错吗?
程序代码:
/*输入班级人数和成绩, 统计各分数段人数. 要求: 1) 根据用户输入的人数, 利用malloc/calloc函数创建动态数组, 用于保存成绩. 2) 分数分为5段: [0,60), [60, 70), [70, 80), [80, 90), [90, 100]*/ #include<stdio.h> #include<time.h> #include<stdlib.h> int main() { int N, sec1, sec2, sec3, sec4, sec5; sec1 = sec2 = sec3 = sec4 = sec5 = 0; printf("请输入学生人数:\n"); scanf("%d", &N); int k, grades[N]; srand(time(NULL)); printf("请输入学生成绩:\n"); for(k = 0; k < N; k++) { grades[k] = rand() % 100 + 1; printf("%-4d", grades[k]); } for(k = 0; k < N; k++) { if(grades[k]>=0&&grades[k]<60) sec1++; else if(grades[k]>=60&&grades[k]<70) sec2++; else if(grades[k]>=70&&grades[k]<80) sec3++; else if(grades[k]>=80&&grades[k]<90) sec4++; else if(grades[k]>=90&&grades[k]<100) sec5++; } printf("\n"); printf("各分数段人数统计如下:\n"); printf("[0,60):%d人\n", sec1); printf("[60,70):%d人\n", sec2); printf("[70,80):%d人\n", sec3); printf("[80,90):%d人\n", sec4); printf("[90,100]:%d人\n", sec5); }这是效果图