selection Sort Recursion

G

gambler

Guest
hey people's
So i've written this program a recursive form of selection sort- I'm pretty new to C++ and well I've been getting these errors and can't seem to fix 'em!-Any help?

Thanks!!

#include<iostream>
#include<cstdlib>
#include<iomanip>

void PrintArray(int x[]);
void SelectSort(int x[], int n);

using std::cout;

int main()
{
const int n = 20;
int x[] = {n};

//Loops through all positions of the array and assigns each position of
//the array to a random number between 1 and 1000


for(int i = 0; i < 20; i++)
{
int x = (1+ rand() % 1000); //Line 25
}

PrintArray(x);

cout << "\nArray after it has gone through Selection Sort." ;

SelectSort(x,n);

PrintArray(x);


//Prints all the elements within the indices of the Array

void PrintArray (int x[])
{
for (int t = 0; t < 20 - 1; t++) //Line 43
{
cout << x[t] << " ";
}
}

//Finds the position of the largest element of the array.

void LargestElement(int x[], int n)
{
int Temp; //temporary storage for exchange of values
int J; //Is loop control variable and Array Subscript
int MaxIndex; //holds index of largest value so far

MaxIndex = n; //assume x[n] contains initial largest value

for(J = n - 1; J >= 0; J--)
{
if(x[J] > x[MaxIndex])
{
MaxIndex = J; //assumes x[J] contains larges value
}

if(MaxIndex != n) //Makes sure MaxIndex is subscript
{
x[n] = x[MaxIndex];
x[MaxIndex] = Temp;
}
}
}

void SelectSort(int x[], int n)
{
if(n > 0)
{
LargestElement(x, n); //Place Largest value in x[n]
SelectSort(x, n - 1); //Sort Array 0..n-1
}
}
} //Line 89

So there are the error's
it's line 43 that I can't seem to fix!
In function `int main()':
25: error: variable-sized object `x' may not be initialized
42: error: parse error before `{' token
43: error: `t' undeclared (first use this function)
43: error: (Each undeclared identifier is reported only once for each
ion it appears in.)
43: error: parse error before `)' token
At global scope:
89: error: parse error before `}' token
 
first off, i have little experience with c++, i use java, but ill give it a whack

try intializing your array x properly (its and improper declaration of the array)

for line 25, try using math.rand() (include the library)

im not too sure about the rest, it looks quite messy. (then again im a java coder, what can i say :p)
 
Code:
 const int n = 20;
int x[] = {n};

That's wrong.

If you're trying to create an Array of 20 Integers, you just have to type:

Code:
int x[20];
 
I think your line 42-3 problems are because you didn't close your main function, ie. put a '}' after the printArray call. It might give you a warning wanting you to put a "return 0;" in there too.

edit: This is a lot easier to catch if you use proper indentation.

edit^2: Might fix that last error too.
 
One more question does anyone know if I wrote the SelectSort function (last section of page) correctly because it doesn't produce the right results. -Any suggestions? Thank you!!
 
Is there a repeated pattern in the unwanted results? Or is it just giving you nothing and stopping the program?
 
Code:
void PrintArray (int x[])
{
    for (int t = 0; t < 20 - 1; t++) //Line 43
[b]
    {
    cout << x[t] << " "; //<--- You don't need the {} if there's just one  statement. It gives you an error, I believe, if you do.
    }
[/b]
}

http://cpp.sourceforge.net/?show=4192 <--- Hope this helps, fixed alot of the bracket over-use and other various stuff.
 
Ah, well they are bad practice. BTW, proper tabulation is wise
 
That depends... for example, if you have deeply nested if-else-clauses, sometimes brackets help to make it clear which 'else' belongs to which 'if', even though they are not necessary for the syntax. :)
 
That totally didn't fix the prob, but I appreciate the help! -the prog is not printing out the numbers in ascending order is all.
 
Man, brings me back to writing a Binary sort and search back in high school.
 
It's been a while, so chances are you've got this figured out by now, but whatever...

One problem is that the first call to LargestElement is LargestElement(x, 20), which starts by assuming x[20] is the largest element and comparing things to that. Unfortunately x[20] is one past the end of your array, in unallocated space.

And your printarray function doesn't look like it'll print the last element of the array. ("t<20-1" should be "<=" or no "-1")
 
Back
Top