1
$\begingroup$

I'm trying to implement the multiplication of two matrices something like this picture in c langage. enter image description here

I want to read the numbers from a text file of x and store it later in an array the code that i tried is below
So the problem that I have is when i use the static method Complex x[4] = { { 1,0 },{ 0,-1 },{ 1,1 },{ 4,-1 } }; it's work but when i want to read it from input.txt Complex x[4] it does not work what i missed.

typedef struct Complex
{
    double real;
    double image;
}Complex;

int main()
{
   Complex x[4] = { { 1,0 },{ 0,-1 },{ 1,1 },{ 4,-1 } };
   Complex y[4] = { { 0,1 },{ 1,-1 },{ 2,-3 },{ 4,0 } };
   FILE *input;
   input = fopen("input.txt", "r");
   if (sensorInput == NULL ) {
        printf(stderr, "Cannot open file\n");
        return 1;
    }
    else {
        for (i = 0; i<4; ++i)
        {
                fscanf(sensorInput, "%lf + j*%lf\t", &x[i]); 
                printf("\n");
        }
    }

}

input.txt
1,0
0,-1
1,1
4,-1

$\endgroup$
1
  • $\begingroup$ There are matrix manipulation packages for C, C++, FORTRAN, Python and Rust -- and those are just the ones that I know about. They handle input/output, math, linear algebra, etc., etc.. $\endgroup$
    – TimWescott
    Mar 19, 2021 at 16:15

1 Answer 1

1
$\begingroup$

The line

                fscanf(sensorInput, "%lf + j*%lf\t", &x[i]); 

should be

                fscanf(sensorInput, "%lf,%lf\t", &x[i].real, &x[i].imag); 

Notice that C++ provides complex type, they support the common complex operations, and provide an io, and they can read complex numbers writen as (1,3) directly file_in >> x[i], once you have a istream

Note that this type of question you should ask in stackoverflow instead.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.