I am struggling with C++. I learned Java before and i think it is much easier, tbh. Currently, I am trying to code a counting sort algorithm with c++. I want to get the maximum of my array to declare my helping array with the size = maximum +1; I tried several ways but nothing works. It always shows "variable cannot be used as a constant". I also have found a code which does the same as mine but seems like to work. Can anyone give me some hints or solutions?
Thanks in advance.
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
int getMax(int arr[], int size)
{
int max = arr[0];
for (int i = 1; i < sizeof(arr); i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void countingSort(int *arr, int size)
{
int max = getMax(arr, size);
int hilfsArr[max + 1];
}