'strange error in main and incorrect reading of a vector [closed]

hi I was writing a code where near the vectors it returns true if they are placed in ascending order otherwise false, when I try to start the code it gives me this error that I cannot understand

main.cpp:9:5: error: conflicting types for 'main'
int main(int, const char *[]) {
    ^
./solution.cpp:30:5: note: previous definition is here
int main()
    ^
1 error generated.

the other error happens in the vector inAscOrder_3 in which it should return false but I don't understand why it doesn't return it.

the code.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool isAscOrder(vector<int> arr)
{
    int size_array = arr.size()-1;
    int min = 0;
    int max = 0;
    for (int j = 0; j < size_array; j++)
    {
        if (min == 0)
        {
            min = arr[j];
            max = arr[j];
        }

        if (arr[j] > min)
        {
            return true;
        }
        if(arr[j]<min)
        {
            return false;
        }
    }
    return false;
}

int main()
{
    vector<int> inAscOrder_1 = {1, 2, 4, 7, 19}; // returns true
    vector<int> inAscOrder_2 = { 1, 2, 3, 4, 5 }; // returns true
    vector<int> inAscOrder_3 = { 1, 6, 10, 18, 2, 4, 20 }; // returns false
    vector<int> inAscOrder_4 = { 9, 8, 7, 6, 5, 4, 3, 2, 1 }; // returns false because the numbers are in DESCENDING order


    //test 1

    if (isAscOrder(inAscOrder_1) == true)
    {
        cout << "test #1 passed! " << endl;;
    }
    if (isAscOrder(inAscOrder_2) == true)
    {
        cout << "test #2 passed! "<<endl;
    }
    if (isAscOrder(inAscOrder_3) == false)
    {
        cout << "test #3 passed! "<<endl;
    }
    if (isAscOrder(inAscOrder_4) == false)
    {
        cout << "test #4 passed! ";
    }


    return 0;
}
c++


Solution 1:[1]

In your average C++ program, the main function is the singular entry and exit point for the program. When your executable is called, the program starts at main, and once main returns, the program releases all of its allocated resources and is terminated.

Your first issue arises because you have multiple declarations of the main function; the first is located at main.cpp:9:5, and the second is located at solution.cpp:30:5

As for your second issue, your function does not do what you think it does; any correlation between your expected outputs and the real outputs are pure coincidence.

bool isAscOrder(vector<int> arr)
{
    // This gets the size of your vector, then subtracts 1.
    //  size_array is now the index of the last element in the vector,
    //  and can be accessed with arr[size_array] without throwing an exception.
    int size_array = arr.size()-1;

    int min = 0;
    int max = 0; //< you never actually read the value from this

    // This condition is true until (j == size_array), and once it is the for loop breaks the loop.  
    //  Keep in mind, size_array is currently set to the index of the last ELEMENT in your vector;
    //  That means you're always skipping the last element in the vector.
    // You can avoid this by checking either ( j < arr.size() ) or ( j <= size_array )
    //                ?
    for (int j = 0; j < size_array; j++)
    {
        // This is always triggered on the first loop because min is always set to 0 the first time.
        //  if every number in the vector is 0 (except for the last one),
        //  then this will trigger every single time.
        //  ????????
        if (min == 0)
        {
        //      ?         Sets min to the value at arr[j]
            min = arr[j];
            max = arr[j];
        //      ?         Sets max to the value at arr[j],
        //                 so max is now equal to min.
        //                For tests 1-3, that value is `1`;
        //                 for test 4, that value is `9`

        // Here we check if the value at arr[j] is greater than min, and if it is-
        //  -we immediately break from the for loop and return true.
        if (arr[j] > min)
        {
            return true;
        }
        // Here we check if the value at arr[j] is less than min, and if it is-
        //  -we immediately break from the for loop and return false.
        if(arr[j]<min)
        {
            return false;
        }

        //< we never reach this point a second time unless ( arr[0] == arr[1] )
    }
    //< This is only ever reached if every element of arr except for
    //  the last one is set to the same number.
    return false;
}

As an example of what this function is doing, I debugged it for you:

enter image description here

As you can see, the function returned true on the second loop; this is because arr[1](2) is greater than min(1).

  • inAscOrder_1
    • Returns true when j == 1, because inAscOrder_1[1] == 2, and 2 > 1
  • inAscOrder_2
    • Returns true when j == 1, because inAscOrder_2[1] == 2, and 2 > 1
  • inAscOrder_3
    • Returns true when j == 1, because inAscOrder_3[1] == 6, and 6 > 1
  • inAscOrder_4
    • Returns false when j == 1, because inAscOrder_4[1] == 8, and 8 < 9

This is a working example of what your function should be doing:

bool isAscOrder(vector<int> arr)
{
    if (arr.size() < 2) //< make sure arr[1] exists
        return false;

    // Start at 1    Don't skip the last element
    //          ?    ?????????????
    for (int i{ 1 }; i < arr.size(); ++i) {
        // Check if arr[i] is less than or equal to arr[i - 1], this is
        //  why we started at 1 instead of 0; to prevent accessing arr[-1]
        //      ?         ?????
        if (arr[i] <= arr[i - 1])
            return false;
        // This approach works because if the sequence is in ascending order,
        //  each number in arr is never less than the previous one. (except for arr[0], which doesn't have a previous number)
    }

    return true;
}

You can allow a vector with repeated numbers to return true with this variant:

bool isAscOrder(vector<int> arr, bool allowRepeats = false)
{
    if (arr.size() < 2) //< make sure arr[1] exists
        return false;

    for (int i{ 1 }; i < arr.size(); ++i) {
        if (allowRepeats
             ? ( arr[i] <  arr[i - 1] ) //< when allowing repeats, return false if the current element is less than the previous one.
             : ( arr[i] <= arr[i - 1] ) //< when disallowing repeats, return false if the current element is less than or equal to the previous one.
           )
        {
            return false;
        }
    }

    return true;
}

When you're writing C++ code, having access to a debugger is incredibly important; if you're on Windows you can download and use Visual Studio Community for free.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1