0

iam writing the code for the radix sort, i saw some online but when i wrote in the visual studio, it doesnot accept the const size in countSort. can you guys help me with this. thank you so much it has [problem at function countsort (output[size])/ IDK how to fix it, can you guys give me a hand

#include <iostream>
#pragma comment(linker, "/STACK:8000000")
#include <ctime>
#include <iomanip>
#include <stdio.h>
using namespace std;
void radixSort (int [], int);
int maxNumber ( int[], int);
void countSort(int [], int , int );

int main(){
    const int size = 8;
    int arr [] = {110, 5, 10,3 ,22, 100, 1, 23};
    //int size = sizeof(arr)/sizeof(arr[0]);
    for ( int i = 0; i <size; i++){
        cout << setw(7) <<arr[i] ;
    }
for ( int i = 0; i < size; i++){
        cout << setw(7)  <<arr[i] ;
    }
    cout << endl;

    radixSort (arr, size);

    system("pause");
    return 0;
}


void radixSort ( int arr[], int size){
    int exp, m;

    m  = maxNumber(arr, size);

    for (exp = 1; m/exp > 0; exp *= 10)
        countSort(arr, size, exp);
}

int maxNumber ( int arr[], int size){
    int max = arr[0];
    for (int i = 1; i < size; i++)
        if (arr[i] > max)
            max = arr[i];
    return max;
}

void countSort(int arr[],  int size , int exp)
{ 


    **int output[size];**
    int i, count[10] = {0};


    for (i = 0; i < size; i++)
        count[(arr[i] / exp) % 10]++;


    for (i = 1; i < 10; i++)
        count[i] += count[i-1];

    for (i = size - 1; i >= 0; i--)
    {
        output[count[(arr[i] / exp) % 10] - 1] = arr[i];
        count[(arr[i] / exp) % 10]--;
    }

    for (i = 0; i < size; i++)
        arr[i] = output[i];
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Sean Pham
  • 11
  • 2

1 Answers1

1

You can't declare an array with a size unknown at compile time like this:

int output[size];

You need to declare it dynamically like this:

int* output = new int[size];

Note that you are now responsible for cleaning up the memory you have allocated here and will need to delete it when you're finished with it:

delete[] output;

Or better yet use a standard c++ container like std::vector