Advertisement
Guest User

double_matrix

a guest
Mar 14th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /*
  2.  * DMatrix.c
  3.  *
  4.  *  Created on: Mar 14, 2013
  5.  *      Author: none
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. void    print_int_array         (int *src, size_t size){
  13.     int i;
  14.  
  15.     for (i = 0; i < size; i++){
  16.         printf("-%i) [%p] -> (%d)   ", i, (src + i), src[i]);
  17.     }
  18.     printf("\n\n");
  19. }
  20.  
  21.  
  22. int main (int argc, char **argv){
  23.     int     **first_matrix = NULL;
  24.     int     ****double_matrix = NULL;
  25.  
  26.     int     *array_to_copy;
  27.  
  28.     int     rows, cols;
  29.     int     i, j;
  30.  
  31.     puts("Enter rows and the cols");
  32.     scanf("%d\n%d", &rows, &cols);
  33.  
  34.     //WHAT IS first_matrix? POINTER TO INT*, SO CAST WILL BE INT**
  35.     first_matrix = (int**)malloc(sizeof(int*) * rows);
  36.  
  37.     //WHAT IS first_matrix[i]? POINTER TO INT, SO CAST WILL BE INT*
  38.     for (i = 0; i < rows; i++){
  39.         first_matrix[i] = (int*) malloc (sizeof(int) * cols);
  40.     }
  41.  
  42.     //OK, IN first_matrix EACH ELEMENT IS OF TYPE INT*, SO MANY POINTERS TO INT (OR MANY INT ARRAYS). I'M GOING TO USE array_to_copy
  43.     //WHICH HAS cols TO COPY IT ON EACH first_matrix[i]
  44.     array_to_copy = (int*) malloc (sizeof(int) * cols);
  45.     for (i = 0; i < cols; i++){
  46.         array_to_copy[i] = i;
  47.     }
  48.  
  49.     //LET'S CROSS EACH first_matrix[i] AND COPY IT'S VALUE FROM array_to_copy
  50.     for (i = 0; i < rows; i++){
  51.         for (j = 0; j < cols; j++){
  52.             first_matrix[i][j] = array_to_copy[j];
  53.         }
  54.     }
  55.  
  56.     //LET'S PRINT EACH first_matrix[i]
  57.     puts("array_to_copy:");
  58.     print_int_array(array_to_copy, cols);
  59.  
  60.     puts("\nfirst_matrix:");
  61.     for (i = 0; i < rows; i++){
  62.         print_int_array(first_matrix[i], cols);
  63.     }
  64.  
  65.  
  66.     for (i = 0; i < rows; i++){
  67.         free(first_matrix[i]);
  68.     }
  69.     free(first_matrix);
  70.     free(array_to_copy);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement