-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
  • 18,008
  • 2
  • 24
  • 52
  • 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? – Pedro Isaaco 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)

Pedro Isaaco
  • 404
  • 3
  • 10