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?!

8
  • ar contains only 20 ints, 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? Commented May 5, 2016 at 19:26
  • Also, int y = 1.2; results in a value of 1. Commented May 5, 2016 at 19:27
  • @aschepler I just realized. I corrected the numbers. I replaced variables to make it better understandable. Commented May 5, 2016 at 19:30
  • ar[i][i] is a single element, you know that, right? Commented May 5, 2016 at 19:33
  • @SouravGhosh no. What does that mean?! I thought I created a two dim array with the extend [32][2] Commented May 5, 2016 at 19:35

2 Answers 2

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

awesome. now i got it!
@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: stackoverflow.com/questions/37538/…
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.