-1

I think its easy question! I have 2 dimension array with 0 for all element! now in each iteration I want to make one of the column equal to 1! for example: input={{0,0},{0,0}} out put:={{1,0},{1,0}} ,{{0,1},{0,1}} I wrote this code but I get very strange result!!!!

  int main() {

int j = 3;
int arr[5][j] = { 0 };
int dx = 0;
for (int g = 0; g < j; g++) {
    for (int t = 0; t < 5; t++) {
        arr[t][g] = 1;

    }
    dx = g;

    for (int d = 0; d < 5; d++) {
        for (int n = 0; n < j; n++) {
            cout << arr[d][n] << " ";
        }
        cout << endl;

    }
    cout << "next column=0" << " " << endl;
}
return 0;
 }

the out put is:

1 -1 64

1 2 0

1 0 4272944

1 65535 0

1 0 56

next column=0

1 1 64

1 1 0

1 1 4272944

1 1 0

1 1 56

next column=0

1 1 1

1 1 1

1 1 1

1 1 1

1 1 1

next column=0

sherek_66
  • 501
  • 5
  • 14
  • Fix your code formatting please. – drescherjm Oct 27 '17 at 15:44
  • is it good now? – sherek_66 Oct 27 '17 at 15:47
  • A little better but still not perfect. Normally I copy and paste the code from an IDE, select it and press the {} button and code is formatted properly. – drescherjm Oct 27 '17 at 15:48
  • Don't use [variable length arrays](https://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number). Do `const int j = 3` instead of `int j = 3`. Should fix all of your problems ([see it run here](https://ideone.com/rPcQBH)) – scohe001 Oct 27 '17 at 15:48
  • thanks for reply but in my code j is not const! – sherek_66 Oct 27 '17 at 15:54
  • @sherek_66 it must be. Your only other option will be to use dynamic allocation. I'll write up an answer to demonstrate. – scohe001 Oct 27 '17 at 15:57

1 Answers1

2

Variable length arrays are not allowed in C++. You have a couple options... (see them running by clicking the option numbers)

Option 1

Define a macro:

#define j 3
int arr[5][j] = {0};

Option 2

Define a constant variable:

const int j = 3;
int arr[5][j] = {0};

Option 3

Use dynamically allocated arrays (but make sure to delete them after you're done so we don't get memory leaks!)

int j = 3;
int** arr = new int*[5];
for(int i = 0; i < 5; i++)
    arr[i] = new int[j];

If you go dynamic, you'll need to make sure to deallocate the array after you're done:

for(int i = 0; i < 5; i++)
    delete [] arr[i];
delete [] arr;

Option 4

Use a C++ Standard Library container:

int j = 3;
std::vector<vector<int>> v(5, vector<int>(j, 0));
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • I appropriate! actually j is a int that is input! so I cin>> j. so in this case which one is better? – sherek_66 Oct 27 '17 at 16:14
  • If you can't force `j` to be constant, your only option is option 3. Just be careful with the dynamically allocated memory. Be sure to deallocate when you're done! – scohe001 Oct 27 '17 at 16:16
  • 1
    Option 4: use std::vector. – drescherjm Oct 27 '17 at 16:48
  • @drescherjm Good call. In another question, OP specified he couldn't use vectors so I didn't think to do it, but for others looking at this question in the future it'll be helpful. – scohe001 Oct 27 '17 at 16:53
  • thanks so much! the problem with vector is that when you want to fill it you should fill in order! but in array if i – sherek_66 Oct 27 '17 at 17:25
  • @sherek_66 see the code I posted with a `vector`. I've constructed the vector with values (just like you would an array), so it'll act the exact same way. it starts with `5*j` 0's in it just like your arrays do (click *Option 4* to see the running example) – scohe001 Oct 27 '17 at 17:27