Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Populating 2-D arrays

Reply
Thread Tools

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)
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      10-12-2010
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2010
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)


Please give us your instructor's e-mail address so we can submit our
solutions directly.

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

--
Keith Thompson (The_Other_Keith) kst- <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"
 
Reply With Quote
 
fgg
Guest
Posts: n/a
 
      10-12-2010
On Oct 12, 1:08*pm, Keith Thompson <ks...@mib.org> wrote:
> fgg <fabioguimaraesgoncal...@gmail.com> 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)

>
> Please give us your instructor's e-mail address so we can submit our
> solutions directly.
>
> Alternatively, what have you tried, and how did it not work?
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org *<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
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      10-12-2010
On Oct 12, 1:47*pm, fgg <fabioguimaraesgoncal...@gmail.com> wrote:
> On Oct 12, 1:08*pm, Keith Thompson <ks...@mib.org> wrote:
>
>
>
>
>
> > fgg <fabioguimaraesgoncal...@gmail.com> 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)

>
> > Please give us your instructor's e-mail address so we can submit our
> > solutions directly.

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

>
> > --
> > Keith Thompson (The_Other_Keith) ks...@mib.org *<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
 
Reply With Quote
 
fgg
Guest
Posts: n/a
 
      10-12-2010
On Oct 12, 2:32*pm, Fred <fred.l.kleinschm...@boeing.com> wrote:
> On Oct 12, 1:47*pm, fgg <fabioguimaraesgoncal...@gmail.com> wrote:
>
>
>
> > On Oct 12, 1:08*pm, Keith Thompson <ks...@mib.org> wrote:

>
> > > fgg <fabioguimaraesgoncal...@gmail.com> 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)

>
> > > Please give us your instructor's e-mail address so we can submit our
> > > solutions directly.

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

>
> > > --
> > > Keith Thompson (The_Other_Keith) ks...@mib.org *<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.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      10-12-2010
fgg <> wrote:
> On Oct 12, 1:08Â*pm, Keith Thompson <ks...@mib.org> wrote:
> > fgg <fabioguimaraesgoncal...@gmail.com> 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)

> >
> > Please give us your instructor's e-mail address so we can submit our
> > 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
 
Reply With Quote
 
fgg
Guest
Posts: n/a
 
      10-12-2010
On Oct 12, 2:42*pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> fgg <fabioguimaraesgoncal...@gmail.com> wrote:
> > On Oct 12, 1:08*pm, Keith Thompson <ks...@mib.org> wrote:
> > > fgg <fabioguimaraesgoncal...@gmail.com> 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)

>
> > > Please give us your instructor's e-mail address so we can submit our
> > > 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: dont.be.a.j...@comp.lang.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 *___ * * *j...@toerring.de
> * *\__________________________ * * *http://toerring.de


Ok... let's always assume that's the case and treat newcomers with
sarcasm.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      10-12-2010
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 / usenet-
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      10-12-2010
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 talk about what they already tried, and what specific problems
they're having.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Declaring/populating variable arrays in ASP.NET??? Friday ASP .Net 35 05-10-2005 06:24 PM
Declaring/populating variable arrays in ASP.NET??? Friday ASP .Net Web Services 33 05-10-2005 06:24 PM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57