-
September 8th, 2011, 10:47 PM
#16
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%;
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;
}
}
-
September 9th, 2011, 01:50 AM
#17
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.
-
September 9th, 2011, 07:16 AM
#18
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.
-
September 9th, 2011, 09:18 AM
#19
Re: Little help
Originally Posted by GCDEF
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.)
-
September 9th, 2011, 09:21 AM
#20
Re: Little help
Originally Posted by Lindley
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.
-
September 9th, 2011, 11:36 AM
#21
Re: Little help
Originally Posted by GCDEF
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.
-
September 13th, 2011, 02:13 PM
#22
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.
-
September 13th, 2011, 02:30 PM
#23
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.
-
September 13th, 2011, 02:38 PM
#24
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.
-
September 13th, 2011, 02:47 PM
#25
Re: Little help
Originally Posted by WastedPotential
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|