Search code examples
c++visual-studiodev-c++

Dev C++ gives one output, but Visual studio code gives another output for same code


Dev C++ gives one output, but Visual studio code gives another output for same code Why is this happening? Any ideas, maybe it is caused by diffrent compiling options, or some mistake in code up here. Ask if you need more info, I have no idea why this is happening. If this happends to such a small code what will happend to a huge one.

#include <iostream>
using namespace std;
int main() {
int line[10];
int i, j;
bool growing = false;
cout << "Input 10 numbers:\n";
for (i = 0; i < 10; i++ ) {
    cin >> line[i];
}
if (i >= 10) {
    for (j = 0; j < 10;) {

        if (line[j] < line[j + 1]) {
            growing= true;
            j++;
        }
        else {
            growing= false;
            j = 12;
        }

    }
}

if (j >= 10 && growing== false) {
    cout << "Not growing";
}
else if (j >= 10 && growing== true
) {
    cout << "Growing";
}

}

Solution

  • Try this solution. start the variable j at 1 and compare line[j - 1] with line[j]. j will be max 9 in this case.

    #include <iostream>
    using namespace std;
    int main()
    {
        int line[10];
        bool growing = false;
        cout << "Input 10 numbers:\n";
    
        for (int i = 0; i < 9; i++)
        {
            cin >> line[i];
        }
    
        for (int j = 1; j < 10; j++)
        {
    
            if (line[j - 1] < line[j])
            {
                growing = true;
                continue; // start next iteration and skips line 22 and 23
            }
            growing = false;
            break; // stops the loop and moves to line 25
        }
    
        if (growing)
        {
            cout << "Growing";
        }
        else
        {
            cout << "Not Growing";
        }
    }
    

    or make it even simpler:

    #include <iostream>
    int main()
    {
        const int size = 10;
        int line[size];
    
        for (int i = 0; i < size; i++)
        {
            std::cin >> line[i];
        }
    
        for (int i = 1; i < size; i++)
        {
            if (!(line[i - 1] < line[i]))
            {
                std::cout << "Not Growing\n";
                return 0;
            }
        }
    
        std::cout << "Growing";
    }