0
#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++?

  • 6
    Use `std::vector` – UnholySheep Jul 15 '21 at 08:48
  • 3
    `int arr[i];` defines `arr` as a brand new variable, a [variable-length array](https://en.wikipedia.org/wiki/Variable-length_array) which [aren't really a part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). For compilers which have that non-portable and non-standard extension. it defines an array with `i` elements. So the first iteration you have an array of zero elements. And as you use `i` as index it will of course be out of bounds. I guess you don't actually want to define a new variable `arr` inside the loop? – Some programmer dude Jul 15 '21 at 08:51
  • 3
    BTW why do you need the `arr` array at all? You never use the array as a whole. You could replace `arr[i]` with `temp` and declare `int temp;` right at the beginning of the loop. – Jabberwocky Jul 15 '21 at 08:54
  • @Some programmer dude after the for loop array size would be equal to the digit number and its values would be assigned by the for loop simultaneously. – PhysicsSolvesAll Jul 15 '21 at 08:55
  • So what you really want is to create a *vector* and then *push back* a value inside the loop? Inside the loop itself use a temporary variable as suggested by @Jabberwocky, at the end call `push_back` of the vector passing the temporary variable. – Some programmer dude Jul 15 '21 at 08:56
  • @Jabberwocky how to write it without array but with a temp variable?? – PhysicsSolvesAll Jul 15 '21 at 08:56
  • @Some programmer dude I dont really know what vector is. – PhysicsSolvesAll Jul 15 '21 at 08:57
  • 1
    Invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes. Learn C++ and programming properly. – Some programmer dude Jul 15 '21 at 08:57
  • 1
    @PhysicsSolvesAll exactly as I wrote in my comment. But I suppose you want to use the `arr` array once the loop is terminated. So my previous comment is probably rather pointless. – Jabberwocky Jul 15 '21 at 09:03
  • Yes, I wanted to use the array after the for loop has been terminated. :) @Jabberwocky – PhysicsSolvesAll Jul 15 '21 at 09:08

2 Answers2

1

You probably want something like this:

#include <iostream>
#include <cmath>
#include <vector>

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;
  std::vector<int> arr(digitNumber);  // initialize a vector containing 
                                      // digitNumber elements

  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;
  }

  // printf values in arr
  cout << "arr: ";
  for (auto& value : arr)
    cout << value << ", ";

  return true;
}

int main() {
  int value = 153;
  narcissistic(value);

  return 0;
}

But this is a rather convoluted way to decompose a number into it's digits. You don't need floating point arithmetic at all to do this:

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

bool narcissistic(int value)
{
  cout << "value is:" << value << endl;
  std::vector<int> arr;  // start off with an empty vector

  for (int i = 0; value != 0; i++)
  {
    arr.push_back(value % 10);  // add to the vector
    value /= 10;
  }

  // printf values in arr
  cout << "arr: ";
  for (auto& value : arr)
    cout << value << ", ";

  return true;
}

int main() {
  int value = 153;
  narcissistic(value);

  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
-1

If you want to avoid using vector, you can use this:

#include <iostream>
#include <cmath>
#include <vector>

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;
  
  vector<int> v(digitNumber);
  int *arr = &v[0];

  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;
  }

  // print values in arr
  cout << "arr: ";
  for (int i = 0; i <= digitNumber - 1; i++)
    cout << arr[i] << ", ";

  return true;
}

int main() {
  int value = 153;
  narcissistic(value);

  return 0;
}
DonLarry
  • 702
  • 2
  • 11
  • 22