1

I first apologize if some of you find my problem stupid and easy to solve, but I am a very beginner in "c".

My task is to create an inverse of a 3x3 matrix using different functions.

What I am trying to do now is to tell the user to input values for the 3x3 matrix and then print them. I made 2 functions which read and print the values, but I have problems with calling them since I cannot directly call an array in printf.

For now I am able to run the program, enter the values and print a wrong result which leads to a not responding program.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 3 //defining the size of the matrix (3x3)

//prototyping the functions used to calculate the inverse of the matrix
void readMatrix(double a[SIZE][SIZE]);
void printMatrix(double a[SIZE][SIZE]);


main()
{
    double a[SIZE][SIZE];
    int i,j;


   printf("Enter the values for the matrix:\n", i, j);
   readMatrix(a);
   printf("Your Matrix:%d\n",a[i][j]);
   printMatrix(a);

   return 0;
}


//function 1
//letting the user to enter a matrix
void readMatrix(double a[SIZE][SIZE]){


    int i,j;

    for(i = 0; i < SIZE; i++){
        for(j = 0; j < SIZE; j++){
            scanf("%d", &a[i][j]);
        } 
    } 
}

//function 2
//outputing the given matrix
void printMatrix(double a[SIZE][SIZE]){

    int i,j;

    for(i = 0; i < SIZE; i++){
        for(i = 0; i < SIZE; j++){
            printf("Your matrix is: %d", a[i][j]);
       }
   }
}
3
  • 1
    scanf("%d", &a[i][j]); --> scanf("%lf", &a[i][j]); Commented Jun 23, 2017 at 6:10
  • 1
    I don't know what "I cannot directly call an array in printf" means, but your printMatrix contains a silly typo. Something you are supposed to be able to check for by yourself. And why are you using %d to print your matrix values? Commented Jun 23, 2017 at 6:11
  • 1
    for(i = 0; i < SIZE; j++){ printf("Your matrix is: %d", a[i][j]); --> for(j = 0; j < SIZE; j++){ printf("Your matrix is: %f\n", a[i][j]); Commented Jun 23, 2017 at 6:11

4 Answers 4

2

In printf and scanf, it is crucial that you pass the exact format specifier that matches the type of the pointer to the variable. If your format specifier doesn't match the supplied argument, the result is undefined behaviour.

In effect, this

scanf("%d", &a[i][j]);

Needs to be replaced with

scanf("%lf", &a[i][j]);

And the same for printf("Your matrix is: %d", a[i][j]); -> printf("Your matrix is: %lf", a[i][j]);

Also, in printMatrix, you've used the loop variable i in the inner loop twice. What you want is

for(i = 0; i < SIZE; i++){
    for(j = 0; j < SIZE; j++){ 
          printf("%lf ", a[i][j]);
    printf("\n");
}

Edit: As pointed out by @cse in the comments, remove this line in main:

printf("Enter the values for the matrix:\n", i, j);

Since at this point, i, and j are not initialised, they'd contain junk.

Sign up to request clarification or add additional context in comments.

3 Comments

Very strange..I succeeded executing it once, but most of the times i get - "A problem caused the program to stop working correctly. Windiows will close the program and notify you if a solution is available. Also, do you have an idea how can I print it in order to look like a real 3x3 matrix.
You're printing it transposed. Also a good way to pave fault when dealing with larger arrays... granted, the input is transposed too so it isn't evident.
The program is crashing because: In line printf("Your Matrix:%d\n",a[i][j]); of main() function, since variable i and j are not initialized so it contains garbage value. So don't print value at a[i][j] because it may cause of segmentation fault. OR initialize i and j with a valid value i.e. which is a valid index in array double a[][]
1

in printMatrix, there is a infinite loop which will definitely lead your program to no responding. Should be:

   for(j = 0; j < SIZE; j++){
        printf("Your matrix is: %d", a[i][j]);
}

Comments

1

For printing the output: you will want to actually print out the entire thing rather than one value after another, correct?

printf ("matrix is:\n")
char outstr[64];            //arbitrary but plenty big enough
char * pout;                 //points to a point in the string
for(j = 0;  j < SIZE;  j++){
    pout = outstr;
    for(i = 0;  i < SIZE;  i++){ 
        pout += sprintf (pout, "%lf," a [i][j]);
    *(--pout) = 0;      //end the string one char earlier (dangling ',')
    printf ("[%s]\n", outstr);
}

Will print:

matrix is:
[1,2,3]
[4,5,6]
[7,8,9]

Where the numbers are of course the ones in the array.

Also unless you were intending on filling the matrix in a columnar fashion, you should switch the i and j loops on the input function. You are storing the matrix in memory transposed. (This code assumes that you are not)

Comments

1

There are following problems with above code:

  • In line printf("Your Matrix:%d\n",a[i][j]); of main() function, since variable i and j are not initialized so it contains garbage value. So don't print value at a[i][j] because it may cause of segmentation fault. OR initialize i and j with a valid value i.e. which is a valid index in array double a[][]. Also you can change line printf("Enter the values for the matrix:\n", i, j); in main() to printf("Enter the values for the matrix:\n");. Because i and j are not being used here.
  • In line scanf("%d", &a[i][j]); of function void readMatrix(double a[SIZE][SIZE]). Since you are reading a double primitive data type then you should use %lf formatter instead of %d. Same for line printf("Your matrix is: %d", a[i][j]) in function void printMatrix(double a[SIZE][SIZE]).
  • In line for(i = 0; i < SIZE; j++) of function void readMatrix(double a[SIZE][SIZE]). It should be for(j = 0; j < SIZE; j++) i.e. the variable to be used in inner loop should be j not i.

You can find working code here which is after correcting the code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.