# Thread: Determinant Calculation Program.... HELP PLZ

1. ## Determinant Calculation Program.... HELP PLZ

Hi guys, I have to make a program to print out the matrix from the file and its determinant. Here's the outline Im supposed to fill out:

Code:
```#include
#define N 10
#define NMAX 4

double determinant2(double a[N][N]) ;
double determinant3(double a[N][N]) ;
double determinant4(double a[N][N]) ;
void read(double a[N][N], int n) ;
void print(double a[N][N], int n) ;
int count_inversions(int b[N], int n)  ;

int
main(void)
{
double a[N][N] ;
double determinant ;
int n ;

/*
Get n, the size of the square array
*/

/*
Read the n by n array and then print it.
*/

/*
Find and print the determinant of the matrix
*/

}```
And heres what I got so far:

Code:
```#include<stdio.h>
#include<stdlib.h>
#define N 10
#define NMAX 4

{
int i,j;

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

void print(double a[N][N], int n)
{
int i,j;
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
printf("%.2lf\n", a[i][j]);
}
}

main ()
{
int n;

FILE *inp;
inp = fopen("matrix.txt", "r");

double a[N][N];
fscanf(inp, "%d", &n);

printf("The size of matrix is %d\n", n);

print(a, n);
}```
and the sample of the input file must be in the following format:

4
1 2 5 6
8 5 6 7
4 5 9 8
2 8 4 1

where the first element (4) indicates the size of square matrix(4x4)

As you can see Im trying to read the matrix from the input file and print it out on the screen, but the errors Im getting are:

assi2-.c: In function `main':
assi2-.c:42: parse error before `double'
assi2-.c:47: `a' undeclared (first use in this function)

If anyone can help me out with this, it be greatly appreciated. Thank you for looking.

2. For starters, read and print take 2 arguments, and you're only sending them one.

Also, I don't believe the way you declare matrix is legal, but don't quote me on that.

3. thanks for the reply, I just updated the reworked program and errors, still getting those few.

4. I got it to compile without any errors using this code:

Code:
```#include<stdio.h>
#include<stdlib.h>
#define N 10
#define NMAX 4

{
int i,j;
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
fscanf(inp, "%lf",&a[i][j]);
}
}

void print(double a[N][N], int n)
{
int i,j;
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
printf("%.2lf\n", a[i][j]);
}
}

main ()
{
int n;
double a[N][N];

FILE *inp;
inp = fopen("matrix.txt", "r");

fscanf(inp, "%d", &n);

printf("The size of matrix is %d\n", n);

print(a, n);```
but I am getting this error while running it......
http://img95.imageshack.us/img95/5729/errorzx8.jpg

Any ideas?

5. Your array shouldn't have the ampersand there in your scanf function.

6. hey thanks for pointing that out, I got rid of that, and it still has that error while running

7. 1. you need an ampersand in scanf
2. you said - you wanted to read a numbers from file? you should use fscanf then
3. In this case - fscanf requires file pointer - you should give 3rd parameter to your read function

8. 1. ok put back the ampersand
2. replaced scanf with fscanf in read function
3. how would the pointer work exactly? Something like this?

Code:
```void read(double a[N][N], int n)

{
int i,j, x;
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
fscanf(inp, "%lf", &a[i*x + j]);
}
}```
Please correct me if im wrong. Thank you.

9. Code:
```void read(double a[N][N], int n,FILE* inp)

{
int i,j;
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
fscanf(inp, "%lf", &a[i][j]);
}
}```

10. hey thanks a lot for your help vart!

I think it kinda worked, it prints out the numbers, but I thought it would print it out in the form of matrix, is anything Im missing?

here is the result screenshot

http://img172.imageshack.us/img172/9272/outputho6.jpg

11. you add \n after each number - you should print it only at the end of the line

12. thank you, thank you, thank you, I got it..finally........ (and it was supposed to be easy part of an assignment!).

Figuring out determinants is for tomorrow.

13. my question now, is for example lets say I have input file with 3 matrices of sizes 2x2, 3x3 and 4x4. How can I input all 3 matrices at the same time from 1 file? I tried putting while loop in read function but no success:

Code:
```void read(double a[N][N], int n, FILE* inp)

{
int i,j;
while(fscanf(inp,"%lf", &a[n][n])!=EOF)
{
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
fscanf(inp, "%lf", &a[i][j]);
}
}
}```
Code:
```4
4 6 8 3
2 1 3 7
4 5 7 9
1 4 2 5

3
1 5 6
8 5 2
3 0 4

2
1 5
2 8```
a sameple of input file:

14. > while(fscanf(inp,"%lf", &a[n][n])!=EOF)
fscanf(inp,"%d", &n );

Then your two loops, then you would read in just one matrix.

Assuming your N constant is big enough to hold any possible matrix you may have.

Returning the value of 'n' to the calling function would be a good idea, so you know how big it is.

15. ok this is what I changed it to:

Code:
```void read(double a[N][N], int n, FILE* inp)

{
int i,j;
fscanf(inp,"%d", &n);
for (i=0; i<n; i++)
{
for (j=0;j<n;j++)
fscanf(inp, "%lf", &a[i][j]);
}
}```
But it wont read the other matrices below the first one from the input file as above