使用矢量和循环

时间:2017-01-27 04:09:22

标签: c++ vector

抱歉在这里发布学生问题。我不是在寻找快速解决方案,我希望了解。我的代码中的注释将解释相同,但这里是纯文本版本:

这是一个" LoShu Magic Square"的开始,我不是要添加矩阵的所有部分我做的,我一直在尝试验证之前没有将相同的数字放入行中。我的想法是使用一个向量来测试"到目前为止输入的数字,所以它不需要是多维的(没有一个是,但我不关心测试人员的限制)。

按原样,代码将第一个数字带入测试向量,转到检查功能,意识到该数字是存在的(它应该,避免了在哪里添加初始值),以及在初始检查之后,它将采取1-9之间的任何其他值,包括重复,这是不好的。请帮忙?为什么它会在第一轮之后停止识别测试向量中的值?

单独链接到代码,如果它可能更容易在那里阅读:http://ideone.com/Dzh4mJ

#include<iostream>                                  // This is the beginning of a "LoShu magic square" program for class, currently my
#include <vector>                                   // goal is simply getting vectors to check whether or not a number has already been
using namespace std;                                // entered, and if so to go back and ask for another one. As-is it does not work 
                                                // through the first iteration. It recognizes the first number, says it's already in
bool theCheckening(vector<int>, int );              // and proceeds to take ANY numbers afterwards, repeats and all.

int main () {

int tester;

vector<int> loShu1;         // Rows 1-3 of a "square"
vector<int> loShu2;
vector<int> loShu3;
vector<int> testCaseOut(1,0);   // Test vector to iterate inside check function


do {

        do{
            cout << "Enter 1-9: ";              // Working as intended, makes sure no number besides 1-9 is entered
            cin >> tester;
        } while (tester < 1 || tester > 9);

        // Put initial value into test Vector
        if (theCheckening(testCaseOut, tester)){    // If check function returns true, add value to row 1
            loShu1.push_back(tester);
            testCaseOut.push_back(tester);  
            cout << "It worked?!";
        }

    } while (loShu1.size() <= 2);                   // shooting for size of 3, working as intended


for (int var : loShu1)                          // Debug to see rows before maths and adding them (to come)
    cout << var << " ";

cout << "\n";

for (int var : loShu2)
    cout << var << " ";

cout << "\n";

for (int var : loShu3)
    cout << var << " ";

return 0;
}

bool theCheckening(vector<int> testCaseInc, int testInt) {

    int count;
    vector<int> testCase(testCaseInc);          // Initialize vector inside check function to current test numbers
    for (int var : testCase)
        cout << var << " ";
    for (count = 0;count<=testCase.size();count++) {        // for all the numbers inside the testing vector
        if (testCase[count]!=testInt){                  // if current position of test vector is ! in vector already,
            cout << "ADDED!";                           // add it to row back in main()
            return true;
        for (int var : testCase)
            cout << var << " ";
            }
        cout << "ALREADY ENTERED!";             // Debug
        cout << testCase.size();
        return false;               // otherwise, ignore and ask for another number

        }
}

1 个答案:

答案 0 :(得分:0)

我认为你的TheCheckening功能存在逻辑错误。

据我所知,如果值不在向量中,则希望函数返回TRUE,如果在向量中,则返回FALSE。

现在问题:

想象一下有人为您的代码添加了以下值:

1 2 5 8

此值将添加到矢量中,矢量也将包含:

1 2 5 8

现在让我们再说一次2.你的函数现在从向量的值1开始并将它与2进行比较。这当然是假的。 看看你的代码:

if (testCase[count]!=testInt)
     return true;

您的代码说您现在可以返回true。这将导致您的函数结束并返回true给调用者。

您没有检查矢量的以下值。

你的功能theCheckening应该是这样的:

// user const vector<int> &, which will not cause to copy the
// vector
bool theCheckening(const vector<int> & testCase, int testInt) {

    // use size_t which represents a integer datatype which
    // is as big as arrays can be in the current bit setting
    // 32 bit => size_t = unsigned int
    // 64 bit => size_t = unsigned long long
    for(size_t count = 0; count <= testCase.size(); count++) {
        if(testCase[i] == testInt)
            return false;
    }
    return true;
}

我希望这有效,我正确地理解了这个问题。