Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Populating 2-D arrays

 
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2010
fgg <(E-Mail Removed)> writes:
> On Oct 12, 2:42*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
>> fgg <(E-Mail Removed)> wrote:

[...]
>> > BTW, here's my instructor's email address: http://www.velocityreviews.com/forums/(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....
>>

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

--
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"
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-13-2010
superpollo <(E-Mail Removed)> writes:

> fgg ha scritto:
>> 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)
>>> Please give us your instructor's e-mail address so we can submit our
>>> solutions directly.

> ...
>> BTW, here's my instructor's email address: (E-Mail Removed).c

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


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

--
Ben.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      10-13-2010


"fgg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
fgg
Guest
Posts: n/a
 
      10-13-2010
On Oct 12, 5:43*pm, "BartC" <(E-Mail Removed)> wrote:
> "fgg" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.

 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      10-14-2010
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
 
Reply With Quote
 
Mark Wooding
Guest
Posts: n/a
 
      10-31-2010
Francois Grieu <(E-Mail Removed)> 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]
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-31-2010
Mark Wooding <(E-Mail Removed)> writes:
> Francois Grieu <(E-Mail Removed)> 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) (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"
 
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