| | |
assign elements to a multi-d array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
c Syntax (Toggle Plain Text)
multi-d[rows][columns] = {{0},{0}};/*Initialize all elements to zero*/
This will initialize the first and second row's first element's to the value of 0.
Since it IS necessary you use a two dimensional array, then the following might give you an idea of how it might work:
C Syntax (Toggle Plain Text)
#define rows 7 #define columns 10 #define max_total 12 #define id 1 void array1( int (*multi_d)[columns]) { int x, y; for (int i=0;i<rows;i++) for (int j=0;j<columns;j++) multi_d[i][j] = 0; srand(time(NULL)*rand()); for (int i=0;i<max_total;i++) { x= rand() % rows; y = rand() % columns; multi_d[x][y] = id; } }
I hope the above helps you out.
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 11:41 am.
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 272
>>why are we defining elements
By that I assume you mean why intialize elements to some default value. Assuming that is correct, the answer is: when you declare an array none of the elements are initialized, just like when you declare a single element. Therefore, if you try to access any given element of an unitialized array to check it's value there is nothing you can check it against. So you initialize all the elements with a default value. Then when you go to enter the actual data you check to see if the value of any given element is the default value or not. If it is the default, then the space is open. It it isn't the default then the space is already occupied. If you have 12 elements assigned to random spots in the array there is always the possibility that you could end up overwriting one value with another if the same x and y values came up which would mean you have less than the desired elements actually occupied.
If you have a large array, then loops are a much easier way to initialize t he array, rather than initializing each element manually.
By that I assume you mean why intialize elements to some default value. Assuming that is correct, the answer is: when you declare an array none of the elements are initialized, just like when you declare a single element. Therefore, if you try to access any given element of an unitialized array to check it's value there is nothing you can check it against. So you initialize all the elements with a default value. Then when you go to enter the actual data you check to see if the value of any given element is the default value or not. If it is the default, then the space is open. It it isn't the default then the space is already occupied. If you have 12 elements assigned to random spots in the array there is always the possibility that you could end up overwriting one value with another if the same x and y values came up which would mean you have less than the desired elements actually occupied.
If you have a large array, then loops are a much easier way to initialize t he array, rather than initializing each element manually.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
Thanks, can I also ask and why is it necessary to use the time function along with rand()? and using LamaBot's method don't I wanna check it the location is equal to zero using something like
C Syntax (Toggle Plain Text)
for (int i=0;i<max_total;i++) { x= rand() % rows; y = rand() % columns; if(multi_d[x][y]=={{0},{0}})//or some form of check// multi_d[x][y] = id; }
Last edited by boujibabe; Mar 5th, 2007 at 5:45 pm.
•
•
•
•
Thanks, can I also ask and why is it necessary to use the time function along with rand()? ...
C Syntax (Toggle Plain Text)
for (int i=0;i<max_total;i++) { x= rand() % rows; y = rand() % columns; if(multi_d[x][y]=={{0},{0}})//or some form of check// multi_d[x][y] = id; }
•
•
•
•
and using LamaBot's method don't I wanna check it the location is equal to zero...
Good luck, LamaBot
•
•
•
•
>>why are we defining elements
By that I assume you mean why intialize elements to some default value. Assuming that is correct, the answer is: when you declare an array none of the elements are initialized, just like when you declare a single element....
In the later case here is the code modified:
C Syntax (Toggle Plain Text)
#define rows 7 #define columns 10 #define max_total 12 #define id 1 void array1( int (*multi_d)[columns]) { int x, y; for (int i=0;i<rows;i++) for (int j=0;j<columns;j++) multi_d[i][j] = 0; srand(time(NULL)*rand()); for (int i=0;i<max_total;i++) { x= rand() % rows; y = rand() % columns; if (mutli_d[x][y] != 0) multi_d[x][y] = id; } }
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 8:18 pm.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
Thanks a bundle one last thing say I had to guess at a location and i guessed at the row and column how would i check that specific row and column to see if it contained the id? Here what i came up with
C Syntax (Toggle Plain Text)
printf("Guess the row location "); scanf(rowLoc) printf("Guess the column location "); scanf(colLoc); for (int i=0;i<row;i++) for (int j=0;j<col;j++) if( multi-d[rowLoc][colLoc]==id) printf ("You found a location");
Last edited by boujibabe; Mar 5th, 2007 at 10:35 pm.
•
•
•
•
I want to use it but i want to know how i should check if the location is free before I add to it as mentioned before this will ensure i don't overwrite anything.
I posted the modified program that does exactly what you want, I believe. Every time it loops it'll only write to the array if the value at x and y is equal to 0, which indicates it hasn't been written too already. Let me know if you'd like a more thorough explaination
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 10:27 pm.
Here is the modified code:
Good luck, LamaBot
C Syntax (Toggle Plain Text)
#define rows 7 #define columns 10 #define max_total 12 #define id 1 void array1( int (*multi_d)[columns]) { int x, y; // Loop through to initialize all array elements to 0 for (int i=0;i<rows;i++) for (int j=0;j<columns;j++) multi_d[i][j] = 0; srand(time(NULL)*rand()); // Seed the random for (int i=0;i<max_total;i++) { // Generate random coordinates x= rand() % rows; y = rand() % columns; // Make sure you don't overwrite a value if (!mutli_d[x][y]) multi_d[x][y] = id; } }
Good luck, LamaBot
LamaBot, please watch your formatting. We are trying to show by example. Poorly formatted examples tells the poster it's OK to be sloppy...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Other Threads in the C Forum
- Previous Thread: Unresolved External Errors
- Next Thread: can anyone help me with writing a magic square program?
| Thread Tools | Search this Thread |
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






