Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++_Validation_Loops - Fatal编程技术网

如何检查数组中的所有值是否彼此相等。C++

如何检查数组中的所有值是否彼此相等。C++,c++,validation,loops,C++,Validation,Loops,我想问题是。。。这并不是说所有的总和都相等,即使它们是相等的。 我知道,这是一些糟糕的编程。xD我会在我的问题旁边放一些星星 谢谢你的帮助 #include <iostream> using namespace std; //function that passes a variable through and checks and sees if that variable has been used before. bool checkArray(int pass[

我想问题是。。。这并不是说所有的总和都相等,即使它们是相等的。 我知道,这是一些糟糕的编程。xD我会在我的问题旁边放一些星星

谢谢你的帮助

#include <iostream>

using namespace std;


//function that passes a variable through and checks and sees if that variable has been used before.
    bool checkArray(int pass[5][5], int toCheck)
    {
        bool check = false;
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 5; j++)
            {
                if(pass[i][j] == toCheck)
                {
                    check = true;
                }
            }
        }
    return check;
    }



int main(){
bool allTrue;
int array[5][5] = {0};
int num;
int count=0;
int arraySums[12];

    for(int i = 0; i < 5; i++)
        {
        for(int j = 0; j < 5; j++)
            {
            do //    do while executes when number is 1-25, and a number has not been found when run through the function to check if it is found in the array.
                {
                cout << "Please enter a number that will go in slot (" << i + 1 << ")(" << j + 1 << "): ";
                cin >> num;
                }
            while(((num < 1) || (num > 25)) || (checkArray(array, num) ==  true));

            array[i][j] = num;
        }
    }

//populate the arraySums Array.
//for(int i=0; i<2)




    for(int i = 0; i < 5; i++) // prints out array
    {
        for(int j = 0; j < 5; j++)
        {
            cout << " | " << array[i][j] << "  ";
        }
    cout << endl;
    count++;
    }

//for horizontal sums
arraySums[0]=array[0][0] + array[0][1] + array[0][2] + array[0][3] + array[0][4];
arraySums[1]=array[1][0] + array[1][1] + array[1][2] + array[1][3] + array[1][4];
arraySums[2]=array[2][0] + array[2][1] + array[2][2] + array[2][3] + array[2][4];
arraySums[3]=array[3][0] + array[3][1] + array[3][2] + array[3][3] + array[3][4];
arraySums[4]=array[4][0] + array[4][1] + array[4][2] + array[4][3] + array[4][4];

//for vertical sums
arraySums[5]=array[0][0] + array[1][0] + array[2][0] + array[3][0] + array[4][0];
arraySums[6]=array[0][1] + array[1][1] + array[2][1] + array[3][1] + array[4][1];
arraySums[7]=array[0][2] + array[1][2] + array[2][2] + array[3][2] + array[4][2];
arraySums[8]=array[0][3] + array[1][3] + array[2][3] + array[3][3] + array[4][3];
arraySums[9]=array[0][4] + array[1][4] + array[2][4] + array[3][4] + array[4][4];

//for diagonal sums
arraySums[10]=array[0][0] + array[1][1] + array[2][2] + array[3][3] + array[4][4];
arraySums[11]=array[0][4] + array[1][3] + array[2][2] + array[3][1] + array[4][0];

//to display horizontal sums
int count2=0;
for(int i = 0; i<5; i++)
{
    cout << "Horizontal sum for row: " << count2+1 << " is " << arraySums[i] << endl;
    count2++;
}

//to display the vertical sums.
count2=0;
for(int i = 5; i<10; i++)
{
    cout << "Vertical sum for row: " << count2+1 << " is " << arraySums[i] << endl;
    count2++;
}


//to display both diagonal sums
cout << "The diagonal sum from left to right is: " << arraySums[10] << endl;
cout << "The diagonal sum from right to left is: " << arraySums[11] << endl;

   //************************************************************************************************************* 
for(int i=0; i<13; i++)
{
    if(!(arraySums[i]==arraySums[i+1]))
    {
        allTrue=false;
        break;
    }

}


if(allTrue==true)
{
    cout<< "All the values are equal to each other." << endl;
}

}

在循环的最后一次迭代中,您正在比较arraySums[12]==arraySums[13]。此数组仅包含12个值,编号范围为0-11

您的for循环应为

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

标准算法行吗

如果是这样,您可以尝试计数或搜索

计数版本: bool-bAllTrue=countarray[0][0],array[5][0],array[0][0],25

搜索版本: bAllTrue=search_n&array[0][0],&array[5][0],array[0][0],25

此代码可能重复,或者为什么不工作。
bool allTrue = true;