## Re: loops and arrays

• From: "Rui Batista" <ruiandrebatista@xxxxxxxxx>
• To: <programmingblind@xxxxxxxxxxxxx>
• Date: Tue, 16 Oct 2007 23:46:26 +0100

Hi,

See below my answers:
Daniel Dalton wrote:

Hi,

Could someone help me out with this?
I have an array called array declared like this:
int array [5][4];
So I think this is called a multidimensional  array.

The book said I need to assign a random number to each array index. (Using the rand library function.
In stlib.h)

I then have to print them to the screen.
In a table. From what I can understand 5 column's and 4 rows.
Should my code look something like this? It doesn't seem to work.
Could someone tell me where I am going wrong?

#include <stdio.h>
#include <stdlib.h>

int main ()
{
int array[5][4];
int a, b;

for(a =0; a < 5; a++)
{
for(b =0; b < 4; b++)
{
array [a][b] =rand ();
}
}

/*print them */
for(a =0; a <5; a++)
{
for(b =0; b < 4; b++)
{
/* I don't know what to do with this part.*/

Can someone help me finish it off? I have know idea how to get them to print in the table.
Is there any other problems?

R: you can do the following (and see the comments in the code):

for (a = 0; a < 5; a++)
{ /* outer loop */
for (b = 0; b < 4; b ++)
{ /* iner loop */
printf("%d\t", array[a][b]); /* the \t is just to make a tab to print it like a table - I guess it works fine this way */
} /* end iner loop */
/* again in the outer loop */
printf("\n"); /* print a new line to start a new line.  */
} /* end outer loop */

And one more question:
How do loops inside other loops work?
If I wrote:
for(i =0; i <200; i++)
{
for(x =0; x < 10; x++)
{
/*code*/
}
}
So would the loop with x (the loop inside the first loop) be executed while the first loop is true?
So while the first loop is executing would
it run the whole second loop for one increment of I.
so would the second loop count up to 9 for every increment of i?

R: yap, that's it.

Thanks for any help.

--
Daniel Dalton

http://members.iinet.net.au/~ddalton/
daniel.dalton47@xxxxxxxxx

HTH

Rui Batista

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind