Which error will occur in this code?
#include <vector>
#include <algorithm>

#include <cmath>

int main(int argc, char *argv[])
{
    const int Xmin = -6;
    const int Xmax = 6;

    std::vector <double> xArray;
    std::vector <double> yArray;

    for (int i=Xmin; i < Xmax;  i += 0.001)
    {
        xArray.push_back(i);
        yArray.push_back(sin(i));
        i += 0.2;
    }

    return 0;
}
Explanation
In the loop
for (int i=Xmin; i < Xmax;  i += 0.001)
the i variable is of type int, so adding to the variable (i += 0.001) will be discarded.
The loop is not just infinite (there will never be i == Xmax).
Arrays will be filling up until the computer’s memory runs out.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по C++
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты