Winston_Churchill
Bazinga!
+521|7005|Toronto | Canada

So I've got a lab due tomorrow and our assignment is to input a matrix into an array and then preform row operations to get the inverse.  So we have to have an A matrix that is inputted and a B matrix that is the identity matrix, and then do the row ops to get the inverse of A.  I know how to scan in the matrix A, but I have absolutely no idea whatsoever on how to preform row operations (multiplying, dividing, switching rows etc) on both the A and B matricies.  Does anyone know how to do this?

This is what I have so far - just freeing the memory for dynamic array and about to scan in the numbers, but I'm stumped as to how to do the row ops.

Code:

//Lab 6 of APS105 - Computing a Matrix Inverse

#include <stdio.h>

int main(void)
{
        int M, N;
        printf("Enter the dimension of square matrix:\n");
        scanf("%d", &M);
        N = M;

        //Allocate memory for rows in matrix
        double **matrix = (double**)malloc(sizeof(double*)*M);
        //Allocate memory for columns in matrix

        int i;
        for(i=0; i<M; i++)
        {
                matrix[i] = (double*)malloc(sizeof(double)*N);
        }
        printf("Enter the %d entries of the matrix:\n", size*size);
        scanf("






        for(i=0;i<M;i++)
        {
                free(matrix[i]);
        }
        free(matrix);



}
san4
The Mas
+311|6954|NYC, a place to live
Is the problem that you don't know how to get the inverse of a matrix or that you just don't know how to do it in C?
Scorpion0x17
can detect anyone's visible post count...
+691|7032|Cambridge (UK)
@WC: find a book on C programming and read it FFS. Sheesh. Why don't you just post the assignment and "please do my homework for me" next time. Either that or, if you want any help from me at least, try getting to the point where you've got an algorithm or program actually written but not working correctly before coming here. As I think I've said to you before, you're not going to learn anything by asking us to just give you the answer.
Winston_Churchill
Bazinga!
+521|7005|Toronto | Canada

Scorpion0x17 wrote:

@WC: find a book on C programming and read it FFS. Sheesh. Why don't you just post the assignment and "please do my homework for me" next time. Either that or, if you want any help from me at least, try getting to the point where you've got an algorithm or program actually written but not working correctly before coming here. As I think I've said to you before, you're not going to learn anything by asking us to just give you the answer.
I have a book and I've read everything we have on arrays but I have absolutely no idea how to do it.  I dont see how I could write an algorithm or program if I have no clue whatsoever on how to do this kind of thing.  Out of our entire class maybe 5% know how to do this.  I'm not asking for the answer, if you could guide me on how to do this kind of thing it would be really helpful.  I've looked up many different ways on the internet but all of them are using functions we havent learned yet.
Scorpion0x17
can detect anyone's visible post count...
+691|7032|Cambridge (UK)
Well, all I see is you saying "do my homework for me please", but, if that's not the case, then I apologise, but you need to explain the problem more clearly...

what part do you not understand?

do you not understand how to 'add and multiply values held in an array' in C?

or, do you not understand how to 'calculate the inverse of a matrix'?
Winston_Churchill
Bazinga!
+521|7005|Toronto | Canada

Scorpion0x17 wrote:

Well, all I see is you saying "do my homework for me please", but, if that's not the case, then I apologise, but you need to explain the problem more clearly...

what part do you not understand?

do you not understand how to 'add and multiply values held in an array' in C?

or, do you not understand how to 'calculate the inverse of a matrix'?
That.  I know how to calculate the inverse of a matrix.  Sorry, if I wasn't clear enough

I could probably do it if I did each value one by one but that would take forever, especially if I have to do the same operation to the identity matrix.

And I already got a zero on the lab now, but as you can see I'm more concerned with understanding what I'm doing than getting the mark
san4
The Mas
+311|6954|NYC, a place to live
I don't know how to find the inverse of a matrix, but I think you have to do most matrix operations element by element. Is the problem that the number of rows and columns in the matrix can vary? Do the mathematical operations change depending on the size? If so, this sounds like a hard problem.
Winston_Churchill
Bazinga!
+521|7005|Toronto | Canada

san4 wrote:

I don't know how to find the inverse of a matrix, but I think you have to do most matrix operations element by element. Is the problem that the number of rows and columns in the matrix can vary? Do the mathematical operations change depending on the size? If so, this sounds like a hard problem.
Yeah, the matrix is dynamic, so it has to apply for (theoretically) all sizes
Scorpion0x17
can detect anyone's visible post count...
+691|7032|Cambridge (UK)
@WC: Ah, I see what you're asking now, and I'm afraid there's no simple answer - well, that is to say, the reason all you could find via google is solutions using functions is that using functions is the only sensible way to do it...

Ok, now, seeing as you say you haven't done functions I'll give you a quick overview - a function is, generally, a short piece of code that caries out one specific operation.

e.g. a matrix-matrix multiplication can be broken down into a series of row-column operations on the source matrices - this row-column operation is an ideal candidate for a function in C - in that it is applied repeatedly to differing rows and columns when doing a matrix-matrix operation.

I can't recall the matrix inversion routine off the top of my head (in fact, if you do a search I believe there's a thread round here somewhere where I ask how it's done), but from what I can recall it similarly breaks down into a number of simple operations that are performed on different regions of the source matrix.

It is those operations that get put into functions.

Now, any algorithm involving functions can be written without said functions, but usually the code is hideously complicated - so I'll leave you to work that bit out...

But basically, you need to simply access the individual component values in the source matrix directly, thus:

Code:

double n;
int i, j;
...
n=matrix[i][j];
...
Hope that helps...

Last edited by Scorpion0x17 (2008-11-08 14:14:40)

Board footer

Privacy Policy - © 2025 Jeff Minard