×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

# Contact US

Log In

#### Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# array of random #'s

 Forum Search FAQs Links MVPs

## array of random #'s

(OP)
need a function outside of main to fill a 2-D array with random numbers in the range
of 0 to 99, for example:  rand () % 100.  the prototype declaration is:
void fill (int *rows, int *cols, int ***array);  where all three were declared in main and the values for rows and cols were read in by the user in another function.

### RE: array of random #'s

Im assuming that *rows is a pointer to an integer that contains the number of rows in the array, *cols is a pointer to an integer that contains the number of columns in the array, and that ***array is a pointer to the array which has already been declared and allocated the necessary memory that it needs.

That being the case, it is pretty simple and straightforward:

void fill( int *rows, int *cols, int ***array )
{
int a, b;
for ( a = 0; a < *rows; a++)
{
for ( b = 0; b < *cols; b++)
{
*array[a] = rand() % 100;
}
}
}

If Im assuming too much, let me know... ;)

Regards,
Gerald

### RE: array of random #'s

Why do you need int ***array. You can do with **array for 2-D array. Also , if you have already passed the array which has been allocated sufficient memory . Then the function of gerald will do . But if you need to allocate memory inside the function itself . Then just modify the function as:

void fill( int *rows, int *cols, int **array )
{
array = (int**) malloc(sizeof(int*) * (*rows));
for(int i = 0;i < rows ;i++)
array = (int*) malloc(sizeof(int)* (*cols));

int a, b;

for ( a = 0; a < *rows; a++)
{
for ( b = 0; b < *cols; b++)
{
array[a] = rand() % 100;
}
}
}

Siddhartha Singh
siddhu_singh@hotmail.com
siddhu.freehosting.net

### RE: array of random #'s

Unfortunately, that is not very random. Each invocation will produce the same set of random numbers as rand() will use a seed value of 1 every time. Call srand() before the loop.

ex.

#include <stdlib.h>
#include <time.h>

...

srand((unsigned)time(NULL));

for ( b = 0; b < *cols; b++)
{
*array[a] = rand() % 100;
}

Russ
bobbitts@hotmail.com
http://home.earthlink.net/~bobbitts

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.