Sorting program

Hey guys, just want to say I am a super noob when it comes to programming so take it easy on me. I have to create a program that will sort five scores that the user enters. I got the score part down. I just cant seem to pass it through my sort function. I know im making a stupid mistake Ive looked for hours for solutions and tried to play around with it with no luck. Here is the code :


#include <iostream>
#include <algorithm>

using namespace std;

const int Scores = 5;
void function();


int main() {
cout << "enter 5 scores: " << endl;

function();

return 0;
}

void function () {
int a, i;
int temp = 0;
int array[5] = {0,0,0,0,0};

for (a = 0; a < 5; a++) {
cin >> array[a];
}
}

void sortscores(int array, int scores, int array[a], int a)
{
int intArray[Scores] = {array[a]};
sort(int array, int array + Scores);

cout << "Sorted Array looks like this." << endl;
for (size_t i = 0; i != Scores; ++i)
cout << intArray[i] << " ";

return 0;
}


Im sure there is a more elegant way to approach this but I just need to get it working. Also the codes are:

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)

Any suggestions would be awesome. Im probably over looking this
Thanks
Andrew
Your functions should look like this:
1
2
3
4
5
// stores count integers in array numbers
void get_input(int numbers[], int count) ;

// sort array numbers with count elements
void sortscores(int numbers[], int count);
Hi drewdizzle92,

From also my limited knowledge, I see couple of things.

1- You never call sortscores in function or main.
2- If you want function() to change your array, you should either declare array as a static variable, or you have to call it in function as an argument.
3- I also see, there are many parameters like i and temp, not needed, so you can just delete those.
4- Lastly, you dont have to call array twice, scores (since you define them outside of main and other functions) or a(since you just dont need it) in sortscores.

I have cleaned the code up, I can send it to you if you want. But since you are trying to learn (like me), I assume you would want to try it by yourself first.
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.
Topic archived. No new replies allowed.