assign elements to a multi-d array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #41
Mar 9th, 2007
That works! But is there a way I can ensure that the 20 locations always get filled? It seems to only get around 18
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #42
Mar 9th, 2007
Originally Posted by boujibabe View Post
  1. int countLoc(const int array2[][col], int row_size, int col_size )
  2. {
  3. int count = 0; //Stores the number of locations found
  4.  
  5. for (int i=0;i<row_size;i++)
  6. for (int j=0;j<col_size;j++)
  7. if(array2[i][j] ==id)
  8. {
  9. count++;
  10. }
  11.  
  12. return count;
  13. }
When you cout the locations, try the following to see if it works then:
  1. for (int i=0;i<=row_size;i++) {
  2. for (int j=0;j<=col_size;i++) {....

This might be the reason you can only see around 18 and not an exact 20. If you sometimes, randomly, get 20 locations but never less than 18 locations, this is probably the problem.

Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #43
Mar 10th, 2007
nope,now I'm seeing 17 and sometimes 20
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #44
Mar 10th, 2007
Originally Posted by boujibabe View Post
nope,now I'm seeing 17 and sometimes 20
Ok, keep the code I just said to changed, changed and try this.
  1. void populate(int array[][col], int row_size, int col_size) {
  2. int x, y;
  3. for (int i=0;i<=row_size;i++)
  4. for (int j=0;j<=col_size;j++)
  5. array[i][j] = 0;
  6. srand(time(NULL)*rand());
  7. for (int i=0;i<=max_total;i++) {
  8. x= rand() % row_size;
  9. y = rand() % col_size;
  10. if(array[x][y]!=0)
  11. array[x][y] = id;
  12. }/*endfor*/
  13. }

In the above code, I changed the for loops to have a "<=" that
may fixed the problem. Keep the coutLoc for loop "<=" suggestion
set along with this one, and let me know.

LamaBot.
Last edited by Lazaro Claiborn; Mar 10th, 2007 at 1:58 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: assign elements to a multi-d array

 
0
  #45
Mar 10th, 2007
Jeez! Stop shooting in the dark and think about the problem. First, format the code properly with braces and spaces so it can be read, then:
  1. void populate(int array[][col], int row_size, int col_size)
  2. {
  3. int x, y;
  4. for (int i=0; i <= row_size; i++) // This loop just blew your array
  5. // boundaries. Take it back to <
  6. {
  7. for (int j=0; j <= col_size; j++) // Same here
  8. {
  9. array[i][j] = 0;
  10. }
  11. }
  12. // srand(time(NULL) * rand()); // don't multiply by rand -- does nothing
  13. srand(time(NULL));
  14. for (int i=0; i < max_total; i++)
  15. {
  16. do // start a loop
  17. {
  18. x= rand() % row_size;
  19. y = rand() % col_size;
  20. } while (array[x][y] != 0); // keep looping (get new coordinates)
  21. // if the location is already filled
  22. array[x][y] = id;
  23. }/*endfor*/
  24. }

Originally Posted by LamaBot
In the above code, I changed the for loops to have a "<=" that may fixed the problem. Keep the coutLoc for loop "<=" suggestion set along with this one, and let me know.
Looping 1 extra time is not a solution for data collisions. What if you had 2 collisions before the extra loop? You'll be missing one value. What if you had no collisions before the extra loop? You'd have too many values.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #46
Mar 10th, 2007
Problem solved by Walt's code.

LamaBot
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #47
Mar 11th, 2007
Excellent! That works, thanks everyone 2d arrays are my new best friends
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #48
Mar 12th, 2007
Originally Posted by boujibabe View Post
Excellent! That works, thanks everyone 2d arrays are my new best friends
I hope I don't "burn my bridges" by adding this but here it is. I'd like to say one thing, in reference to Walt's solution code to another. Consider the following which would do the exact same thing as Walt's for loop:
  1. for (int x=0;x<max_value;x++) {
  2. x = rand() % row;
  3. y = rand() % col;
  4. if (array[x][y] == 0)
  5. array[x][y] = id;
  6. else
  7. x--;
  8. }
The probability of a 0 character being encountered lessens each loop. Walt's corresponding code will loop 20 * (20 - the probability factor for each successive loop) as will the above for loop, however, what makes Walt's code better is that it executes less statements when it tries to find an element that is == 0. example; say there is an array of 5 characters like this "[1][1][1][0][1]". Using Walt's code the number of statemetns executed is 3 * 4. The above for loop is 5 * 4 which is obviously more statements. This is just to emphasize on why Walt's code would be bette than the above. I add this because it is helpful to think about this too when trying to make something work.

Best regards, LamaBot
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: assign elements to a multi-d array

 
0
  #49
Mar 12th, 2007
>Consider the following which would do the exact same thing as Walt's for loop
Are you so sure about that? You're using one of the array subscript variables 'x', as a loop index. That's certain death when you start assigning it a random value inside the loop.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: assign elements to a multi-d array

 
0
  #50
Mar 12th, 2007
Well all be.... you're right Joe! Here is the oh so massive change
  1. for (int i=0;i<max_value;i++) {
  2. x = rand() % row;
  3. y = rand() % col;
  4. if (array[x][y] == 0)
  5. array[x][y] = id;
  6. else
  7. i--;
  8. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 10476 | Replies: 49
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC