Mình mới học C mong các bạn giúp đở. Mình có đoạn code sau, khi mình chạy thì không có lỗi nhưng kết quả thi không đúng.Mong các bạn giúp mình. Xin cám ơn!
Bài này yêu cầu đọc 2 ma trận từ file rồi thực hiện phép cộng 2 ma trận. Mình có kèm theo hình ảnh về kết quả mà mình chạy!
#include <conio.h>
#include <stdio.h>
int read_file(int array[100][100])
{
FILE *fp;
char file_name[25];
int c,m, n;
int j=0;
int i=0;
//int array[100][100] = {{0}};
int temp = 1;
printf("Enter the name of file you want to open:");
gets(file_name);
fp = fopen(file_name,"r");
if(fp == NULL){
perror("Error!Check your file again");

}

while(true){
c = fgetc(fp);
if(c == EOF){
break;
}
if (c == 45) temp = -1;

if((c >= 48) && (c <= 57)){
array[i][j] = array[i][j] * 10 + (c - '0');
}else if (c == 10){
i++;
j = 0;
}else if(c == 44){
array[i][j] *= temp;
temp = 1;
j++;
}
}

for(m = 0; m <= i; m++){
for(n = 0; n <= j; n++){
printf("%d ", array[m][n]);
}
printf("
");
}
fscanf(fp,"%d",&array[m][n]);
fclose(fp);
}
void add_2matrixes( int a[100][100], int b[100][100])
{
FILE *fp;
int c[100][100];
int m,n;
fp=fopen("D:\\output.txt","w");
if(fp==NULL){
perror("
The file does not exist");
}
for(int i=1;i<=m;i++) <-- Theo mình thi sai sót xuất phát từ đây!
for( int j=1;j<=n;j++){
c[i][j]=a[i][j] + b[i][j];
printf("%d ",c[i][j]);
fprintf(fp,"%d ",c[i][j]);
}
fclose(fp);
}


int main()
{
FILE *fp;
int a[100][100],b[100][100],c[100][100];
int m,n;
printf("
Matrix 1:");
read_file(a);
printf("
Matrix 2:");
read_file(b);
add_2matrixes(a,b);


return 0;
}



- - - Nội dung đã được cập nhật ngày 31-07-2014 lúc 01:32 PM - - -

Mong các bạn giúp đỡ mình, xin cám ơn!