using decrement operator in recursive function argument list

Feb 19, 2015 at 5:52pm
Im using a recursive function to sort array. The decrement operator is used to eventually get to base condition in function. Used debugger the size-- expression is not decrementing. I figured out how to fix it but dont quite understand it.

[coed]#include "stdafx.h"
#include <iostream>


void selectionsort(int [], int);


int main()
{
using namespace std;
const int arrysize = 10;
int srtarry[arrysize] = {50,80,100,150,10,20,90,1,15,35};
int j;

selectionsort(srtarry, 10);



for (j = 0; j < arrysize; j++)
cout << srtarry[j]<<" ";

cin.clear();
cin.ignore(255, '/n');
cin.get();

return 0;
}

void selectionsort(int arry[], int size)
{
int count = 9;
int hold,i;

if (size == -1)
return;
else{
for (i = count; i > count - size; i--){
if (arry[i] < arry[i - 1]){
hold = arry[i - 1];
arry[i - 1] = arry[i];
arry[i] = hold;
}
}
selectionsort(arry, size--);
}
} [/coed]
Last edited on Feb 20, 2015 at 1:44am
Feb 19, 2015 at 6:02pm
You used post-decrement instead of pre-decrement. Though really, why not just write size - 1? The variable is never used again after that line.

Also, why did you use [c0ed] instead of [code] ?
Last edited on Feb 19, 2015 at 6:03pm
Feb 20, 2015 at 1:44am
why post decrement does'nt work.
Feb 20, 2015 at 1:56am
1. the variable is never used after that line
2. after that line, the variable is not used again
3. post decrement returns the old value from before the decrement
4. you pass that old value to the function
Topic archived. No new replies allowed.