| | |
assign elements to a multi-d array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
C Syntax (Toggle Plain Text)
int countLoc(const int array2[][col], int row_size, int col_size ) { int count = 0; //Stores the number of locations found for (int i=0;i<row_size;i++) for (int j=0;j<col_size;j++) if(array2[i][j] ==id) { count++; } return count; }
c Syntax (Toggle Plain Text)
for (int i=0;i<=row_size;i++) { 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
Ok, keep the code I just said to changed, changed and try this.
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.
C Syntax (Toggle Plain Text)
void populate(int array[][col], int row_size, int col_size) { int x, y; for (int i=0;i<=row_size;i++) for (int j=0;j<=col_size;j++) array[i][j] = 0; srand(time(NULL)*rand()); for (int i=0;i<=max_total;i++) { x= rand() % row_size; y = rand() % col_size; if(array[x][y]!=0) array[x][y] = id; }/*endfor*/ }
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.
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:
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.
C Syntax (Toggle Plain Text)
void populate(int array[][col], int row_size, int col_size) { int x, y; for (int i=0; i <= row_size; i++) // This loop just blew your array // boundaries. Take it back to < { for (int j=0; j <= col_size; j++) // Same here { array[i][j] = 0; } } // srand(time(NULL) * rand()); // don't multiply by rand -- does nothing srand(time(NULL)); for (int i=0; i < max_total; i++) { do // start a loop { x= rand() % row_size; y = rand() % col_size; } while (array[x][y] != 0); // keep looping (get new coordinates) // if the location is already filled array[x][y] = id; }/*endfor*/ }
•
•
•
•
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.
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
•
•
•
•
Excellent! That works, thanks everyone 2d arrays are my new best friends
c++ Syntax (Toggle Plain Text)
for (int x=0;x<max_value;x++) { x = rand() % row; y = rand() % col; if (array[x][y] == 0) array[x][y] = id; else x--; }
Best regards, LamaBot
>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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
Well all be.... you're right Joe! Here is the oh so massive change 

c++ Syntax (Toggle Plain Text)
for (int i=0;i<max_value;i++) { x = rand() % row; y = rand() % col; if (array[x][y] == 0) array[x][y] = id; else i--; }
![]() |
Other Threads in the C Forum
- Previous Thread: Unresolved External Errors
- Next Thread: can anyone help me with writing a magic square program?
Views: 10476 | Replies: 49
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






