Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序输出问题_C++_Function_Multidimensional Array - Fatal编程技术网

C++ 程序输出问题

C++ 程序输出问题,c++,function,multidimensional-array,C++,Function,Multidimensional Array,下面是我的程序,我对行列式函数有一个问题 文件输入为: 2 1 0 0 1 3 8 9 1 3 5 2 -2 3 -1 0 对于第二个矩阵,它正在读取输入文件中矩阵2的行列式函数的结果,对于我的代码可能出了什么问题,有什么想法吗 #include <iostream> #include <fstream> #include <cmath> using namespace std; const int maxsize = 10; ifstream fin

下面是我的程序,我对行列式函数有一个问题

文件输入为:

2
1 0
0 1
3
8 9 1
3 5 2
-2 3 -1
0
对于第二个矩阵,它正在读取输入文件中矩阵2的行列式函数的结果,对于我的代码可能出了什么问题,有什么想法吗

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

const int maxsize = 10;

ifstream fin;
ofstream fout;

void transpose (double omatrix[][maxsize],double tmatrix [][maxsize], int array_size)
{
    for(int i = 0; i < array_size; i++)
    {
        for(int j = 0; j < array_size; j++)
        {
            tmatrix[j][i] = omatrix[i][j];
        }


    }
}

void sub (double omatrix[][maxsize], double smatrix[][maxsize], int array_size, int i, int j)
{
    int counter1 = 0, counter2 = 0;

    for (int a = 0; a < array_size; a++)
    {
        if (a != i)
        {
            for (int b = 0; b < array_size; b++)
            {
                if (b != j)
                {
                    smatrix[counter1][counter2] = omatrix[a][b];
                    counter2++;
                }
            }
            counter1++;
        }
    }
}

double determininant(double original_matrix[][maxsize], int array_size)
{
    double D = 0.0, temp[maxsize][maxsize];

    if(array_size == 1)
    {
        return original_matrix[0][0];
    }

    else if(array_size == 2)
    {
        return (original_matrix[0][0] * original_matrix[1][1]) - (original_matrix[0][1] * original_matrix[1][0]);
    }
    for(int i = 0; i < array_size; i++)
    {
        sub (original_matrix,temp,array_size, 0, i);
        D += pow(-1.0,i) * original_matrix[0][i] * determininant(temp, array_size - 1);
    }
    return D;
}

void inverse(double omatrix[][maxsize], int array_size, double imatrix[][maxsize])
{
    double D = determininant(omatrix, array_size);
    double c[maxsize][maxsize];

    if (D != 0)
    {
        for(int i = 0; i < array_size; i++)
            {
                for(int j = 0; j < array_size; j++)
                {
                    sub (omatrix, c, array_size, i, j);
                    imatrix[j][i] = pow(-1.0, i+j) * determininant( c, array_size - 1) / D;
                }
            }
    }
}

void mult(double omatrix[][maxsize], double imatrix[][maxsize], double pmatrix[][maxsize], int array_size)
{
    for(int i = 0; i < array_size; i++)
            {
                for(int j = 0; j < array_size; j++)
                {
                    pmatrix[i][j] = 0;

                    for(int k = 0; k < array_size; k++)
                    {
                        pmatrix[i][j] += omatrix[i][k] *imatrix[k][j];
                    }
                }
            }
}

void print (const double m[][maxsize], int array_size)
{
    for(int i = 0; i < array_size; i++)
            {
                for(int j = 0; j < array_size; j++)
                {
                    fout << m[i][j] << "  ";
                }
                fout << "\n";
            }
            fout << "\n";
}


int main()
{
    double original_matrix[maxsize][maxsize],
           inverse_matrix [maxsize][maxsize],
           transposed_matrix[maxsize][maxsize],
           product_matrix [maxsize][maxsize],
           D;


    int array_size;

    fout.setf(ios::fixed);
    fout.precision(2);

    char File_Name[10];
    cout << "Please enter a name for the output file";
    cin >> File_Name;

    fin.open ("input.txt");
    fout.open (File_Name);

    if (fin.fail())
    {
        cout << "Input file opening failed. \n";
        return 0;
    }

    while(fin >> array_size)
    {
        if(array_size != 0)
        {
            for(int i = 0; i < array_size; i++)
            {
                for(int j = 0; j < array_size; j++)
                {
                    fin >> original_matrix[i][j];
                }
            }

            fout << "THE ORIGIONAL ARRAY! \n";
            print (original_matrix, array_size);
            transpose (original_matrix, transposed_matrix, array_size);
            fout << "THE TRANSPOSED VIRSION! \n";
            print (transposed_matrix, array_size);
            fout << determininant(original_matrix, array_size) << "\n\n";
            inverse(original_matrix, array_size, inverse_matrix);
            print (inverse_matrix, array_size);
            mult(original_matrix, inverse_matrix, product_matrix, array_size);
            print (product_matrix, array_size);


        }
    }

    fin.close();
    fout.close();

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
常量int maxsize=10;
流鳍;
流式流量计;
无效转置(双omatrix[][maxsize],双tmatrix[][maxsize],整数数组大小)
{
for(int i=0;i因为您忘记在
sub(…)
中重置
计数器2
。因此您复制到结果矩阵中的错误位置,并在计算中使用未初始化的内存


(这个故事的寓意是在你实际需要的尽可能小的范围内声明变量。)

是的,否则我为什么要用矩阵做所有这些……Lol你确定你的数组被正确读取了吗?肯定,因为其他一切都很好,第一个运行良好。
fin>>原始矩阵[i][j]
我没有看到任何错误处理…int vs float?(我还没有读过代码…)你是上帝!非常感谢!
THE ORIGIONAL ARRAY! 
1.00  0.00  
0.00  1.00  

THE TRANSPOSED VIRSION! 
1.00  0.00  
0.00  1.00  

1.00

1.00  -0.00  
-0.00  1.00  

1.00  0.00  
0.00  1.00  

THE ORIGIONAL ARRAY! 
8.00  9.00  1.00  
3.00  5.00  2.00  
-2.00  3.00  -1.00  

THE TRANSPOSED VIRSION! 
8.00  3.00  -2.00  
9.00  5.00  3.00  
1.00  2.00  -1.00  

nan                              <--------------------- is wrong should be -78.00
                                                        which makes the next 2 mess up.
0.00  -0.00  0.00  
-0.00  0.00  -0.00  
0.00  -0.00  0.00  

0.00  0.00  0.00  
0.00  0.00  0.00  
0.00  0.00  0.00