Populating 2-D arrays

Discussion in 'C Programming' started by fgg, Oct 12, 2010.

1. fggGuest

Hello,

I'd like to populate the 4x3 array below using the function "function"
to determine the value of each element. This function takes 2
arguments, which will vary in a systematic way (see below). How can I
use for loops in C to accomplish this task? Thanks.

function(20,0.1) function(20,0.2) function(20,0.3)
function(21,0.1) function(21,0.2) function(21,0.3)
function(22,0.1) function(22,0.2) function(22,0.3)
function(23,0.1) function(23,0.2) function(23,0.3)

fgg, Oct 12, 2010

2. Ben PfaffGuest

fgg <> writes:

> I'd like to populate the 4x3 array below using the function "function"
> to determine the value of each element. This function takes 2
> arguments, which will vary in a systematic way (see below). How can I
> use for loops in C to accomplish this task? Thanks.

This looks like a homework problem to me.
--
Ben Pfaff
http://benpfaff.org

Ben Pfaff, Oct 12, 2010

3. Keith ThompsonGuest

fgg <> writes:
> I'd like to populate the 4x3 array below using the function "function"
> to determine the value of each element. This function takes 2
> arguments, which will vary in a systematic way (see below). How can I
> use for loops in C to accomplish this task? Thanks.
>
> function(20,0.1) function(20,0.2) function(20,0.3)
> function(21,0.1) function(21,0.2) function(21,0.3)
> function(22,0.1) function(22,0.2) function(22,0.3)
> function(23,0.1) function(23,0.2) function(23,0.3)

solutions directly.

Alternatively, what have you tried, and how did it not work?

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Oct 12, 2010
4. fggGuest

On Oct 12, 1:08 pm, Keith Thompson <> wrote:
> fgg <> writes:
> > I'd like to populate the 4x3 array below using the function "function"
> > to determine the value of each element. This function takes 2
> > arguments, which will vary in a systematic way (see below). How can I
> > use for loops in C to accomplish this task? Thanks.

>
> > function(20,0.1)  function(20,0.2)  function(20,0.3)
> > function(21,0.1)  function(21,0.2)  function(21,0.3)
> > function(22,0.1)  function(22,0.2)  function(22,0.3)
> > function(23,0.1)  function(23,0.2)  function(23,0.3)

>
> solutions directly.
>
> Alternatively, what have you tried, and how did it not work?
>
> --
> Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>     -- Antony Jay and Jonathan Lynn, "Yes Minister"

This is a small piece of my first program in C. I know I need two
nested loops (one for the function and one for the array), but I don't
really know how to integrate them. Here are the pieces I have so far:

int row, col;
float i, j;
float array[4][3];

for (col = 0; col < 3; col++)
for (row = 0; row < 4; row++)

???

for (j = 0.1; j < 0.4; j = j + 0.1)
for (i = 20; i < 24; i++)

???

array[col][row] = function(i,j);

BTW, here's my instructor's email address: .c

fgg, Oct 12, 2010
5. FredGuest

On Oct 12, 1:47 pm, fgg <> wrote:
> On Oct 12, 1:08 pm, Keith Thompson <> wrote:
>
>
>
>
>
> > fgg <> writes:
> > > I'd like to populate the 4x3 array below using the function "function"
> > > to determine the value of each element. This function takes 2
> > > arguments, which will vary in a systematic way (see below). How can I
> > > use for loops in C to accomplish this task? Thanks.

>
> > > function(20,0.1)  function(20,0.2)  function(20,0.3)
> > > function(21,0.1)  function(21,0.2)  function(21,0.3)
> > > function(22,0.1)  function(22,0.2)  function(22,0.3)
> > > function(23,0.1)  function(23,0.2)  function(23,0.3)

>
> > solutions directly.

>
> > Alternatively, what have you tried, and how did it not work?

>
> > --
> > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
> > Nokia
> > "We must do something.  This is something.  Therefore, we must do this."
> >     -- Antony Jay and Jonathan Lynn, "Yes Minister"

>
> This is a small piece of my first program in C. I know I need two
> nested loops (one for the function and one for the array), but I don't
> really know how to integrate them. Here are the pieces I have so far:
>
> int row, col;
> float i, j;
> float array[4][3];
>
> for (col = 0; col < 3; col++)
>     for (row = 0; row < 4; row++)
>

OK, which is it? here you say you have
three columns and four rows...

> for (j = 0.1; j < 0.4; j = j + 0.1)
>     for (i = 20; i < 24; i++)
>
> ???
>
> array[col][row] = function(i,j);

... and you have dimensioned array[4][3], so the above statement
says you have 4 cols and 3 rows.

Also, it is almost never correct to use a float (or double)
as the index in a do loop. Are you aware that
..1 + .1 does NOT equal .2 ?
For that matter, x=.1 does not set x equal to .1
but rather to some number that is close to but not
exactly equal to .1, since .1 cannot be represented exactly.

Perhaps in the first set of do's you want something like:

for ( nc=0; nc < numCols; nc++ ) {
for ( nr=0; nr < numRows; nr++ ) {
array[nc][nr] = function( (20+nr), (0.1*(nc+1)));
}
}

Now that depends on whether you mean array to be dimensioned
array(rows,cols) or array(cols,rows)

--
Fred K

Fred, Oct 12, 2010
6. fggGuest

On Oct 12, 2:32 pm, Fred <> wrote:
> On Oct 12, 1:47 pm, fgg <> wrote:
>
>
>
> > On Oct 12, 1:08 pm, Keith Thompson <> wrote:

>
> > > fgg <> writes:
> > > > I'd like to populate the 4x3 array below using the function "function"
> > > > to determine the value of each element. This function takes 2
> > > > arguments, which will vary in a systematic way (see below). How can I
> > > > use for loops in C to accomplish this task? Thanks.

>
> > > > function(20,0.1)  function(20,0.2)  function(20,0.3)
> > > > function(21,0.1)  function(21,0.2)  function(21,0.3)
> > > > function(22,0.1)  function(22,0.2)  function(22,0.3)
> > > > function(23,0.1)  function(23,0.2)  function(23,0.3)

>
> > > solutions directly.

>
> > > Alternatively, what have you tried, and how did it not work?

>
> > > --
> > > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
> > > Nokia
> > > "We must do something.  This is something.  Therefore, we must do this."
> > >     -- Antony Jay and Jonathan Lynn, "Yes Minister"

>
> > This is a small piece of my first program in C. I know I need two
> > nested loops (one for the function and one for the array), but I don't
> > really know how to integrate them. Here are the pieces I have so far:

>
> > int row, col;
> > float i, j;
> > float array[4][3];

>
> > for (col = 0; col < 3; col++)
> >     for (row = 0; row < 4; row++)

>
> OK, which is it? here you say you have
> three columns and four rows...
>
> > for (j = 0.1; j < 0.4; j = j + 0.1)
> >     for (i = 20; i < 24; i++)

>
> > ???

>
> > array[col][row] = function(i,j);

>
> .. and you have dimensioned array[4][3], so the above statement
> says you have 4 cols and 3 rows.
>
> Also, it is almost never correct to use a float (or double)
> as the index in a do loop. Are you aware that
> .1 + .1 does NOT equal .2 ?
> For that matter, x=.1 does not set x equal to .1
> but rather to some number that is close to but not
> exactly equal to .1, since .1 cannot be represented exactly.
>
> Perhaps in the first set of do's you want something like:
>
> for ( nc=0; nc < numCols; nc++ ) {
>    for ( nr=0; nr < numRows; nr++ ) {
>       array[nc][nr] = function( (20+nr), (0.1*(nc+1)));
>    }
>
> }
>
> Now that depends on whether you mean array to be dimensioned
> array(rows,cols) or array(cols,rows)
>
> --
> Fred K

Thanks for the constructive reply, Fred.

fgg, Oct 12, 2010
7. Jens Thoms ToerringGuest

fgg <> wrote:
> On Oct 12, 1:08Â pm, Keith Thompson <> wrote:
> > fgg <> writes:
> > > I'd like to populate the 4x3 array below using the function "function"
> > > to determine the value of each element. This function takes 2
> > > arguments, which will vary in a systematic way (see below). How can I
> > > use for loops in C to accomplish this task? Thanks.

> >
> > > function(20,0.1) Â function(20,0.2) Â function(20,0.3)
> > > function(21,0.1) Â function(21,0.2) Â function(21,0.3)
> > > function(22,0.1) Â function(22,0.2) Â function(22,0.3)
> > > function(23,0.1) Â function(23,0.2) Â function(23,0.3)

> >
> > solutions directly.
> >
> > Alternatively, what have you tried, and how did it not work?

> This is a small piece of my first program in C. I know I need two
> nested loops (one for the function and one for the array),

I guess you mean one for the rows and one for the columns...

> but I don't
> really know how to integrate them. Here are the pieces I have so far:

> int row, col;
> float array[4][3];

> for (col = 0; col < 3; col++)
> for (row = 0; row < 4; row++)

array[ row ][ col ] = function( row + 20, 0.1 * ( col + 1 ) )

> BTW, here's my instructor's email address: .c

Try not to be one yourself, there are lots of people that try to
get their homework done here and a typical sign for that is asking
some obscure but trivial question without showing the least attempt
of having tried to arrive at a solution themselves....

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de

Jens Thoms Toerring, Oct 12, 2010
8. fggGuest

On Oct 12, 2:42 pm, (Jens Thoms Toerring) wrote:
> fgg <> wrote:
> > On Oct 12, 1:08 pm, Keith Thompson <> wrote:
> > > fgg <> writes:
> > > > I'd like to populate the 4x3 array below using the function "function"
> > > > to determine the value of each element. This function takes 2
> > > > arguments, which will vary in a systematic way (see below). How can I
> > > > use for loops in C to accomplish this task? Thanks.

>
> > > > function(20,0.1)  function(20,0.2)  function(20,0.3)
> > > > function(21,0.1)  function(21,0.2)  function(21,0.3)
> > > > function(22,0.1)  function(22,0.2)  function(22,0.3)
> > > > function(23,0.1)  function(23,0.2)  function(23,0.3)

>
> > > solutions directly.

>
> > > Alternatively, what have you tried, and how did it not work?

> > This is a small piece of my first program in C. I know I need two
> > nested loops (one for the function and one for the array),

>
> I guess you mean one for the rows and one for the columns...
>
> > but I don't
> > really know how to integrate them. Here are the pieces I have so far:
> > int row, col;
> > float array[4][3];
> > for (col = 0; col < 3; col++)
> >     for (row = 0; row < 4; row++)

>
>           array[ row ][ col ] = function( row + 20, 0.1 * ( col + 1 ) )
>
> > BTW, here's my instructor's email address:

>
> Try not to be one yourself, there are lots of people that try to
> get their homework done here and a typical sign for that is asking
> some obscure but trivial question without showing the least attempt
> of having tried to arrive at a solution themselves....
>
>                                 Regards, Jens
> --
>   \   Jens Thoms Toerring  ___
>    \__________________________      http://toerring.de

Ok... let's always assume that's the case and treat newcomers with
sarcasm.

fgg, Oct 12, 2010
9. SeebsGuest

On 2010-10-12, fgg <> wrote:
> BTW, here's my instructor's email address: .c

Wow, would you look at that? I was trying to find an efficient way
to compute DBL_EPSILON, but it turns out it was much faster to read
this post and then measure my level of interest in spending time
helping you.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach /
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Seebs, Oct 12, 2010
10. SeebsGuest

On 2010-10-12, fgg <> wrote:
> Ok... let's always assume that's the case and treat newcomers with
> sarcasm.

Generally, people who aren't trying to get their homework done are much
more interested in solving their problems, and this can be observed because
they're having.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach /
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Seebs, Oct 12, 2010
11. Keith ThompsonGuest

fgg <> writes:
> On Oct 12, 2:42Â pm, (Jens Thoms Toerring) wrote:
>> fgg <> wrote:

[...]
>> > BTW, here's my instructor's email address: .c

>>
>> Try not to be one yourself, there are lots of people that try to
>> get their homework done here and a typical sign for that is asking
>> some obscure but trivial question without showing the least attempt
>> of having tried to arrive at a solution themselves....
>>

> Ok... let's always assume that's the case and treat newcomers with
> sarcasm.

Good idea, let's do that!

Seriously, you're here asking us for help. Understand that we do
get a lot of "Do my homework for me!" posts, and there was no way
to tell that yours wasn't just another. For that matter, from what
you've told us so far it's still not clear that you weren't just

(and don't take that suggestion as an insult; it really isn't intended
as one).

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Oct 13, 2010
12. Ben BacarisseGuest

superpollo <> writes:

> fgg ha scritto:
>> On Oct 12, 1:08 pm, Keith Thompson <> wrote:
>>> fgg <> writes:
>>>> I'd like to populate the 4x3 array below using the function "function"
>>>> to determine the value of each element. This function takes 2
>>>> arguments, which will vary in a systematic way (see below). How can I
>>>> use for loops in C to accomplish this task? Thanks.
>>>> function(20,0.1) function(20,0.2) function(20,0.3)
>>>> function(21,0.1) function(21,0.2) function(21,0.3)
>>>> function(22,0.1) function(22,0.2) function(22,0.3)
>>>> function(23,0.1) function(23,0.2) function(23,0.3)
>>> solutions directly.

> ...
>> BTW, here's my instructor's email address: .c

>
> LOL, now THIS will get you some help alrighty!

Well, it worked! The OP had two full answers within two hours.

--
Ben.

Ben Bacarisse, Oct 13, 2010
13. BartCGuest

"fgg" <> wrote in message
news:...
> Hello,
>
> I'd like to populate the 4x3 array below using the function "function"
> to determine the value of each element. This function takes 2
> arguments, which will vary in a systematic way (see below). How can I
> use for loops in C to accomplish this task? Thanks.
>
> function(20,0.1) function(20,0.2) function(20,0.3)
> function(21,0.1) function(21,0.2) function(21,0.3)
> function(22,0.1) function(22,0.2) function(22,0.3)
> function(23,0.1) function(23,0.2) function(23,0.3)

#include <stdio.h>

int function(int a,double x) {
int b = (x+0.05)*10;
return a*100+b;
}

int main(void){
#define rows 4
#define cols 3
int col,row;

int array[cols][rows]={0};

for (row=0; row<rows; ++row)
for (col=0; col<cols; ++col)
array[col][row] = function (row+20, (col+1)*0.1);

for (row=0; row<rows; ++row) {
for (col=0; col<cols; ++col)
printf("%d ",array[col][row]);
printf("\n");
}

}

(I think array could have been defined as array[rows][cols] too, with other
subscripts also reversed.)

--
Bartc

BartC, Oct 13, 2010
14. fggGuest

On Oct 12, 5:43 pm, "BartC" <> wrote:
> "fgg" <> wrote in message
>
> news:...
>
> > Hello,

>
> > I'd like to populate the 4x3 array below using the function "function"
> > to determine the value of each element. This function takes 2
> > arguments, which will vary in a systematic way (see below). How can I
> > use for loops in C to accomplish this task? Thanks.

>
> > function(20,0.1)  function(20,0.2)  function(20,0.3)
> > function(21,0.1)  function(21,0.2)  function(21,0.3)
> > function(22,0.1)  function(22,0.2)  function(22,0.3)
> > function(23,0.1)  function(23,0.2)  function(23,0.3)

>
> #include <stdio.h>
>
> int function(int a,double x) {
>  int b = (x+0.05)*10;
>  return a*100+b;
>
> }
>
> int main(void){
> #define rows 4
> #define cols 3
>  int col,row;
>
>  int array[cols][rows]={0};
>
>  for (row=0; row<rows; ++row)
>    for (col=0; col<cols; ++col)
>      array[col][row] = function (row+20, (col+1)*0.1);
>
>  for (row=0; row<rows; ++row) {
>    for (col=0; col<cols; ++col)
>      printf("%d ",array[col][row]);
>    printf("\n");
>  }
>
> }
>
> (I think array could have been defined as array[rows][cols] too, with other
> subscripts also reversed.)
>
> --
> Bartc

Thank you very much, Bartc. It looks like I don't need a separate loop
for the function arguments after all.

fgg, Oct 13, 2010
15. Francois GrieuGuest

On 12/10/2010 23:53, fgg wrote:
> I'd like to populate the 4x3 array below using the function "function"
> to determine the value of each element. This function takes 2
> arguments, which will vary in a systematic way (see below). How can I
> use for loops in C to accomplish this task? Thanks.
>
> function(20,0.1) function(20,0.2) function(20,0.3)
> function(21,0.1) function(21,0.2) function(21,0.3)
> function(22,0.1) function(22,0.2) function(22,0.3)
> function(23,0.1) function(23,0.2) function(23,0.3)
>
> Here are the pieces I have so far

[commented and edited to make a program that compiles in my head]

// global array to populate
float array[4][3];

// prototype of some function
float function(int i, float j);

int main(void)
{
int row, col;
for (col = 0; col< 3; col++)
for (row = 0; row< 4; row++)
array[ row ][ col ] = function( row + 20, 0.1 * ( col + 1 ) );
return 0;
}

Seasoned programmers avoid duplication of numerical constants,
especially when they are index bounds like 3 and 4 in the above.
That's by far the most important remark in this post. One way
to fix this is define preprocessor constants for 3 and 4, then
use them. Another method is illustrated at the end of this post.

Beware that above code may not exactly meet the original
statement, due to rounding errors.

Also, it is not immediately clear if the following code fragment
which at some point was in the thread:
float j;
for (j = 0.1; j < 0.4; j = j + 0.1)
loops 3 or 4 times, and if the third value of j will be
exactly equal to any of the (conceivably different)
(float)0.3
(float)(3*0.1)
3*(float)0.1
Also, seasoned programmers consider using j for a "real"
variable poor taste.

If you want to improve on that, use
double y;
for (y = 0.1; y < 0.35; y += 0.1)

Except for arrays (especially huge ones) and other exceptions,
the first choice should be to define all variables holding
"real" values as double rather than float; <math.h> and other
libraries do this.

When given a choice and dealing with multi-dimensional
arrays, I arrange things so that the right index is the
one in the inner loop; this help hardware because memory
is used linearly; it may also help humans doing debug.

I prefer ++col to col++ because
- the intend (incrementation) is the first thing read;
- it is a closer equivalent to col += 1 or col = col+1;
- some old or minimalist compilers generate better code.

So all in all that gives:

// global array[col][row] to populate
float array[3][4];
const int ncol = sizeof( array)/sizeof( *array);
const int nrow = sizeof(*array)/sizeof(**array);

// prototype of some function
double function(int i, double y);

int main(void)
{
int col, row;
for (col = 0; col<ncol; ++col)
for (row = 0; row<nrow; ++row)
array[col][row] = function( row+20, (col+1)*0.1 );
return 0;
}

Francois Grieu

Francois Grieu, Oct 14, 2010
16. Mark WoodingGuest

Francois Grieu <> writes:

> Also, seasoned programmers consider using j for a "real" variable poor
> taste.

Really? This (somewhat mathematically inclined) programmer considers
use of i, j, and k for index variables to be in perfectly fine taste.

* I have a mathematical background, largely involving number theory
and algebra. To me, i is the usual imaginary unit, though I
understand that some physicists use j instead.

* The use of i, j, and k as indices, or indeed, as the basis vectors
for a 3-dimensional vector space, seems sufficiently commonplace as
to be unremarkable.

* Many -- probably even most -- programs don't make use of complex
arithmetic or analysis; I'd bet that almost all programs /do/ make
use of indexing, and probably most make some use of (at least)
double indexing. Therefore reserving the convenient and natural
name j for the former purpose seems like a poor idea.

* The imaginary unit already has a name in C. In fact, it has three,
all defined in <complex.h>. By far the most convenient of the three
is I, which is, happily, distinct from both i and j by virtue of
case-sensitivity. Were complex arithmetic not available directly in
C, I'd expect that naming the imaginary unit explicitly was unlikely
to be very useful, given that one would probably need explicit
constructor macros or functions for one's complex numbers anyway,
and they'd be likely more convenient than constructing complex
numbers using arithmetic using an explicitly named imaginary unit.

-- [mdw]

Mark Wooding, Oct 31, 2010
17. Keith ThompsonGuest

Mark Wooding <> writes:
> Francois Grieu <> writes:
>> Also, seasoned programmers consider using j for a "real" variable poor
>> taste.

>
> Really? This (somewhat mathematically inclined) programmer considers
> use of i, j, and k for index variables to be in perfectly fine taste.

I think Francois meant "real" as "floating-point". i, j, and k are
often good names for array indices, which are integers; calling a
floating-point variable "j" is usually a bad idea (unless there's
a specific reason for in in the problem domain).

[...]

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Oct 31, 2010