Hi there, new to C here. I can't find information anywhere on how I could extract say a 2nd column from a 2D array and save it as a 1D array. Many thanks if you could help.
Hi there, new to C here. I can't find information anywhere on how I could extract say a 2nd column from a 2D array and save it as a 1D array. Many thanks if you could help.
It's not built-in, like you would get from an array language. You'll have to do it yourself. I would suggest a for loop.
if I have a 2D array x[m][n], then why doesn't this work:
for (m=0; m <= NR-1; m++);
x_values[m] = x[m][0]; (storing column 1 in a separate array)
it compiles just fine, but printed elements of an array don't make sense.
all array elements start with 0. So if you declare # of rows as 50, you would need to do the iteration only 49 times.
my bad, if you declare # of rows as 50, and begin with row = 0, you would need to do the for loop until row = 49, right?
No. You would loop until it reached 50, at which point the test would fail, so it wouldn't execute the body of the loop for x == 50.Code:for( x = 0; x < 50; x++ ) printf( "x is %d\n", x ) /*Would print 0 ... 49*/ printf( "x is %d\n", x ); /*Would print x is 50*/
Quzah.
Hope is the first step on the road to disappointment.
what about the original question
for (m=0; m <= 999; m++);
n=2;
B[m] = A[m][n];
printf("new array %f \n", B[m]);
can't figure out why this doesn't work
If you ever see a semi-colon at the end of a line with a for-statement on it, then you know you have screwed up.