我为作业编写了代码。它基本上是一个程序,提示用户输入学生的标记,并决定学生是否有资格进入下一级别。 该程序应该将学生的ID号和他/她的标记写在* .dat文件中,并根据它们在文本文件中的优点以表格的形式按降序输出所有学生ID。 我使用了一个多维数组(结果[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);
}
答案 0 :(得分:1)
mbass和WeatherVane是对的。你不想在这里使用二维数组,你想使用一个结构。类似的东西:1-数组的每一行由两个单元格组成,一个是int,一个是float。 (我应该将数组的类型初始化为int,float?)。
typedef struct
{
int studentID;
float avgScore;
}StudentData_t;
StudentData_T myStudentData[10];
(结构名称是匿名的,因为它捆绑在typedef中)
2-输出文件始终由零组成。每个单元格。
我不知道如果这是唯一的问题,但是:
printf("\nEnter student ID number: ");
scanf("%f", id);
id = results[count][0];
scanf()(这是可怕的btw,但每个人都从某处开始),将第二个参数作为指针。所以你想要
scanf("%f", &id);
因此来自用户的数据被加载到INTO id
中。但是现在这一点毫无意义,因为id
被结果[x] [0]覆盖,你初始化为零。
另外,saveMarks工作的数组大小为37,而tabulateMarks的工作尺寸为10和10。 2.两者都使用名称array
。来吧,这简直令人困惑。名字很重要。
答案 1 :(得分:-1)
您正在描述struct我的朋友。