I have an assignment to write a program that does a bunch of crap with matrices in C. One thing I have to do is randomize the contents of a matrix. I want to do this by copying random data from memory into the matrix (yes, I know there are other ways, but I'm not using them). So I have to create a pointer to an int[100][100] array, and then use malloc. I've tried declaring this as int temp[100][100]*, but the compiler gives me an error. I don't get an error if I declare it as int * temp[100][100], but I'm not sure if that's a point to a matrix or a matrix of pointers. Moreover, I have to cast malloc to a pointer to an int[100][100], but the compiler doesn't like temp = (int[100][100]*) malloc(40000), either.
Announcement
Collapse
No announcement yet.
pointers to multidimensional arrays in C
Collapse
X
-
12-17-10 Mohamed Bouazizi NEVER FORGET
Stadtluft Macht Frei
Killing it is the new killing it
Ultima Ratio Regum
-
I'm not laughing at the way you're doing it. I'm laughing at the fact that you have to do it and I don't.Originally posted by Kuciwalker
I might as well find out how to do it now, even if it's an inefficient and more difficult way of doing things
God, I hated learning to program.
When my **** wouldn't compile I used to randomly insert &s and *s until it did.12-17-10 Mohamed Bouazizi NEVER FORGET
Stadtluft Macht Frei
Killing it is the new killing it
Ultima Ratio Regum
Comment
-
...
One thing I have to do is randomize the contents of a matrix.Code:#include <stdio.h> #include <sys/types.h> #include <time.h> int main() { time_t t; int i, j; long arr[100][100]; time(&t); srand48((long) t); // set seed to current time for(i=0; i < 100; i++) for(j=0; j < 100; j++) arr[i][j] = lrand48(); return 0; }"The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
Ben Kenobi: "That means I'm doing something right. "
Comment
-
Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding.12-17-10 Mohamed Bouazizi NEVER FORGET
Stadtluft Macht Frei
Killing it is the new killing it
Ultima Ratio Regum
Comment
-
Are they as simple to do in stupid applications such as this?Originally posted by KrazyHorse
Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding."The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
Ben Kenobi: "That means I'm doing something right. "
Comment
Comment