c - 将多维数组写入文本文件

标签 c arrays function multidimensional-array

我为作业编写了代码。它基本上是一个程序,提示用户输入学生的分数并决定学生是否有资格进入下一级别。 该程序应该将学生的 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);
}

最佳答案

1- Each row of the array consists of two cells, one int and one float. (What am I supposed to initial the array's type as, int or float?).

mbass 和 WeatherVane 是对的。您不想在这里使用二维数组,您想使用结构体。像这样的东西:

typedef struct 
{
  int studentID;
  float avgScore;
}StudentData_t;

StudentData_T myStudentData[10];

(结构名称是匿名的,因为它捆绑到 typedef 中)

2- The output file always consists of zeros. Every single cell.

我不知道这是否是唯一的问题,但是:

printf("\nEnter student ID number: ");
scanf("%f", id);
id = results[count][0];

scanf() (顺便说一句,这很可怕,但每个人都从某个地方开始),将第二个参数作为指针。所以你想要

scanf("%f", &id);

因此,来自用户的数据被加载到 id 中。但现在这是没有意义的,因为 id 会被 results[x][0] 覆盖,而你将其初始化为零。

此外,saveMarks 适用于大小为 37 的数组,而 tabulateMarks 适用于大小为 10 和 2 的二维数组。两者都使用名称 array。来吧,伙计,这简直令人困惑。名字很重要。

关于c - 将多维数组写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34341946/

相关文章:

r - 如何在R中使用重复生成正数

c - 大于 200 万的埃拉托色尼筛 [C]

atoi 的 c 字符串版本

c - 循环不会中断。 C程序

php - 找出数组中的序列相似性

JavaScript,执行函数

c - 打印文件内容

c - ... 的多个定义(链接器错误?)

java - 按矩阵中的公共(public)属性对值进行分组

c - 二维数组 : Alphabetical Sorting in C