Quantcast
Channel: Dạy Nhau Học - Latest topics
Viewing all articles
Browse latest Browse all 34350

Hỏi về cách đọc kích thước ma trận từ file

$
0
0

@Tran_Viet_Hung wrote:

Dạ em đang luyện tập một bài tập yêu cầu tính kích thước của một ma trận từ file ạ, ý tưởng của e là e đếm tất cả phần tử, đếm số hàng rồi chia ra số cột ạ, nhưng e làm thế này chạy ko ra kết quả ạ, mọi người giúp e với:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void read(int *m, int *n, int *a, FILE *f);
int main()
{
    int m=0,n=0;
    FILE *f;
    int *a;
    a=(int *)malloc(m*n*sizeof(int));
    f=fopen("input.txt","rt");
    read(&m,&n,a,f);
    printf("%d %d",m,n/m);
}
void read(int *m, int *n, int *a, FILE *f)
{
    char row[100],kitu[100];
    int x;
    while(fgets(row,100,f))  //tinh so hang
    {
        *m=*m+1;
    }
    while(!feof(f))    //tinh so phan tu
    {
        if(fgets(kitu,100,f)!=NULL)
        {
            x=strlen(kitu);
            for(int i=0;i<x;i++)
            {
                if(kitu[i]!=' '&&kitu[i]!='\n')
                {
                    *n=*n+1;
                }
            }
        }
    }
}

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 34350

Trending Articles