CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Function

  1. #1
    Join Date
    Apr 2010
    Posts
    14

    Function

    Hi everyone

    Please i need help with this question. it says:

    Write down a function that reads in a set of integers into an array X and returns it to its caller . Assume that the maximum size of the array is 100.

    I am particularly confused on how to stop the loop if the elements are less than 100 and whether to read in the values from the main or from the function.!!!!!

    I need this because a similar question will be asked for my exam tomorrow. Any help is highly appreciated.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function

    Quote Originally Posted by shani09 View Post
    I am particularly confused on how to stop the loop if the elements are less than 100
    You have a few options there. If they don't tell you which one to use, just pick one and go with it.

    1) Ask for the number of items to be entered before you start reading them.
    2) Assign (and tell the user about) a special value which will signify the end of the input. For instance, if you were only interested in positive numbers, you might stop reading when you see a -1 (and don't count that as one of the values entered).
    3) Read only integers on a single line, stopping when the user hits return. This is sort of a variation on (2), except that you're looking for a character (newline) which isn't of the entered type (integer) as the delimiter.

    and whether to read in the values from the main or from the function.!!!!!
    Seems clear to me that the reading is expected to occur within the function. What else would the function do if you read the values in main? The assignment doesn't give it any other purpose.

  3. #3
    Join Date
    Apr 2010
    Posts
    14

    Re: Function

    okay thanks....

    But one more question: it says in the question that X returns the integers back to its caller.
    What is going to be the return statement assuming i use your 1st guideline.?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function

    That's a good question. There are several basic approaches to returning an array of values from a function:

    1) Dynamically allocate the array within the function, then return both the array pointer and the allocated size (and optionally also the used size, if this is different from the allocated size). This is common but really isn't a good idea in C++. In C it's all you've got sometimes, though.

    2) Make a pointer to the output array be passed to the function as a parameter. In the case where you have a maximum possible number of values this is a particularly attractive option, since you can pass in a fixed-size array declared on the stack in main() for the function to fill. Don't forget to return the number of items in the array after the function call.

    3) Return (either by value, or as an output reference parameter) a container class wrapping a fixed-size or dynamic array, such as std::vector. This is the preferred C++ approach when the size of the array is unknown.

    There is one additional option sometimes used in C++, although it's a somewhat advanced topic and certainly overkill in this case: use a template to pass in an output iterator to the function as an argument. You can think of this as a generalization of #2 to make the function much more flexible.

  5. #5
    Join Date
    Apr 2010
    Posts
    14

    Re: Function

    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured