0

This is for a Deal or No Deal game.

So in my main function I'm calling my casesort method as such:

casesort(cases);

My method looks like this, I already realize it's not the most efficient sort but I'm going with what I know:

void casesort(float cases[10])
{
int i;
int j;
float tmp;
float zero = 0.00;

for (i = 0; i < 10; i++)
{
    for (j = 0; j < 10; j++)
    {
        if (cases[i] < cases[j])
        {
            tmp = cases[i];
            cases[i] = cases[j];
            cases[j] = tmp;
        }
    }
}

//Print out box money amounts
printf("\n\nHidden Amounts: ");
for (i = 0; i < 10; i++)
{
    if (cases[i] != zero)
        printf("[$%.2f] ", cases[i]);
}

}

So when I get back to my main it turns out the array is sorted. I thought void would prevent the method returning a sorted array. I need to print out actual case numbers, I do this by just skipping over any case that is populated with a 0.00. But after the first round of case picks I get "5, 6, 7, 8, 9, 10" printing out back in my MAIN. I need it to print the cases according to what has been picked. I feel like it's a simple fix, its just that my knowledge of the specifics of C is still growing. Any ideas?

5 Answers5

1

Return type void has nothing to do with prevention of array from being sorted. It just says that function does not return anything.

You see that the passed array itself is affected because an array decays to a pointer when passed to a function. Make a copy of the array and then pass it. That way you have the original list.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

In C, arrays are passed by reference. i.e. they're passed as pointer to the first element. So when you pass cases into your function, you're actually giving it the original array to modify. Try creating a copy and sorting the copy rather than the actual array. Creating a copy wouldn't be bad as you have only 10 floats.

Rahul Shardha
  • 399
  • 7
  • 16
0

Instead of rolling your own sort, consider using qsort() or std::sort() if you are actually using c++

There are 2 obvious solutions. 1) Make a copy of the array and sort the copy (easy, waste some memory, likely not a problem these days). 2) Create a parallel array of integers and perform an index sort, i.e., instead of sorting thing original, you sort the index and then dereference the array using the index when you want the sorted version, otherwise by the raw unsorted array.

Gary Walker
  • 8,831
  • 3
  • 19
  • 41
0

Well, make a local copy of you input and sort it. Something like this:

void casesort(float cases[10])
{
    float localCases[10];
    memcopy(localCases, cases, sizeof(cases));
    ...

Then use localCases to do your sorting.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
0

If you don't want the array contents to be affected, then you'll have to create a copy of the array and pass that to your sorting routine (or create the copy within the routine itself).

Arrays Are Different™ in C; see my answer here for a more detailed explanation.

Community
  • 1
  • 1
John Bode
  • 119,563
  • 19
  • 122
  • 198