/* * Playing around with matrices in C. * * Remember that with the include , this needs to find * the math library libm, and so needs to be compiled with the "-lm" flag. * On cs.marlboro.edu, compile and run it with * * \$ cc -march=pentium4 -O3 -lm matrix.c -o matrix * \$ ./matrix * * * x[n] means *(x+n) which is "de-reference pointer at location x + n * &x[0] means same as x # * * Feb 24 2005, Jim M in class */ #include #include #define NSIZE 10; void print_matrix(int*, int); void print_matrix2D( int m[NSIZE][NSIZE] ); int main(){ int MAX = 100; int x[MAX]; /* allocate space for x[0], x[1], ..., x[99] integers */ int N = sqrt(MAX); /* size of 2 dimensional grid */ int y[NSIZE][NSIZE]; /* a two dimensional matrix ! */ int row = 3; /* row, column index of matrix */ int col = 1; int i,j; /* loop counter */ for ( i = 0; i < MAX; i++ ){ x[i] = 0; } for ( i = 0; i < 10; i++ ){ for ( j = 0; j < 10; j++ ){ y[i][j] = 0; } } x[0] = 42; /* a number we can remember */ x[1] = 50; x[2] = 51; x[3] = 101; /* some other random number */ x[4] = 103; y[1][3] = 42; printf(" y[1][3] = '%i' \n", y[1][3]); print_matrix2D(y); /* NO: printf(" y[13] = '%i' \n", y[13]); */ /* NO: printf(" *(y+13) = '%i' \n", *(y+13)); */ /* NO: print_matrix(y, 100); */ printf(" size of int is %i \n", sizeof(int)); printf(" \n"); printf(" x is %p\n", x); printf(" x[0] is %i\n", x[0]); printf(" *x is %i \n", *x); printf("\n"); printf(" &x[1] is %p \n", &x[1]); printf(" x[1] is %i \n", x[1]); printf(" *(x+1) is %i \n", *(x+1) ); //printf(" x[3] = %i \n", *(x+3*sizeof(int)) ); // printf(" Here's the matrix after we set it to zero : \n"); // print_matrix(x, MAX); x[row*N + col] = 6; // printf(" Here's the matrix after we set 3,1 to 6 : \n"); // print_matrix(x, MAX); return 0; } // // prints a matrix which has been declared as a two dimensional thingy. // void print_matrix2D( int m[NSIZE][NSIZE]){ int row, col; for (row = 0; row < NSIZE; row++){ for (col = 0; col < NSIZE; col++){ printf(" %i ", m[row][col]); } printf("\n"); } } // // prints a matrix which was declared as a one dimensional thingy. // void print_matrix( int m[], int size){ int N = sqrt(size); int row, col; for (row = 0; row