【问题标题】:Summing elements of matrix from a file从文件中求和矩阵的元素
【发布时间】:2014-10-14 10:39:47
【问题描述】:

我在文本文件中有一个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]

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

【问题讨论】:

  • 请缩进你的代码。
  • 我看不到您将文件值读入矩阵的位置a[][]
  • 我认为这个 'FILE *fp=fopen("D:\1980.txt","r");'应该是这个 'FILE *fp=fopen("D:\\1980.txt","r");',你错过了 \\
  • a[i][j+1]; 可能是 j+1 == 24 和 i&lt;=999 --> i&lt;=99float a[1000][24]; , i&lt;=99 --> i&lt;=999

标签: c matrix


【解决方案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);          
}

【讨论】:

  • 谢谢阿迪尔。代码现在按应有的方式运行。谢谢你的帮助。感谢您指出错误。 :)
【解决方案2】:

这段代码有很多错误。我已经格式化了你的 main 函数并添加了 cmets 以指出错误:

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);

【讨论】:

  • 嗨,乔纳森,感谢您的 cmets。是的,你的论点完全正确。我现在可以识别我的错误并让代码运行。非常感谢。
  • @krishcal23 如果这解决了您的问题,我鼓励您通过单击复选标记来接受答案。如果有一些问题可以解决您的问题,您也可以考虑返回接受其他问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2023-03-25
  • 2019-08-24
  • 2017-03-05
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多