c++ - 如何在代码中正确使用指针

标签 c++ pointers

对于下面的代码,我如何使用指针找到[A^-1](等式[A^-1]= 1/det (A)?我不确定算术中是否使用了指针,或者是否它们被用来调用一个值。解释什么是指针会很好,因为我不确定它们甚至做了什么。我编写了大部分代码,除了这一部分,所以非常感谢任何帮助。谢谢提前。

#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;
}

最佳答案

好的,这是一个使用字符数组的简单指针示例。这也适用于其他数据类型。不记得我是在哪里读到的;将指针想象成一个空瓶子。

让我们先用英语对此进行分解,然后我将分享一个可以编译的简单示例。想象一下,您面前的 table 上有 10 个 m&m。 这些 m&m 表示一个字符数组,我们将其称为 char mm[10]; 假设该数组填充了表示所有 10 个 m&m 颜色的字符

现在,我们不想整天把糖果留在 table 上,所以我们将把所有 10 颗 m&m's 放在一个专门为 m&m's 制作的特殊 jar 里。这个 jar 将被称为 char* mm_ptr;

所以现在我们只剩下一张 m&m 的 table 和一堆旁边的 jar 。下一步是用 m&m 填充 jar ,您可以这样做:mm_ptr = mm;

你现在有一个“装满 m&m's 的 jar ”。这使您可以直接从 jar 中访问 m&m!

这是将 m&m 放入 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;
}

一个正确初始化的指针会像普通数组一样工作

关于c++ - 如何在代码中正确使用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49621384/

相关文章:

c++ - 如何选择所有轮廓?

c++ - C++ Visual Studio 和 gcc 编译器中的空指针和空地址

转换为 void**,出于什么原因?

c++ - 取消引用这个指针有什么问题?

c - 如果我将值重新分配给 c 中的字符指针会发生什么

c++ - 我如何在单独的头文件中的结构中定义一个 char* 数组?

c++ - 使用 C++ 进行机器人实时编程

c++ - 我怎样才能避免名称混淆?

c++ - Winsock 中 SOCKET 对象的范围是什么?

c - 包含 SDL_Rect 的二维结构数组的 SDL_BlitSurface 参数指针