I am trying to sort an array and eventually get the 'n' closest numbers to the position the user chooses but i can't get the sort function to work first. Here's the code:
#include <iostream> //for standard I/O
#include <iomanip> //for setw(), etc.
#include <cstdlib> //for rand(), srand()
#include <ctime> //for time()
#include <cmath> //for math functions
#include <string> //for strings
using namespace std;
//constants
const int MAX_SIZE = 1000; //maximum number of elements we can hold
//function prototypes (function declarations)
void query(const int key, const int list[], const int list_size, int result[], const int result_size);
void printList(const int list[], const int list_size);
void swap(int&a, int&b);
void sort(int list[], const int list_size);
//the main function
int main()
{
int space_size, k, key, i, A[MAX_SIZE], a, B[MAX_SIZE];
srand(time(NULL));
cout<<"Enter a search area (1-1000): ";
cin>>space_size;
cout<<""<<endl;
cout<<"Enter a number's position within the search area: ";
cin>>k;
cout<<""<<endl;
cout<<"Enter a key to search the search space: ";
cin>>key;
for(i=0; i<space_size; i++)
{
A[i]=rand()%5001;
cout<<A[i]<<" ";
}
sort(A[], MAX_SIZE); //Gives me an error here before ]
return 0;
}
//function definitions
/**
* Performs K-NN query on key.
*
* @param key The search key
* @param list[] The search list
* @param list_size Size of list
* @param result[] The results
* @param list_size Size of results
*
*/
void query(const int key, const int list[], const int list_size, int result[], const int result_size)
{
//implement K-NN query here
}
/**
* Prints array
*
* @param list[] The search list
* @param list_size Size of list
*
*/
void printList(const int list[], const int list_size)
{
}
/**
* Sorts an array
*
* @param list[] The search list
* @param list_size Size of list
*
*/
void sort(int list[], const int list_size)
{
int i, j, k, tmp;
for(i=0; i<=list_size-2; i++)
{
k=i;
for(j=i+1; j<=list_size-1; j++)
{
if(list[j]<list[k])
k=j;
}
tmp=list[k];
list[k]=list[i];
list[i]=tmp;
cout<<list[i]<<" ";
}
}
Can anyone please help me?