Velocity Reviews > Populating 2-D arrays

# Populating 2-D arrays

fgg
Guest
Posts: n/a

 10-12-2010
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)

Ben Pfaff
Guest
Posts: n/a

 10-12-2010
fgg <(E-Mail Removed)> 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

Keith Thompson
Guest
Posts: n/a

 10-12-2010
fgg <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"

fgg
Guest
Posts: n/a

 10-12-2010
On Oct 12, 1:08*pm, Keith Thompson <(E-Mail Removed)> wrote:
> fgg <(E-Mail Removed)> 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) (E-Mail Removed) *<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: (E-Mail Removed).c

Fred
Guest
Posts: n/a

 10-12-2010
On Oct 12, 1:47*pm, fgg <(E-Mail Removed)> wrote:
> On Oct 12, 1:08*pm, Keith Thompson <(E-Mail Removed)> wrote:
>
>
>
>
>
> > fgg <(E-Mail Removed)> 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) (E-Mail Removed) *<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

fgg
Guest
Posts: n/a

 10-12-2010
On Oct 12, 2:32*pm, Fred <(E-Mail Removed)> wrote:
> On Oct 12, 1:47*pm, fgg <(E-Mail Removed)> wrote:
>
>
>
> > On Oct 12, 1:08*pm, Keith Thompson <(E-Mail Removed)> wrote:

>
> > > fgg <(E-Mail Removed)> 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) (E-Mail Removed) *<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.

Jens Thoms Toerring
Guest
Posts: n/a

 10-12-2010
fgg <(E-Mail Removed)> wrote:
> On Oct 12, 1:08Â*pm, Keith Thompson <(E-Mail Removed)> wrote:
> > fgg <(E-Mail Removed)> 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: (E-Mail Removed).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 ___ (E-Mail Removed)
\__________________________ http://toerring.de

fgg
Guest
Posts: n/a

 10-12-2010
On Oct 12, 2:42*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> fgg <(E-Mail Removed)> wrote:
> > On Oct 12, 1:08*pm, Keith Thompson <(E-Mail Removed)> wrote:
> > > fgg <(E-Mail Removed)> 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: (E-Mail Removed)

>
> 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 *___ * * *(E-Mail Removed)
> * *\__________________________ * * *http://toerring.de

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

Seebs
Guest
Posts: n/a

 10-12-2010
On 2010-10-12, fgg <(E-Mail Removed)> wrote:
> BTW, here's my instructor's email address: (E-Mail Removed).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 / (E-Mail Removed)
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
Guest
Posts: n/a

 10-12-2010
On 2010-10-12, fgg <(E-Mail Removed)> 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 / (E-Mail Removed)
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.