CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Little help

  1. #16
    Join Date
    Sep 2011
    Posts
    10

    Re: Little help

    Code:
    int arrayPtr;
    const int SIZE = 20;
    
    
    int randArray(arrayPtr);
    int dispArray(arrayPtr);
    int sortArray(arrayPtr);
    
    int main() {
    
    
    
    
            randArray(arrayPtr);
            dispArray(arrayPtr);
            sortArray(arrayPtr);
            dispArray(arrayPtr);
            delete [] arrayPtr;
            return 0;
    
    }
    
    
    
    int randArray(int array[]){
            int num;
            if(num =< 0)
                    return NULL;
            for (int count = 0; count < 20; count++)
                    array[count] = rand()100&#37;;
            return array;
    
    }
    int dispArray(int array[]){
    
            int count;
            //Display the array
            for (count = 0; count < SIZE; count++)
            cout << array[count] << endl;
    }
    int sortArray(int array[]){
            int startScan, minIndex, minValue;
                    for (startScan = 0; startScan < (SIZE - 1); startScan++){
                    minIndex = startScan;
                    minValue = array[startScan];
                    for(int index = startScan + 1; index < SIZE; index++){
                            //If the value is the smallest availible, move it to the
     front
                            if (array[index] < minValue){
                                    minValue = array[index];
                                    minIndex = index;
                            }
                    }
                    //Reset to starting from the new smallest value
                    array[minIndex] = array[startScan];
                    array[startScan] = minValue;
            }
    
    }

  2. #17
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: Little help

    Your function declarations are not compatible with their definitions. Your declarations should be:
    Code:
    int randArray(int array[], int size);
    int dispArray(int array[], int size);
    int sortArray(int array[], int size);
    And the respective definitions (below the main routine) should match these.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Little help

    You should read about how to declare functions and their arguments. You still don't get it.

    In a forward declaration you declare the argument type and optionally give it a name.

    In the function definition itself you have to declare the argument type and a name is required. The number of arguments in both and their types have to be the same. In your forward declarations you're specifying the name not the type. You're also specifying the name of a global which is going to lead to confusion and errors.

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

    Re: Little help

    Quote Originally Posted by GCDEF View Post
    In the function definition itself you have to declare the argument type and a name is required.
    Actually, a name *isn't* required in the definition. However, any parameter you don't give a name to obviously can't be used in the function. (This can be useful for suppressing "unused parameter" warnings in some cases.)

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Little help

    Quote Originally Posted by Lindley View Post
    Actually, a name *isn't* required in the definition. However, any parameter you don't give a name to obviously can't be used in the function. (This can be useful for suppressing "unused parameter" warnings in some cases.)
    I know, but defining parameters you don't use doesn't make much sense and in this and almost every other case, since he's planing on using them, he'll need to name them.

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

    Re: Little help

    Quote Originally Posted by GCDEF View Post
    I know, but defining parameters you don't use doesn't make much sense and in this and almost every other case, since he's planing on using them, he'll need to name them.
    It can make sense in the context of polymorphism or future-proofing. For simple stuff, it certainly doesn't.

  7. #22
    Join Date
    Sep 2011
    Posts
    10

    Re: Little help

    Just wanted to stop by again and say thanks for all the help you guys gave me. I fixed it up as well as I could.

    If anyone has the time, could the just edit all the code so everything works correctly, or at least so the arguments interact correctly? I'd really like to see how far off I was and how to do it right with my next project.

    Thanks again. You are all awesome.

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

    Re: Little help

    Here's just the dispArray() portion, with a hard-coded array for illustration (you would fill it using randArray() in practice).

    Code:
    void dispArray(int myarray[], int size);
    
    int main()
    {
    	const int SIZE = 20;
            // don't use "array" as a variable name
            int myintarray[SIZE] = {4, 1, 5, 23, 64, 21, 7, 0}; // just 0-fill the rest, values aren't important
    	
    	dispArray(myintarray, SIZE);
    
    	return 0;
    }
    
    void dispArray(int myarray[], int size)
    {
            int count;
            //Display the array
            for (count = 0; count < size; count++)
                cout << myarray[count] << endl;
    }
    Note, in a function argument, the int[] type is identical to the int* type. The only difference is that the [] notation is a clue to the programmer that this parameter is a pointer to an array rather than a single object.

    In a variable declaration, of course, an array declared with int arr[c] where c is a compile-time constant is a very different entity than int *arr.

  9. #24
    Join Date
    Sep 2011
    Posts
    10

    Re: Little help

    I see. I think I see what my problems were then. Is it just considered bad form to use array as the variable name, or will that cause errors? Thanks so much, by the way.

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

    Re: Little help

    Quote Originally Posted by WastedPotential View Post
    Is it just considered bad form to use array as the variable name, or will that cause errors?
    It could cause errors if you have a compiler new enough to support the <array> header, and you happen to be including that header, and you happen to have a "using namespace std" statement; because then your variable name might be confused with the std::array template type.

    In all likelihood that was not your problem, so in this situation it's merely good form. The reason it's good form is because of the above, however.

Page 2 of 2 FirstFirst 12

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