•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,912 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,662 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 7259 | Replies: 49 | Solved
![]() |
•
•
•
•
I need a function that will populate a multidimensional array with a fixed amount of elements. So that only some of the locations contain a distinguishing number for identification.
If the multi-dim array is of fixed length, then most of the work is done for you. Simply declare a mutli-dimensional array then assign the locations the distinguishing numbers. Although if you're using numbers as identification, you don't necessarily need a multi-dimensional array but I'll give an example of both:
c Syntax (Toggle Plain Text)
#define ELEMENTS 20 #define ARRAY 5 /* MULTI-DIMENSIONAL */ int multi_ID[ARRAY][ELEMENTS]; ... /* REGULAR ARRAY */ int multi_ID[ELEMENTS];
If you have any questions let me know.
Good luck, LamaBot
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Rep Power: 0
Solved Threads: 0
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
#define rows 7
#define columns 10
#define max_total 12
void array1( int multi-d[rows][columns], int rows, int columns)
{
multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
/* Populate array???*/
} Last edited by boujibabe : Mar 4th, 2007 at 10:28 pm.
•
•
•
•
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
#define rows 7 #define columns 10 #define max_total 12 void array1( int multi-d[rows][columns], int rows, int columns) { multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/ /* Populate array???*/ }
Cool! Ok I understand. Thanks for clear that up:
c Syntax (Toggle Plain Text)
#define elements 100 #define ID 10 void array1(int *multi_d) { int position; /* Put initialization code here (i.e. array = all zeros) */ srand(time(NULL)*rand()); position = rand() % elements; multi_d[position] = ID; }
Make sure you include "time.h" and "stdlib.h" header files. Let me know if you have any problems.
Good luck, LamaBot
Last edited by Lazaro Claiborn : Mar 4th, 2007 at 10:46 pm.
•
•
•
•
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
First define your array, kinda like you did:
int multi-d[rows][columns]Then fill it with 0's with
for loops.Then loop from 1 to 12 and use LamaBot's idea with
rand() to get a location in the array. Check if the location is zero. If so, load the loop value. If not, get another location. Age is unimportant -- except in cheese
•
•
•
•
ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.
#define rows 7 #define columns 10 #define max_total 12 void array1( int multi-d[rows][columns], int rows, int columns) { multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/ /* Populate array???*/ }
Is an absolute requirement that you use a multidimensional array of integers? It'd make the program a little easier if you just used a single dimension array, especially when you transverse the array. Just for closure, you don't need to pass the rows and columns as they're declared as global constants.
Good luck, LamaBot
You know, LamaBot, you don't have to quote every post in their entirety if you aren't going to reference any part of it. And you can edit the quote down to only show the relevant portions you wish to comment on. That would help by not having a 200 line quote with a 3 line reply that has little to do with the quote. Just a thought...
Age is unimportant -- except in cheese
•
•
•
•
You know, LamaBot, you don't have to quote every post in their entirety if you aren't going to reference any part of it. And you can edit the quote down to only show the relevant portions you wish to comment on. That would help by not having a 200 line quote with a 3 line reply that has little to do with the quote. Just a thought...
You're right. I'll keep that in mind. Darn my lack of common sense.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Other Threads in the C Forum
- Previous Thread: Unresolved External Errors
- Next Thread: can anyone help me with writing a magic square program?



Linear Mode