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?

Recommended Answers

All 4 Replies

Your sort function doesn't look correct.
Try bubble sort -

for(i=0;i<=list_size-1;i++)
 {
    for(j=0;j<=list_size-2;j++)
    {
        if(list[j+1]<list[j])
        {  swap  }
     }
 }

I tried that and it still gives me the same error at line 51 with
sort(A[], MAX_SIZE) it says parse error before ] token.

this is what i had for the bubble sort function:

void sort(int list[], const int list_size)
{
  int i, j, tmp;
  for(i=0;i<=list_size-1;i++)
    {
      for(j=0;j<=list_size-2;j++)
    {
      if(list[j+1]<list[j])
        {
          tmp=list[j];
          list[j]=list[j+1];
          list[j+1]=tmp;
        }
    }
    }

}

Try calling sort like this -

sort(A, MAX_SIZE);

Your original sort is correct, if a bit odd.

As pointed out, you were calling it incorrectly, putting the [] after the array argument. Just use the array name as an argument.

Please use code tags to enclose your code. That makes it easier to read, as your indenting will be preserved. Like this

[code]

your code goes here

[/code]

On to some corrections. In your selection sort, your loop tests could be made simpler. When dealing with arrays, it's more common to go for( i = 0; i < size_of_array; i++ ) rather than the way you test <= array_size-1. When you start including offsets like that, you add confusion to the code. So, how about this:

void sort(int list[], const int list_size)
{
	int i, j, k, tmp;

	for( i=0; i< list_size-1; i++)
	{
		k=i;
		for(j=i+1; j< list_size; j++)
		{
			if(list[j]<list[k])
				k=j;
		}
		tmp=list[k];
		list[k]=list[i];
		list[i]=tmp;
		cout<<list[i]<<" ";
	}
}

In your bubble sort, the loops should be:

for( i = 0; i < list_size-1; i++ )
{
   for( j = 0; j < list_size-1 -i; j++ )

Limit the j loop to only sorting the portion that's still unsorted. The tail end of the array is in order, so there's no need to revisit it again and again and again..... This makes what's usually described as the slowest legitimate sort slower than it need be.

Last point, and this is BIG. When calling your sort (either version), don't pass MAX_SIZE as the limit value for the sort, use your value space_size, so that the sorting is limited to the actual data.

And don't be afraid to put some horizontal whitespace between operators and operands, as I've done in the second code sample above. Helps to make it more readable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.