【发布时间】:2015-12-17 18:41:12
【问题描述】:
我为作业编写了代码。它基本上是一个程序,提示用户输入学生的分数并决定学生是否有资格进入下一个级别。 该程序应该将学生的ID号和他/她的分数写入一个*.dat文件,并根据他们的成绩在一个文本文件中按降序输出所有学生的ID,作为一个表格。 我使用了一个多维数组 (results[10][2]) 来存储每个学生的 id 和平均分数,然后我创建了一个函数来按降序写入 ID。问题是:
1- 数组的每一行包含两个单元格,一个 int 和一个 float。 (我应该将数组的类型初始化为 int 还是 float?)。
2- 输出文件总是由零组成。每一个细胞。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void saveMarks(int x, int array[]);
void tabulateMarks(int array[][2]);
int main(){
int choice=1;
int count=0;
int i, id;
int credits=0;
int credits1=0;
int credits2=0;
int total1=0;
int total2=0;
int modules1=0;
int modules2=0;
int marks[37];
int results[10][2]={0};
float weigh, avg1, avg2;
FILE* fmarks;
FILE* fresults;
while(choice!=2){
printf("\n**********************************************************\n");
printf("\t1.Enter student's marks.\n\t2.Quit.\n");
printf("**********************************************************\n");
printf("Your Choice: ");
scanf("%d", &choice);
if(choice == 1){
//year one
printf("\nEnter student ID number: ");
scanf("%f", id);
id = results[count][0];
printf("\nThe following are the modules of Year One:\n");
printf("**NOTE: All the following modules are compulsory.\n\n");
printf("H61IIC Introduction to Electronic Engineering\n");
printf(" MM1DM Design and Manufacture 1\n");
printf("H61LSB Laboratory and Presentation Skills B\n");
printf("H61ICT Introduction to Circuits\n");
printf("H61ICP Introduction to Computer Engineering\n");
printf(" MM1MS Mechanics of Solids 1\n");
printf("HG1M11 Engineering Mathematics 1\n");
printf("H61IAL Introduction to Electrical Engineering\n");
printf("HG1M12 Engineering Mathematics 2\n");
printf(" MM1DM Dynamics of Mechanical Systems\n\n");
printf("Enter student's marks for the above modules:\n");
for(i=0; i<10; i++){
scanf("%d", &marks[i]);
if(marks[i] >= 40){
if(i == 0 || i == 1)
credits += 20;
else
credits += 10;
}
}
if(credits<100){
printf("\n\nStudent failed to progress to Year Two.\n");
}
else{
printf("\nStudent progressed to Year Two.\n");
//year two
printf("\nThe following are the modules of Year Two:\n");
printf("**NOTE: All the following modules are compulsory.\n\n");
printf("MM2DM Design and Manufacture 2\n");
printf("MM1TF1 Thermodynamics & Fluid Mechanics 1\n");
printf("HG2ME1 Mathematical Techniques for Electrical and Electronic Engineers 1\n");
printf("H62SPC Signal Processing and Control Engineering\n");
printf("H62PSE Power Supply Electronics\n");
printf("H62NUM Numerical Methods and Contextual Electrical and Electronic Engineering Mathematics\n");
printf("H62ELD Electronic Engineering\n");
printf("Electrical Engineering Design Project\n\n");
printf("Enter student's marks for the above modules:\n");
for(i=10;i<18; i++){
scanf("%d", &marks[i]);
if(marks[i] >= 40){
total1 += marks[i];
modules1++;
if(i == 10|| i == 11|| i == 13|| i == 16)
credits1 += 20;
else
credits1 += 10;
}
}
if(credits1>=100 && (total1/modules1)>=55){
printf("\nStudent progressed to Year Three.\n");
//year three
printf("\nThe following are the modules of Year Three:\n\n");
printf("Compulsory Modules:\n");
printf("H63MGP Mechatronics Group Project (only for M.Eng students)\n");
printf("H63TYP Third year project\n");
printf("H63CSD Control Systems Design\n");
printf("H63NEN Neural Networks\n");
printf("H63RDC Robotics Dynamics and Control\n");
printf("H63MLB Mechatronics Laboratory\n");
printf("H63RAR Risk and Reliability\n");
printf("H13IDT Industrial training***\n");
printf("\nOptional Modules:\n");
printf("H63EMA Electrical Machines\n");
printf("H63END Electronic Design\n");
printf("H63ECH Embedded Computing\n");
printf("H63PED Power Electronic Design\n");
printf("H63EDR Energy Conversion for Motor and Generator Drives\n");
printf("H63VIC Visual Information Computing\n");
printf("H63REN Renewable Generation Technologies and Control\n");
printf(" MM4MM Material Models and Modes of Failure \n");
printf("HG4MEM Mathematics for Engineering Management \n");
printf("MM3AD Advanced Dynamics of Machines\n");
printf("MM3AUT Introduction to Automotive Technology\n");
printf("N12402 Marketing strategy \n\n");
printf("Enter student's marks for the above modules:\n");
printf("**NOTE: Enter '-1' if the student did not take the module.\n");
for(i=18;i<37; i++){
scanf("%d", &marks[i]);
if(marks[i] >= 40){
total2 += marks[i];
modules2++;
if(i == 18|| i == 19)
credits2 += 30;
else
credits2 += 10;
}
}
saveMarks(id, marks);
if(credits2>=100){
avg1 = total1 / modules1;
avg2 = total2 / modules2;
weigh = (avg1)/3 + (avg2 * 2)/3;
if(weigh>=0 && weigh<40)
printf("\nStudent has failed.\nTotal of the last two years is: %.2f%\n", weigh);
else if(weigh>=40 && weigh<50)
printf("\nStudent has passed [Third Class].\nTotal of the last two years is: %.2f%\n", weigh);
else if(weigh>=50 && weigh<60)
printf("\nStudent has passed [Second Class, division II].\nTotal of the last two years is: %.2f%\n", weigh);
else if(weigh>=60 && weigh<70)
printf("\nStudent has passed [Second Class, division I].\nTotal of the last two years is: %.2f%\n", weigh);
else if(weigh>=70 && weigh<=100)
printf("\nStudent has passed [First Class].\nTotal of the last two years is: %.2f%\n", weigh);
}
else
printf("\nStudent has failed to progress to Year Four.\n");
weigh = results[count][1];
printf("%.2f %10.2f", results[count][0], results[count][1]);
tabulateMarks(results);
}
}
}
count++;
}
return 0;
}
void saveMarks(int x, int array[]){
FILE* fmarks = fopen("marks.dat", "a");
int i;
fprintf(fmarks, "\n%d", x);
for(i=0; i<37; i++){
if(array[i] == -1)
fprintf(fmarks, ",");
else
fprintf(fmarks, ",%d", array[i]);
}
fclose(fmarks);
}
void tabulateMarks(int array[][2]){
FILE* fresults = fopen("results.txt", "w");
int i, j, n, temp;
for(i=0; i<10; i++){
for(j=0; j<9; j++){
if(array[j][1]<array[j+1][1]){
temp=array[j+1][1];
array[j+1][1]=array[j][1];
array[j][1]=temp;
}
}
}
fprintf(fresults, "ID:\t\tFinal Mark:\n");
for(n=0; n<10; n++){
fprintf(fresults, "\n%d \t %d", array[n][0], array[n][1]);
}
fclose(fresults);
}
【问题讨论】:
-
"数组的每一行包含两个单元格,一个 int 和一个 float。"否:在
int results[10][2]中,每行的两个单元格都是int。如果您想要不同类型的变量,请使用struct的数组。 -
printf("%.2f %10.2f", results[count][0], results[count][1]);需要float或double参数,但您提供的是int参数。
标签: c arrays function multidimensional-array