0

I want to declare and initialize one of two variables, depending on certain conditions that depend on the user. The only way I know how to do this is using IF. The problem is, I want the variable to be used outside of the IF function, which is not possible due to the scope of the variable.

Specifically, I have a 2D array, representing a table. The sizes of the array are input by the user, and I need to add either additional rows or additional columns such that the array is of the same size for both indices. I have global variables for the user input, and I need the added rows or added columns to be a new global variable. They cannot be stored in the same variable either, as further calculations will vary depending on which one is present.

EDIT: My college prof said uninitialized variables don't consume memory, which this stackoverflow Q says is not true. I guess deleting unwanted variables during runtime seems to be the best option, though I don't know how to do that.

Community
  • 1
  • 1
Arctus
  • 33
  • 1
  • 6
  • 1
    *"The problem is, I want the variable to be used outside of the IF function, which is not possible due to the scope of the variable."* You have to declare it outside the `if()` block and initialize them in the `if()` block later. – user2004685 Mar 27 '16 at 17:52
  • I know that option, problem is, that adds an additional unused variable. Ideally I should be avoiding that, no? – Arctus Mar 27 '16 at 18:54
  • Why don't you just use a single variable and then initialize it as per your needs? Also, you should start thinking about optimization **after** you achieve the functionality. – user2004685 Mar 27 '16 at 19:46
  • Depending on which is initialized, further calculations will change. Only way to know which calculation path to take is from the variable that is initialized. – Arctus Apr 08 '16 at 12:40

0 Answers0