C++ C+中的正则矩阵+;

C++ C+中的正则矩阵+;,c++,matrix,C++,Matrix,我的家庭作业将创建一个程序,用给定的模式检查数组中的数字。程序必须将矩阵维度和矩阵中的项作为命令行中的参数。例如,程序名为myProg.exe,我们要检查2x3维矩阵,最大尺寸限制为20x20: 然后,我将运行您的程序作为 程序将检查一个特殊的矩阵模式,并根据我们从控制台输入的值打印出可接受或不匹配。特殊模式:在行主表示中,矩阵的单元格必须遵守此规则。矩阵的某些项必须是相邻单元的和或积。在行主表示法中,总和和乘积运算的位置如示例所示。求和和单元格和乘积单元格之间有一个自由单元格。对于奇数行,序列

我的家庭作业将创建一个程序,用给定的模式检查数组中的数字。程序必须将矩阵维度和矩阵中的项作为命令行中的参数。例如,程序名为
myProg.exe
,我们要检查2x3维矩阵,最大尺寸限制为20x20:

然后,我将运行您的程序作为

程序将检查一个特殊的矩阵模式,并根据我们从控制台输入的值打印出
可接受
不匹配
。特殊模式:在行主表示中,矩阵的单元格必须遵守此规则。矩阵的某些项必须是相邻单元的和或积。在行主表示法中,总和和乘积运算的位置如示例所示。求和和单元格和乘积单元格之间有一个自由单元格。对于奇数行,序列从自由单元格开始,对于偶数行,序列从和或积单元格开始

我的代码在这里:

  #include <iostream>
  #include <sstream>

 using namespace std;

static  int iter = 0;
static unsigned int sat=3, sut=2;
bool ok = false;
int *accepted;
int *array;


 string isAcceptable(int mat[]) {

    int l, co = 0;
    bool operation = false;
    int mat2[sat][sut];

    for (int i = 0; i < sat; i++) {
        for (int j = 0; j < sut; j++) {
            mat2[i][j] = mat[co];
            co++;
        }
    }

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

        if (i % 2 == 0)
            l = 1;
        else
            l = 0;

        for (int j = l; j < sut; j += 2) {

            int totalProduct;

            if (!operation) {
                totalProduct = 0;

                if (j > 0)
                    totalProduct += mat2[i][j - 1];
                if (j < sut - 1)
                    totalProduct += mat2[i][j + 1];
                if (i > 0)
                    totalProduct += mat2[i - 1][j];
                if (i < sat - 1)
                    totalProduct += mat2[i + 1][j];

            } else {
                totalProduct = 1;

                if (j > 0)
                    totalProduct *= mat2[i][j - 1];
                if (j < sut - 1)
                    totalProduct *= mat2[i][j + 1];
                if (i > 0)
                    totalProduct *= mat2[i - 1][j];
                if (i < sat - 1)
                    totalProduct *= mat2[i + 1][j];
            }

            if (mat2[i][j] != totalProduct)
                return "NOT MATCH";

            operation = !operation;

        }
    }
    return "ACCEPTABLE";
}

    void change(int index1, int index2) {

    int temp;

    temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

    iter++;

}

 void combine(int mat[], int len) {

    if(ok)
    return;

    array = new int[len];
    *array = *mat;


    if (len <= sat * sut) {

        for (int i = len; i < sat * sut - 1; i++) {

            for (int j = i; j < sat * sut; j++) {

                combine(array, len + 1);
                change(i, j);

                if (isAcceptable(array) == ("ACCEPTABLE")) {
                    int accepted[sat*sut];
                    *accepted = *array;

                    ok = true;
                    return;
                }



            }

        }
    } else
        return;
}

string isAcceptableCombine(int mat[]) {

    combine(mat, 6);

    if (ok)
    {
        cout<< " TRUE Sequense";
        return "ACCEPTABLE";
    }
    else
        cout<< " FALSE Sequense";
        return "NOT MATCH";
}


int main(int argc, char** argv) {



int matris[] = {1,2,1,4,1,6};

isAcceptableCombine(matris);

}
#包括
#包括
使用名称空间std;
静态int-iter=0;
静态无符号整数sat=3,sut=2;
bool ok=false;
int*接受;
int*数组;
字符串可接受(int mat[]){
int l,co=0;
布尔运算=假;
int mat2[sat][sut];
for(int i=0;i0)
总积+=mat2[i][j-1];
if(j0)
总积+=mat2[i-1][j];
如果(i0)
totalProduct*=mat2[i][j-1];
if(j0)
总积*=mat2[i-1][j];
如果(i如果(len在调试器中运行,并逐行遍历代码,它应该可以帮助您找出发生了什么。此外,您向我们展示的代码片段中有一些可疑的代码,如此赋值
*array=*mat
,它将
mat
的第一个值与
array
的第一个值进行匹配,并将其余条目保留在
array
uninitialized(由于未初始化的数据是不确定的),这会给您带来更糟糕的结果。哦,这比我第一次看到的还要糟糕,因为在第一次(非递归)调用
combine
参数
len
为零,这意味着您正在分配一个大小为零的数组,因此写入
array
中的任何索引也是未定义的行为,因为它超出了范围。
array
从何而来
更改
组合
?(顺便说一句,这是一个可怕的名字,因为
array
一起使用名称空间std;
会引起歧义,再次编辑,数组是全局数组。我将mat方法分配给combine中的数组。
  #include <iostream>
  #include <sstream>

 using namespace std;

static  int iter = 0;
static unsigned int sat=3, sut=2;
bool ok = false;
int *accepted;
int *array;


 string isAcceptable(int mat[]) {

    int l, co = 0;
    bool operation = false;
    int mat2[sat][sut];

    for (int i = 0; i < sat; i++) {
        for (int j = 0; j < sut; j++) {
            mat2[i][j] = mat[co];
            co++;
        }
    }

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

        if (i % 2 == 0)
            l = 1;
        else
            l = 0;

        for (int j = l; j < sut; j += 2) {

            int totalProduct;

            if (!operation) {
                totalProduct = 0;

                if (j > 0)
                    totalProduct += mat2[i][j - 1];
                if (j < sut - 1)
                    totalProduct += mat2[i][j + 1];
                if (i > 0)
                    totalProduct += mat2[i - 1][j];
                if (i < sat - 1)
                    totalProduct += mat2[i + 1][j];

            } else {
                totalProduct = 1;

                if (j > 0)
                    totalProduct *= mat2[i][j - 1];
                if (j < sut - 1)
                    totalProduct *= mat2[i][j + 1];
                if (i > 0)
                    totalProduct *= mat2[i - 1][j];
                if (i < sat - 1)
                    totalProduct *= mat2[i + 1][j];
            }

            if (mat2[i][j] != totalProduct)
                return "NOT MATCH";

            operation = !operation;

        }
    }
    return "ACCEPTABLE";
}

    void change(int index1, int index2) {

    int temp;

    temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

    iter++;

}

 void combine(int mat[], int len) {

    if(ok)
    return;

    array = new int[len];
    *array = *mat;


    if (len <= sat * sut) {

        for (int i = len; i < sat * sut - 1; i++) {

            for (int j = i; j < sat * sut; j++) {

                combine(array, len + 1);
                change(i, j);

                if (isAcceptable(array) == ("ACCEPTABLE")) {
                    int accepted[sat*sut];
                    *accepted = *array;

                    ok = true;
                    return;
                }



            }

        }
    } else
        return;
}

string isAcceptableCombine(int mat[]) {

    combine(mat, 6);

    if (ok)
    {
        cout<< " TRUE Sequense";
        return "ACCEPTABLE";
    }
    else
        cout<< " FALSE Sequense";
        return "NOT MATCH";
}


int main(int argc, char** argv) {



int matris[] = {1,2,1,4,1,6};

isAcceptableCombine(matris);

}