-4
int r, i, arrayMinimumIndex(auto a)
{
    for (int c : a)
        c > a[r] ?: r = i, ++i;
    return r;
}

I'm trying to run this code but it shows:

[Error] a function-definition is not allowed here before '{' token
[Error] 'arrayMinimumIndex' was not declared in this scope

Can anyone explain why does it fail and fix it ? Thanks in advance

Yksisarvinen
  • 15,158
  • 2
  • 21
  • 46
  • 2
    What do you expect syntax `int r, i, arrayMinimumIndex` to do? – Yksisarvinen Nov 07 '19 at 09:51
  • 4
    Notice that [`std::min_element`](https://en.cppreference.com/w/cpp/algorithm/min_element) exists. – Jarod42 Nov 07 '19 at 09:52
  • @Yksisarvinen It find the minimum value of array a, and return it index in a – Ducanh Tran Nov 07 '19 at 09:56
  • Yes, but I want to understand how this function does, anyway thanks for another way to solve it @Jarod42 – Ducanh Tran Nov 07 '19 at 09:59
  • @Jarod42s comment can be used together with std::distance to get the index ;) – Hafnernuss Nov 07 '19 at 09:59
  • 1
    You are getting these errors because this is not valid cpp syntax. – Hafnernuss Nov 07 '19 at 10:01
  • 2
    The correct syntax for function declaration is the following: `return_type function_name(argument_list){}`. No commas there (except for argument list inside `()`). If you're trying to return more than one element, this is not possible - you have to combine them in a struct or use `std::pair`/`std::tuple`. You may want to get [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from – Yksisarvinen Nov 07 '19 at 10:01
  • @Yksisarvinen I return r as int type, so this function will return one value. I don't understand why they put i as int variable and add the comma ? – Ducanh Tran Nov 07 '19 at 10:07
  • 1
    You don't add any variable name after the return type. There is only function name. Correct way to declare functions is shown in the answer, but if your book/tutorial/course doesn't explain that, you should really consider getting a better one. – Yksisarvinen Nov 07 '19 at 10:10
  • @Ducanh Tran: could you give us a link to this example? – Piters Nov 07 '19 at 10:15
  • https://app.codesignal.com/challenge/jCJgmTuTmLPGSKXei?solutionId=c5N5Q8dHiZmtNGgrs @Piters – Ducanh Tran Nov 07 '19 at 10:16
  • TBH, it looks like a non-standard extension of some compiler, but I cannot find any compiler which would support that ([MSVC, gcc and clang all reject this code](https://godbolt.org/z/IU_P2G)). The intention seems to be "declare global variable `i`, global variable `r` and function `arrayMinimumIndex`" – Yksisarvinen Nov 07 '19 at 10:21
  • And unfortunately, the link you provided is not accessible to people who are not logged in on that site. – Yksisarvinen Nov 07 '19 at 10:24
  • Yea, I am considering if they are global value or not? @Yksisarvinen – Ducanh Tran Nov 07 '19 at 10:25
  • In sumarize, I just want to understand why they put two variable in front of the name of function like that. And I don't need the way to solve this problem because it seems very easy to find a max or min of array right – Ducanh Tran Nov 07 '19 at 10:28
  • 1
    Hello tran, please can you explain me what is your objective of this problem? and what you are trying to do? – VJAYSLN Nov 07 '19 at 10:33
  • You should learn the syntax of function declaration – Nikhil Badyal Nov 07 '19 at 10:35

1 Answers1

1

A correct function definition would look like the following:

int arrayMinimumIndex(auto a) //format: return type, methode name, parameters
{
    int r = 0, i = 0; //variable definitions in the method body
    // search the index..
    return r;
}

Alternatively

int r, i, arrayMinimumIndex(auto a);

will work as well. r and i are global in this case. And still you will have to implement the method arrayMinimumIndex later (see above).

Apart from that the call (int c: a) will fail if you are not using C++11 (or higher) because simple arrays do not have iterators implemented. So you should consider passing e.g. an std::vector or walk the array manually like for (int i = 0; i < ...; ++i)

Piters
  • 404
  • 2
  • 9
  • The code you have fixed is correct, but it's not what I want, I am not understanding why I code a funtion like: int i, r arrayMinimumIndex(auto a) but in other compiler, it still works – Ducanh Tran Nov 07 '19 at 10:15
  • @Piters c++11 allows for-each loop on arrays – Venkatesh Nandigama Nov 07 '19 at 10:16
  • Nitpick - range-based loop `(int c: a)` will work on C-style arrays, but not on pointers (and I think C-style array will decay to pointer even if passed by `auto`). – Yksisarvinen Nov 07 '19 at 10:18
  • Yes I am using c++11 – Ducanh Tran Nov 07 '19 at 10:19
  • 1
    @Ducanh Tran: which compiler would that be? – Piters Nov 07 '19 at 10:27
  • You can run in any compiler which support for c++ – Ducanh Tran Nov 07 '19 at 10:31
  • How this can work? `int r, i, arrayMinimumIndex(auto a);`. This is not how functions are declared then how it goona work? Can you elaborate? – Nikhil Badyal Nov 07 '19 at 10:33
  • You know, I am having problem with it, but you can ask some master or someone who know about it, it will run! @problematicDude – Ducanh Tran Nov 07 '19 at 10:35
  • 1
    @DucanhTran As I linked in the comments to the question [none of 3 major C++ compilers accept your code](https://godbolt.org/z/IU_P2G). Splitting declaration and definition as suggested in the answer may compile. – Yksisarvinen Nov 07 '19 at 10:35
  • @DucanhTran I'm not asking you , I'm asking the guy who answered it. As he said it can work whereas this is totally wrong syntax. – Nikhil Badyal Nov 07 '19 at 10:36
  • So can you help me by asking someone who know about this problem. This is the test I have done on internet, and after that, I saw one solve from the top of leaderboard and he solved like that @problematicDude – Ducanh Tran Nov 07 '19 at 10:39
  • Firstly tell us what you are trying to do. Only then we can help.? – Nikhil Badyal Nov 07 '19 at 10:41
  • @Yksisarvinen I don't think such of return exist in C++. Return can't be splitted. – Nikhil Badyal Nov 07 '19 at 10:42
  • Im trying to run it. Can you fix it correct but still keeping i and r as two variables in front of the name of this function when declare it@problematicDude – Ducanh Tran Nov 07 '19 at 10:43
  • 1
    @problematicDude: It is a simple declaration, like `int arrayMinimumIndex(auto a);` and `int r, i;` The difference is only the declaration in a single line. – Piters Nov 07 '19 at 10:49
  • @Piters You tried to check and run it ? Check out if it work – Nikhil Badyal Nov 07 '19 at 10:55
  • 1
    @problematic Dude: You can even put two method declarations in one line `int r(int), w(int);` ;) – Piters Nov 07 '19 at 10:58