-3

So this is the question:

Batman, Superman, and Captain America are searching for a group of thugs hiding in a building on New York's 5th Avenue. In order not to look for the same places, the heroes have agreed to search the buildings as follows:

Batman will start looking for the first building on the left and move one building to the right.

Captain America will start the search from the first building on the right and move one building to the left.

Superman, because he can fly, will apply the binary search algorithm to search the buildings. That is, he will first search the building in the middle of 5th Avenue and move left or right accordingly.

If you are given N integers sorted in ascending order indicating the building numbers and then an integer X denoting the building that the villains have fled, create a program that displays the name of the hero who will be able to first detect the villains, searching for the fewest buildings. If some heroes find the villains at the same time, all their names appear in alphabetical order.

Input Format:

An integer N, followed by N integers sorted in ascending order.

Constraints

0

Output Format:

Show the name of the hero who will be able to locate the villains first, looking for the fewest buildings. If some heroes find the villains at the same time, all their names appear in alphabetical order.

Sample Input 0

9
20 30 40 50 60 100 200 500 1000
500

Sample Output 0

Captain America

End of problem. This is my code:

#include <iostream>
#include <algorithm>

using namespace std;

int n;

int batman(int arr[], int x)
{
    for (int p = 0; p < n; p++)
    {
        if (arr[p] == x)
            return p;
    }
}

int cap(int arr[], int x)
{
    int ecount = 0;

    for (int p = n; p > 0; p--)
    {
        ecount++;
        if (arr[p] == x)
            return ecount;
    }
}

int superman(int arr[], int target)
{
    int first = 0, last = n - 1, mid, pos = -1, coun = 0;

    while (first <= last && pos == -1)
    {
        mid = (first + last) / 2;
        if (arr[mid] == target)
            pos = mid;
        else if (target < arr[mid])
            last = mid - 1;
        else
            first = mid + 1;
    }

    coun++;
    if (pos != -1)
        return coun;
}

int main()
{
    cin >> n;

    int arr[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }

    int x;
    cin >> x;

    if (batman(arr, x) < cap(arr, x) && batman(arr, x) < superman(arr, x))
    {
        cout << "Batman";
    }

    else if (cap(arr, x) < batman(arr, x) && cap(arr, x) < superman(arr, x))
        cout << "Captain America";

    else if (superman(arr, x) < batman(arr, x)
            && superman(arr, x) < cap(arr, x))
        cout << "Superman";

    else
        cout << "Batman" << endl << "Captain America" << endl << "Superman";

    return 0;

}

I don't know what I am doing wrong. For some reason it doesn't compile on hackerrank, but it compiles on codeblocks. I also think that some of the answers I get are wrong but I can't put it on hackerrank.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/208744/discussion-on-question-by-chris-k-c-code-compiles-on-codeblocks-but-not-on-h). – Samuel Liew Feb 29 '20 at 05:31

1 Answers1

1

The error is (tried to restore the formatting):

Solution.cpp: In function ‘int batman(int*, int)’: 
Solution.cpp:16:1: error: control reaches end of non-void function [-Werror=return-type] } ^ 
Solution.cpp: In function ‘int cap(int*, int)’: 
Solution.cpp:20:37: error: control reaches end of non-void function [-Werror=return-type] if(arr[p]==x)return ecount;}} 
Solution.cpp: In function ‘int superman(int*, int)’: 
Solution.cpp:39:1: error: control reaches end of non-void function [-Werror=return-type] } ^ 
cc1plus: some warnings being treated as errors

All of them are actually warnings that are treated as errors, because of a -Werror=return-type flag being passed to the compiler.

Warnings should be taken serious and in this case they really are errors. Not returning anything from a function that is declared to return something is undefined behavior.

For example here:

int batman(int arr[], int x)
{
    for (int p = 0; p < n; p++)
    {
        if (arr[p] == x)
            return p;
    }
}

If arr[p] == x is not true for any p then you fall of the end of the function without returning something. I cannot (and actually I dont want to) tell you what you should return if you do not find x in the array, but one way to silence the warning would be

int batman(int arr[], int x)
{
    for (int p = 0; p < n; p++)
    {
        if (arr[p] == x)
            return p;
    }
    return -1;
}

Once you fixed your functions to return on all paths your code should compile.

For sake of completeness, to find an element in an array there is std::find. It works with iterators and uses the common convention to return the end iterator when the element was not found:

#include <iterator>
#include <algorithm>
#include <iostream>
int main() {
    int arr[] = {1,2,3,4};
    auto it = std::find( std::begin(arr), std::end(arr), 3);

    if (it != std::end(arr))  std::cout << "index = " << it-std::begin(arr); 
    else std::cout << "not found";                              
}

Also note that this

cin >> n;
int arr[n];

is a variable length array which is not standard C++. You should use std::vector instead for dynamically sized arrays. It is a feature in C and some compilers offers it as an extension for C++.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 3
    Variable length arrays went optional in C11 so you can't count on support in C either. You can test for support with `__STDC_NO_VLA__`. – user4581301 Feb 28 '20 at 19:54
  • 1
    @user4581301 if I understood [this](https://stackoverflow.com/a/54163435/4117728) correctly then the whole point of vlas in c isnt the way we see them daily in c++ questions but rather things like `void init(unsigned n, unsigned m, int a[n][m])` (also something there is not really a need for in C++) – 463035818_is_not_an_ai Feb 28 '20 at 20:01
  • They can be damn useful, even in C++, when used carefully and correctly. I'd toss them in the same category as `goto` except I've seen far more good uses for VLA than `goto`. I've also seen far more uses of VLA, so that might not be a distinction that's fair to `goto`. – user4581301 Feb 28 '20 at 20:19