0

For the following code, how can I find [A^-1] using pointers(equation for [A^-1]= 1/ det (A)? I am not sure whether pointers are used in the arithmetic or if they are used to call a value. Explaining what a pointer would be nice as I'm not exactly sure what they even do. I have most of the code written, except for this one part, so any help is much appreciated. Thanks in advance.

#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <bits/stdc++.h>
#define N 3

using namespace std;
template<class T>

void display(T A[N][N])
{
for (int i=0; i<N; i++)
{

for (int j=0; j<N; j++)     
cout << A[i][j] << "\t\t";
cout << endl;
}
}

template<class T>

void display(T A[N])

{

for (int i=0; i<N; i++)

{
cout << A[i]<< "\n";
}
}

void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)

{

int i = 0, j = 0;

for (int row = 0; row < n; row++)

{

for (int col = 0; col < n; col++)

{
if (row != p && col != q)

{

temp[i][j++] = A[row][col];

if (j == n - 1)
{

j = 0;

i++;

}

}

}

}

}

int determinant(int A[N][N], int n)
{

int D = 0; 

if (n == 1)

return A[0][0];

int temp[N][N]; 

int sign = 1; 

for (int f = 0; f < n; f++)

{
getCofactor(A, temp, 0, f, n);

D += sign * A[0][f] * determinant(temp, n - 1);
sign = -sign;
}

return D;
}

void adjoint(int A[N][N],int adj[N][N])
{
if (N == 1)
{

adj[0][0] = 1;

return;
}

int sign = 1, temp[N][N];

for (int i=0; i<N; i++)
{

for (int j=0; j<N; j++)
{
getCofactor(A, temp, i, j, N);
sign = ((i+j)%2==0)? 1: -1;
    adj[j][i] = (sign)*(determinant(temp, N-1));
}
}
}

bool inverse(int A[N][N]){

int det = determinant(A, N);

if (det == 0){

cout << "Singular matrix, can't find its inverse";

return false;
}

return true;
}

void computeInverse(int A[N][N], float inverse[N][N]){

int det = determinant(A, N);

int adj[N][N];

adjoint(A, adj);

for (int i=0; i<N; i++)

for (int j=0; j<N; j++)

inverse[i][j] = adj[i][j]/float(det);

cout<<"\nThe Inverse of the Matrix A is:"<<endl;

display(inverse);

}
int main()
{
system("cls");

int A[N][N] = {{-1, 4, -2}, {-3, -2, +1}, {+2, -5, +3}};

char X[N];

int B[N];

float inv[N][N];

cout<<"\nEnter a 3*3 Matrix A"<<endl;

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

cin>>A[i][j];

}

}

cout<<"\nEnter variables x, y, z for Matrix X"<<endl;

for(int i=0;i<N;i++){

cin>>X[i];

 }

 if (X[0] == 'x' && X[1] == 'y' && X[2] == 'z')

 cout<<"\nMatrix X is Valid"<<endl;

else{

cout<<"\nMatrix X is Invalid"<<endl;

return -1;

}

cout<<"\nEnter values for Matrix B"<<endl;

for(int i=0; i<N; i++)

cin>>B[i];

cout<<"\nMatrix A is:------->\n";

display(A);

cout<<"\nMatrix X is:------->\n";

display(X);

cout<<"\nMatrix B is:------->\n";

display(B);

bool isInverseExist = inverse(A);

if (isInverseExist)

computeInverse(A, inv);

return 0;
}
5
  • 1
    Possible duplicate of What are the differences between a pointer variable and a reference variable in C++? Commented Apr 3, 2018 at 2:39
  • A pointer is simply a variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. For example, while int a = 5; stores the immediate value 5 as its value, int *b; creates a pointer to int, and b = &a; stores the address of a (the memory address where 5 is currectly stored) as its value. Since b points to the address where 5 is stored, if you change that value (e.g. *b = 6;) 6 is now stored at the address where 5 was before. Commented Apr 3, 2018 at 6:11
  • If you expect other people to read your question, you should take the trouble to properly indent your code. Commented Apr 3, 2018 at 8:56
  • Caution: as a beginner, never use float. With only 7 digits precision, it is easy to lose accuracy and end up without a meaningful answer. Always use double instead of float unless you really, really know what you are doing. And even then, usually it's a mistake. Commented Apr 3, 2018 at 9:01
  • @RCE11 I attempted to answer your earlier question. When you acknowledge my "answer" below this question (just leave me a comment, that will work), I'll go ahead and delete it since it doesn't belong here. But your question is a valid one and deserves an answer. Commented May 23, 2019 at 14:37

1 Answer 1

1

Okay, Here is a simple pointer example using a char array. This can work with other data types as well. Don't remember where I read it; Think of a pointer as an empty bottle.

lets break this down in English first then I'll share a simple example that can be compiled. Imagine that you have 10 m&m's sitting in front of you on a table. These m&m's represent a character array that we will call char mm[10]; Assume that this array is filled with chars that represent the color of all 10 of the m&m's

Now, we don't want to leave our candy's on the table all day so we will store all ten of the m&m's in a special jar specifically made for the m&m's. this jar will be called char* mm_ptr;

So now we are left with a table of m&m's and a jar sitting next to the pile. The next step is to fill the jar with the m&m's and you do it like this: mm_ptr = mm;

You now have a "jar full of m&m's". This allows you to access the m&m's directly from the jar!

Here is a Working example of of putting m&m's into a jar:

#include <iostream>
//using namespace std;


int main(){//The main function is our table

        char mm[10];    //Our pile of m&m's

        for(int i=0; i<10; i++){
                if(i < 5){
                        mm[i] = 'b';    //Lets say that half of the m&m's are blue
                }else{
                        mm[i] = 'r';    //and that the other half is red
                }
        }

        char* mm_ptr;           //This is our bottle that we will 
                                //use to store our m&m's

        mm_ptr = mm;    //this is us storing the m&m's in the jar!

        for(int i=0; i<10; i++){//Lets dump those candies back onto the table!
                std::cout<<mm_ptr[i]<<std::endl;

        }
        return 0;
}

A correctly initialized pointer will work almost exactly like a normal array

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

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.