First, please use code tags when posting code. See
http://www.cplusplus.com/articles/jEywvCM9/
Then some basics on
* functions:
http://www.cplusplus.com/doc/tutorial/functions/
* arrays:
http://www.cplusplus.com/doc/tutorial/arrays/
That said, you did show verbatim compiler errors and did attempt to use Standard Library std::sort. Very good.
The three error messages do come from the same line, and the first actually generates the other two:
main.cpp:29:50: error: 'a' was not declared in this scope
void sortscores(int array, int scores, int array[a], int a)
^
main.cpp:29:52: error: expected ')' before ',' token
void sortscores(int array, int scores, int array[a], int a)
^
main.cpp:29:54: error: expected unqualified-id before 'int'
void sortscores(int array, int scores, int array[a], int a) |
Therefore, solving the first would be syntactically enough?
Actually no, there is more to it.
What is wrong in:
void sortscores( int array, int scores, int array[a], int a );
The first thing is the mystery 'a':
void sortscores( int array, int scores, int array[a], int a );
If we want to say that argument 'array' is an array of integers that has 'a' elements, then the 'a' must be a known, compile-time constant integer. A value that has been defined before the function declaration appears.
Lets do so:
1 2
|
constexpr size_t a {42};
void sortscores( int array, int scores, int array[a], int a );
|
Alas, we got a new problem: if 'a' means 42, how could it possibly be name of a function argument?
Lets rename one of them:
1 2
|
constexpr size_t NN {42};
void sortscores( int array, int scores, int array[NN], int a );
|
Oh no, yet another dilemma: first and third argument both claim name 'array'.
One more rename:
1 2
|
constexpr size_t NN {42};
void sortscores( int noIdeaWhat, int scores, int array[NN], int a );
|
Now that one line should compile.
There is code reusability issue. When we write
void foo( int bar[7] );
we have a function that accepts an array of exactly 7 elements. That is not very flexible. We certainly do not want to create a new function for every array size.
A more usable function merely takes address of the start of an array, and count of elements in the array:
void foo( int* bar, size_t count );
Now we can call the same function with different inputs.
sort( int array, int array + Scores );
As said, you probably try to call a function there. Typenames do not belong to a function call. This might have been closer:
std::sort( array, array + Scores );
The irony is that the std::sort is a function that sorts an array. There is no need to create a function that calls the std:.sort, when you can call it directly.
You do have a global constant:
const int Scores = 5;
but your code is full of literal value
5
. If you have a named value, then use that name in every place where the value logically is used.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <algorithm>
void function( int* bar, size_t count ); // obscure name?
int main() {
constexpr size_t Scores {5}; // size_t is an unsigned integer
int foo[Scores] {}; // whole array initialized with 0 values
std::cout << "enter " << Scores << " scores: \n";
function( foo, Scores );
std::sort( foo, foo + Scores );
for ( auto s : foo ) {
std::cout << ' ' << s;
}
std::cout << '\n';
return 0;
}
void function( int* bar, size_t count )
{
// fill the bar
}
|
That should get you started.
However, you are probably supposed to write your own sorting routine; not to use the std::sort. It is very good to learn to use Standard Library, but at this point your teacher expects you to learn to
think.