Matrices in xcode-Beginners coding

My homework is to create a computer program that calculates and outputs the inverse of the 2x2 matrix
(-2 -5)
(1 3)

I wrote this, but it didn't work. I posted the errors as comments:

#include<iostream>

using namespace std;

int main()
{
int a, b, c, d, f;
double matrix;
double inv;

a=-2;
b=-5;
c=1;
d=3;
f=a*d-b*c;
matrix=(a b) (c d); // !Expected `)' before 'b'
inv=(d/f -b/f) (-c/f a/f); // !Expected `)' before 'a' and '((d/f) -(b/f)) cannot be used as a function.

cout << "The inverse of the matrix " << matrix << "is " << inv << endl;

return 2;
}

If anyone can help me figure this out I would really appreciate it.
You going to have to use a 2D array to make a matrix:

double matrix[2][2];
double inv_matrix[2][2];

Then assign each element individually:

1
2
3
4
matrix[0][0] = -2;
matrix[0][1] = -5;
matrix[1][0] = 1;
matrix[1][1] = 3;


matrix = (a b) (c d);
inv = (d/f -b/f) (-c/f a/f);


These statements are not accepted as code. You must use the * for multiplication. It is not implied like it is in Math.

Maybe try matrix = (a*b) * (c*d) and inv = (d/f - b/f) * (c/f - a/f)

Even though I took Linear Algebra less than a year ago I remember nothing. I know its sad but I have never been interested in pure math and I spend most of my time learning newer programming languages and web design. This does look familiar though. lol

Hope this helps.
Last edited on
@roberts thank you, but how does that give me the inverse matrix? I tried that and all I got was gibberish.

@icethatjaw It's not multiplication. I was trying to display the matrix in that form.
It doesn't give you the inverse. It at least tells you how to represent a matrix in C++. It seems apparent you don't understand how more complex math terms are represented in C++. A matrix is a multi-dimensional term. A double is dimensionless. Arrays are essentially 1D. 2D matrices can take more than one row and need to be represented as:

intrinsic datatype matrixname[rows][cols]

or:

intrinsic datatype matrixname[cols][row]

However you prefer.

Matrices can also be 3D, 4D, 5D, etc.

I'd recommend you get the book "3D Math Primer for Graphics and Game Development" if you want to know more. The math behind and representation of matrices in a computer language is Chap 7, 8 and 9 in that book with all the previous chapters building up to it.
That sounds really intense. I don't think that's what my professor is looking for. It's only our second week into the class. This was the actual assignment he posted:

Create a computer program that calculates and outputs the inverse of a
2x2 matrix.
Recall: If given a matrix of the form:
(a b)
(c d)

You may recall that this matrix's inverse will be given by the formula:
(d/f b/f)
(c/f a/f)

where
f = ad - bc.
Example: With the matrix:
(2 -1)
(4 -3),
we have that: a = 2, b = -1, c = 4, and d = -3. Then f = ad - bc = 6 + 4 = -2, and so the inverse is:
(-3/-2 --1/-2)
(-4/-2 2/-2)

plugging things directly into the formula above.
Your program should display to the monitor something like:
The inverse of the matrix is:
(1.5 -0.5)
(2 -1)
(a) Use your program to calculate and display the inverse of

(-2 -5)
(1 3)

(b) Do the same thing for:

1 2
3 4

Hint: You should declare 4 variables (at least), and assign them to di fferent
values in each part of the problem ((a) and (b)). You are to use your
program to perform all calculations (none by hand or in your head).
Your program should correctly calculate the inverse in both cases and
display them like in the example above.
We did learn how to create a table. Would that be helpful in this situation?
A table? Are you talking about formatted output or a multi-dimensional array?

Well, it seems your professor is NOT wanting you to use a multi-dimensional array since he or she is requesting you use at least four variables(details are important), so as a starting point:

float a, b, c, d, f;

a = -2; b = -5; c = 1; d = 3;

and

f = a * d - b * c;

then you simply output the results using cout. You started off good, but lost it on the presentation:

cout << "The inverse of the matrix " << endl << a << " " << b << endl;
cout << c << " " << d << endl;
cout << "is" << endl;

This is how you would present it. It's not all of what you need to do, but it is a starting point for knowing how you need to output. The inverse will simply be your variables divided by the determinant inline with the output statement.

Last edited on
Thank you so much. I finally got it. The table we learned would probably be formatted output. It was just basically setting the width of a column and justifying text either right or left.
Topic archived. No new replies allowed.