从文件中求和矩阵的元素

时间:2014-10-14 10:39:47

标签: c matrix

我在文本文件中有一个1000x24矩阵,我想总结整个矩阵的相邻列,例如col1+col2col2+col3col3+col4,... col23+col24并将这些值存储在单独的文本文件中。

我的代码是这样的:

#include <stdio.h>

int main()
{
    int i,j;
    float a[100][24];

    FILE *fp=fopen("D:\1980.txt","r"); //missing one \ here, should be \\
    FILE *fp1=fopen("D:\\sum.txt","w");

    for (i = 0; i <= 99; i++)
    {
        for (j = 0; j <= 23; j++)
        {
            a[i][j]= a[i][j] + a[i][j+1];
        }
    }

    for(i = 0; i <= 999; i++)
    {
        for(j = 0; j <= 23; j++)
        {
            fprintf(fp1,"%.2f\t",a[i][j]);
            fprintf(fp1,"\n");
        }
    }

    fclose(fp);
    fclose(fp1);
    getch();           
}

例如,如果文件1980.txt中的矩阵与[1 2 3;4 5 6;7 8 9]类似,则sum.txt中的输出应为[3 5;9 11; 15 17]

但是,代码无效,我只得到一个空白文件。请帮忙解决这个问题。

2 个答案:

答案 0 :(得分:1)

首先你没有正确指定数组的大小。我假设它是1000X24。你做的第二个错误是你没有从文件中输入。第三个错误是在第20行打印。你必须在第一个for循环之后打印“\ n”。这是你在使用代码。

#include <stdio.h>
int main()
{
int i,j;
float a[1000][24];
FILE *fp=fopen("1980.txt","r");
FILE *fp1=fopen("sum.txt","w");
for (i=0;i<1000;i++)
{
    for (j=0;j<24;j++)
    {
        fscanf(fp,"%f",&a[i][j]);
        //a[i][j]= a[i][j]+a[i][j+1];
    }
}
for(i=0;i<1000;++i)
   for(j=1;j<24;++j)
       a[i][j-1]+=a[i][j];

for(i=0;i<1000;i++){
    for(j=0;j<23;j++)
        fprintf(fp1,"%f\t",a[i][j]);
    fprintf(fp1,"\n"); 
}
fclose(fp);
fclose(fp1);          
}

答案 1 :(得分:0)

这段代码存在很多问题。我已将您的主要功能格式化,并在评论中添加以指出错误:

int i,j;
float a[100][24];                   // This is only a 100x24 matrix your question asks for 1000x24 and you try to read 1000 below as mentioned by BLUEPIXY. float a[1000][24];
FILE *fp=fopen("D:\1980.txt","r");  // You are not escaping the '\' as mentioned by UniCell. FILE* fp = fopen("D:||1980.txt", "r");
FILE *fp1=fopen("D:\\sum.txt","w");
for (i=0;i<=99;i++)                 // Again only initializing a 100x24 matrix as mentioned by BLUEPIXY, but you should also check that you haven't read past the end of the file. for(i = 0; i < 999 && fgetc(fp) != EOF; i++)
{
    // As mentioned by haris you never read from the file. I have included some commented code inline to help with that.
    // int total = 0;
    // int size = 0;
    // local[25];

    // while(total < 25){
    //     if(fscanf(fp,"%f",local + size)){
    //         size++;
    //         total++;
    //     }
    //     else{
    //         if(size > 1){
    for (j=0;j<=23;j++)             // This will need to be switched to use total and size. for(j = total - size;j < total;j++)
    {
        a[i][j]= a[i][j]+a[i][j+1]; // This will need to be switched to use local. a[i][j] = local[j] + local[j + 1];
    }
    //         }
    //         else if(size == 1){
    //             a[i][total] = FLT_MIN;
    //         }
    //         if(fgetc(fp) != ';'){
    //             break;
    //         }
    //         else{
    //             size = 0;
    //         }
    //     }
    // }
}
for(i=0;i<=999;i++)
{
    for(j=0;j<=23;j++)
    {
        fprintf(fp1,"%.2f\t",a[i][j]);
        fprintf(fp1,"\n");
    }
}
fclose(fp);
fclose(fp1);
相关问题