Populating 2-D arrays

F

fgg

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)
 
B

Ben Pfaff

fgg said:
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.
 
K

Keith Thompson

fgg said:
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?
 
F

fgg

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) (e-mail address 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 address removed)
 
F

Fred

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) (e-mail address 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)
 
F

fgg

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)


Thanks for the constructive reply, Fred.
 
J

Jens Thoms Toerring

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 address 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
 
F

fgg

fgg said:
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 address 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

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

Seebs

BTW, here's my instructor's email address: (e-mail address removed)

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
 
S

Seebs

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
 
K

Keith Thompson

fgg said:
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
asking us to do your homework for you.

Suggested reading: <http://www.catb.org/~esr/faqs/smart-questions.html>
(and don't take that suggestion as an insult; it really isn't intended
as one).
 
B

BartC

fgg said:
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.)
 
F

fgg

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.)

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

Francois Grieu

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
 
M

Mark Wooding

Francois Grieu said:
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]
 
K

Keith Thompson

Mark Wooding said:
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).

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top