-11

I would like to create 2^n variables with a piece of code, with the following property:

If n=1, the variables should be named n0 n1

If n=2, the variables should be named n00 n01 n10 n11

If n=3, the variables should be named n000 n001 n010 ..

Maximum value of n is bounded and known so no worries there. The bits and therefore the variables should be in sequence.

I am thinking of creating an array of 2^n elements and then manipulate 2^n bits one by one and append to n. This seems a trivial and lengthy solution. Is there a more elegant way? C or C++ is fine with me.

alk
  • 69,737
  • 10
  • 105
  • 255
zorro
  • 94
  • 2
  • 10
  • the variables are strings and "n0","n1" ... is their values? – 463035818_is_not_an_ai Apr 23 '16 at 12:09
  • 1
    what's the highest value of n?? – Abu Hanifa Apr 23 '16 at 12:10
  • 1
    its not really clear what you are asking. To "create 2^n variables", compute x = 2^n and then `std::vector vect = std::vector(x);` the variables in the vector are stored in consecutive memory – 463035818_is_not_an_ai Apr 23 '16 at 12:11
  • 3
    This doesn't quite make sense, do you need variables or values? Do you need variables with certain values? You are just counting here by the way. – harold Apr 23 '16 at 12:12
  • If you just need a way to enumerate non-negative integers, then there may be better, existing tools for that. – Kerrek SB Apr 23 '16 at 12:13
  • For C: `unsigned char * p = malloc((n+1)/CHAR_BIT)`? – alk Apr 23 '16 at 12:13
  • Really, mallocing? The question was tagged 'c++' (and 'c'), can't you prove some modern suggestion? – JVApen Apr 23 '16 at 12:15
  • 2
    @JVApen `malloc` is still the appropriate function to use in the current version of C. Blame the OP for dual-tagging, don't blame people responding for picking the tag that doesn't have your personal preference. –  Apr 23 '16 at 12:17
  • @harold I added more information. I need the variables with certain names as specified. – zorro Apr 23 '16 at 12:22
  • @zorro So...this isn't about C or C++ at all? Or are you saying you want to write a program in C that outputs more C code? – J... Apr 23 '16 at 12:24
  • 1
    You can write a program to accept an input of n, and generate code that defines the varibles for you. You cannot do generate names for variables in C, C is a static typed language without reflection. – fluter Apr 23 '16 at 12:24
  • 2
    @zorro Variables are purely a compile-time concept. Is `n` a compile-time constant? Otherwise you can't create variables dynamically. You can create objects dynamically, but they won't have variable names. –  Apr 23 '16 at 12:25
  • 1
    You know that C and C++ offer arrays, so that you don't need to manually type (or generate) 2048 variable names? – Jan Christoph Terasa Apr 23 '16 at 12:26
  • This question is related to http://stackoverflow.com/q/2633400/694576 if not a duplicate to it? – alk Apr 23 '16 at 12:29
  • @hvd `n` would be known during the course of program. Your comment makes sense. So I would use objects without the name in one structure. Another structure would simply contain the values of bit sequences. A simple mapping between them will be able to do my work. Thanks! – zorro Apr 23 '16 at 12:34

2 Answers2

2

There is no reason to manually name hundreds or thousands of variables, just use an array:

#include <stdlib.h>

int main(int argc, char **argv) {
        int n = 10;
        /* static */
        int a[1<<n];

        /* dynamic */
        size_t bsize = 1<<n; /* 1024 */
        int *b = malloc(bsize * sizeof(int)); /* 1024 ints */

        return 0;
}
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
1

If you like to create a piece of code having that many different variables with a different name, I suggest you start writing your own code generator. The code below should be a good start.

#include <sstream>
#include <iostream>
#include <string>
#include <vector>

namespace {
template <typename Function>
void bitExecutor(int nrOfBits, std::vector<bool> &bits, const Function &f) {
  if (nrOfBits) {
    bits.push_back(false);
    bitExecutor(nrOfBits - 1, bits, f);
    bits.back() = true;
    bitExecutor(nrOfBits - 1, bits, f);
    bits.pop_back();
  } else {
    f(bits);
  }
}
}

int main(int argc, char **argv) {
  int nrOfBits = 4; // Let's assume runtime init

  std::vector<std::string> values;
  std::vector<bool> bits;
  bitExecutor(nrOfBits, bits, [&values](std::vector<bool> &bits) {
    std::string buffer;
    buffer += "n";
    for (auto &&bit : bits) {
      if (bit)
        buffer += "1";
      else
        buffer += "0";
    }
    values.emplace_back(std::move(buffer));
  });

  for (const auto &value : values)
    std::cout << value << std::endl;
  return 0;
}

That said, I think you are trying to tackle the wrong problem here. Why would like to have a lot variables representing the same thing, while you can use things like std::vector, which totally works with indexing. See also the 'values' variable in this example, which you can index like values[0] // first element ... and get the matching values

JVApen
  • 11,008
  • 5
  • 31
  • 67