#include <iostream>
#include <cmath>
using namespace std;
bool narcissistic(int value)
{
cout << "value is:" << value << endl;
int digitNumber = (log10(value) + 1);
cout << "Digit Number:" << digitNumber << endl;
int sum = 0;
int arr[5];
for (int i = 0; i <= digitNumber - 1; i++)
{
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = (value % (exponential *10)) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
return true;
}
int main() {
int value = 153;
narcissistic(value);
return 0;
}
In this code here; I had to write:
int arr[5];
But I wanted the size of this array to be a variable so that its size could be defined and its values could be put by for loop so its size could be equal to the amount of loop. I wanted to write like this:
for (int i = 0; i <= digitNumber - 1; i++)
{
int arr[i];
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = (value % (exponential *10)) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
Visual Studio says that the value that the array takes as length must be constant that's why you can't determine it by loops etc.
Is there a way to put a variable as size in an array and assign an array's size by a loop in C++?