0

I have a 1000x24 matrix in a text file and i want to sum the adjoining columns of the whole matrix e.g. col1+col2, col2+col3, col3+col4,... col23+col24 and store these values in a separate text file.

My code runs like this:

#include <stdio.h>
int main()
{
int i,j;
float a[100][24];
FILE *fp=fopen("D:\1980.txt","r");
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();           
}

for example if a matrix in file 1980.txt is like [1 2 3;4 5 6;7 8 9], the output in sum.txt should be [3 5;9 11; 15 17].

However, the code is not working and i am getting only a blank file. Please help on this matter. Thanks.

2
Contributors
1
Reply
8
Views
2 Years
Discussion Span
Last Post by Edward_3
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.