LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ functions (https://www.linuxquestions.org/questions/programming-9/c-functions-845878/)

gargolajr 11-22-2010 03:39 PM

C++ functions
 
How can i translate this function to C++ code? thanks

FUNCTION minimumIndex
PARAMETER 1: An integer array
PARAMETER 2: An integer specifying the starting index
PARAMETER 3: An integer specifying the ending index
RETURNS : An integer representing the index of the minimum
value in the array from the starting index to
the ending index
BODY of minimumIndex
Set the minimumIndex to be the starting index
Set the currentIndex to be one more than the starting index
Loop around until currentIndex is equal to ending index
If element at currentIndex is less than minimumIndex
Set minimumIndex to be the currentIndex
END If
END Loop
Return minimumIndex
END BODY of minimumIndex

eSelix 11-22-2010 04:01 PM

In which language this "function" was wrote?

David1357 11-22-2010 04:23 PM

Quote:

Originally Posted by gargolajr (Post 4167426)
How can i translate this function to C++ code?

What you have presented is actually a detailed description of the function signature of the C++ function and a detailed description of how it should work.

Due to the level of detail, it should be easy for you to write the corresponding C++ function.

This looks a lot like homework and you will learn a lot more by doing it yourself.

Tinkster 11-23-2010 10:19 AM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.

johnsfine 11-23-2010 11:19 AM

Quote:

Originally Posted by gargolajr (Post 4167426)
How can i translate this function to C++ code?

I expect that means you haven't learned C yet either.

If you know even a little C, it should be easy to code that function in C. If you know even a little C++, you should know that function has no significant reason to be different in C++ from the same function in C.

But before coding, make sure you resolve the ambiguity in the problem statement:

Quote:

PARAMETER 3: An integer specifying the ending index
In C and C++ programming it is common, but not universal, to interpret an "end" index as the index of the first item not included, meaning one beyond the last item included. But it is also possible that an "end" index represents the last item included.

A good spec should make clear for any "end" pointer or index whether it means last included vs. one past the last included.

Quote:

Loop around until currentIndex is equal to ending index
That sounds like "end" means first not included, but it still is not clearly unambiguous (English rarely is). Do you know whether "end" is the last included vs. one past the last included?

Other than the above comments, I'm not going to do your homework for you.

If there is a specific detail of C++ syntax that is giving you trouble, please feel free to post what you have done so far and the specific question or problem that you have. But please do not ask for someone to do the entire assignment for you.

BTW, regarding my statement that this function would the same in C vs. C++, I want to add one qualification. I don't use C enough to even try to remember which versions of C restrict the declaration of local variables to places other than where good C++ style would place them. In my opinion, good C++ style usually includes declaring most local variables at the natural point where they first receive a value. In older versions of C, you may be forced to declare local variable earlier. In C++, you can declare variables as if you were in an older version of C, but you shouldn't.


All times are GMT -5. The time now is 12:31 AM.