从文件中读取一个3x3矩阵的则在C显示它 [英] Reading a 3x3 Matrix from a file an then displaying it in C

查看:138
本文介绍了从文件中读取一个3x3矩阵的则在C显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧首先,请与我裸视我只现在编码几个星期,我很新的,所以我的错误可能会看起来微不足道。

我只是需要帮助,我没有线索下一步去哪里我的code做的,我只是想打印的矩阵,但我不知道怎么办。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
FILE *输入;
INT I,J;
浮fullmatrix [3] [3];
为const char inp_fn [] =matrix.dat;/*打开文件*/
输入= FOPEN(inp_fn,R);如果((输入!=(FILE *)NULL))
{
    对于(I = 0; I&下; 3;我+ +)
    {
        为(J = 0; J&下; 3; J ++)
        {
            fullmatrix [I] [J] =输入;
        }
        的fscanf(输入%F);    }
    fclose函数(输入);
    的printf(%F%F%F \\ n%F%F%F \\ n%F%F%F \\ n,&安培;矩阵[I] [J]);
}
其他
    的printf(无法打开文件\\ N!);返回(0);
}

我说过对不起,如果这是一个愚蠢的问题,我只是不知道下一步去哪里


解决方案

 为(i = 0;我3;;我++)
{
    为(J = 0; J&下; 3; J ++)
    {
        fullmatrix [I] [J] =输入;
    }
    的fscanf(输入%F);}

应该是:

 为(i = 0;我3;;我++){
    为(J = 0; J&下; 3; J ++){
        的fscanf(输入%F,&安培; fullmatrix [I] [J]);
    }
}

和这条线不会做你认为可以:

 的printf(%F%F%F \\ n%F%F%F \\ n%F%F%F \\ n,&安培;矩阵[I] [J] );

您需要一个对自己使用循环再打印之一:

 为(i = 0;我3;;我++){
    为(J = 0; J&下; 3; J ++){
        的printf(%F,fullmatrix [I] [J]);
    }
    的printf(\\ n);
}

Okay firstly, please bare with me as I have only been coding for a couple of weeks now and I'm very new to it, so my mistakes will probably look trivial.

I just need help as I don't have a clue where to go next for my code, i just want to print the matrix but I don't know how.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[])
{
FILE       *input;
int        i, j;  
float      fullmatrix[3][3];
const char inp_fn[]="matrix.dat";

/*Open File*/
input = fopen(inp_fn, "r");

if( (input != (FILE*) NULL) )
{
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            fullmatrix[i][j] = input;
        }
        fscanf(input, "%f");

    }
    fclose(input);
    printf("%f %f %f\n %f %f %f\n %f %f %f\n", &matrix[i][j]);
}
else
    printf("Could not open file!\n");

return(0);
}

As I said sorry if this is a silly question I just don't know where to go next

解决方案

for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
    {
        fullmatrix[i][j] = input;
    }
    fscanf(input, "%f");

}

should be:

for(i=0; i<3; i++) {
    for(j=0; j<3; j++) {
        fscanf(input, "%f", &fullmatrix[i][j]);
    }
}

and this line won't do what you think either:

printf("%f %f %f\n %f %f %f\n %f %f %f\n", &matrix[i][j]);

You need to print it one by one on your own using loops again:

for(i=0; i<3; i++) {
    for(j=0; j<3; j++) {
        printf("%f ", fullmatrix[i][j]);
    }
    printf("\n");
}

这篇关于从文件中读取一个3x3矩阵的则在C显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆