0

I want to fill a two dimensional array within a for loop. However, that does not work. I have no clue why...

int main () {

int a = 32;
int x = 100;
int y = 1;
int z = 2;
int i, j;

int ar[32][2] = {{1,x},{}};
// works until here!

// gives me that
1   100
0   0
0   0
0   0
...


// I need the array filled
1              100
somenumber     somenumber
somenumber     somenumber
somenumber     somenumber
...

for (i = 1; i <= a; i++) {
    x = (x + y) * z;
    ar[i][i] = {{i + 1, x},{}};
}

return 0;
}

test.c: In function ‘main’: test.c:27:14: error: expected expression before ‘{’ token ar[i][i] = {{i + 1, x},{}};

Why isn't it filling the array?!

four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • `ar` contains only 20 `int`s, but your for loop tries to index 32 different values. `ar[i][i]` is an `int`; why are you trying to assign `{{i+1, x},{}}` to it? – aschepler May 05 '16 at 19:26
  • Also, `int y = 1.2;` results in a value of `1`. – aschepler May 05 '16 at 19:27
  • @aschepler I just realized. I corrected the numbers. I replaced variables to make it better understandable. – four-eyes May 05 '16 at 19:30
  • `ar[i][i]` is a single element, you know that, right? – Sourav Ghosh May 05 '16 at 19:33
  • @SouravGhosh no. What does that mean?! I thought I created a two dim array with the extend `[32][2]` – four-eyes May 05 '16 at 19:35
  • @stop correct. but you access _each element_ using `[x][y]` the xth and the yth of that element. If you want to assign a whole new matrix to yours, you just use its name as `yourArray = {{}, {}}` – Khalil Khalaf May 05 '16 at 19:38
  • @Stophface It means you cannot use the initialization or assignment of `ar[i][i] = {{i + 1, x},{}};`, instead it should be a single value. – David C. Rankin May 05 '16 at 19:39
  • @FirstStep I dont understand. I understand that I cannit use that `[x][y]` since its like "creating a new array". But how would I tell `c` that its supposed to use the `array` I created with `ar[32][2] = {{1,x},{}};` and fill the left over "slots" with the numbers I create?! – four-eyes May 05 '16 at 19:42

2 Answers2

1

Whoever explained to you that using [x][y] meant just to create a new array is kind of not accurate. Or you didn't understand right. You create an array and specify its size this way ElementType ArrayName[RowSize][ColumnSize]; point.

Now we created our 2D matrix. To access every element in it, we use [][] this notation, along with the desired "coordinates" of the element we want, the x and the y:

ArrayName[1][1] = 1; this will assign the value 1 to the element in the second column on the second row. Notice that your system will crash if you provide "out of range" x or y, which were provided when you created the array as RowSize and ColumnSize.

Check out your case fix:

for(int x = 0; x < YourArray.size(); x++)
{
    for(int y = 0; y < YourArray[i].size(); y++)
    {
         YourArray[x][y] = 1; // this will assign 1 to "all" and "every" element.
         // so if you need to fill you array with something, 
         // here is the place where you are iterating every element in your 2d matrix.
         // using two for loops, outer is for the rows and inner is for the columns
    }
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • awesome. now i got it! – four-eyes May 05 '16 at 19:58
  • @Stophface Just a heads up if you don't know already, this is pseudo code,, there is no native `.size()` function for arrays in `c`. You'll need to iterate using magic numbers (not preferred), #defines/constants (eg, `#define ARRAY_ROWS 32` and `#define ARRAY_COLS 2`), then declare your array with those constants and iterate over them, or use `sizeof`. See this: http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – yano May 05 '16 at 20:10
0

What it supposed to mean?

ar[i][i] = {{i + 1, x},{}};

ar[i][i] is integer. So {{i + 1, x},{}} is what? Integer expression? There are no such expressions in C.

Not to mention that ar is 32*2 array, so the first index, while i runs from 1 to 32. When i>=2, the second index is wrong, and when i=32, the first index is wrong too.

user31264
  • 6,557
  • 3
  • 26
  • 40
  • I want my array filled with the result of a calculation (as in the example I gave). And I do not know how to say within my for-loop at which position the array needs to be filled. So the first two slots are already taken. No I want second row, two columns filled. Then thrid row, two columns, and so forth – four-eyes May 05 '16 at 19:49