Advertisement
StormWingDelta

Dynamic Arrays

Feb 12th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. //Still haven't gotten to the part to do with increasing and decreasing at will yet so have fun figuring that out. :)
  2.  
  3. //Dynamically created arrays
  4. struct Matrix
  5. {
  6.     int rows;
  7.     int cols;
  8.     double ** Body;
  9.     int ** IntABody;
  10.     bool ** BoolABody;
  11.     void ** VoidABody;
  12.  
  13. };
  14.  
  15. //Matrix function prototypes
  16. Matrix BuildMatrix(int rows, int cols); //this must be used with a decaired structure
  17. void FreeMatrix(Matrix & M);
  18. Matrix InputMatrix(); //this must be used with a decaired structure
  19. void PrintMatrix(const Matrix & M);
  20.  
  21. Matrix MatrixAddition(const Matrix & M1, const Matrix & M2);
  22. Matrix MatrixSubtraction(const Matrix & M1, const Matrix & M2);
  23. Matrix ScalarMultiplication(const Matrix & M, double scalar);
  24.  
  25.  
  26. //Functions defined
  27. Matrix BuildMatrix(int rows, int cols) //this must be used with a decaired structure
  28. {
  29.  
  30.     Matrix M;
  31.     double **f;
  32.  
  33.     int i = 0, j = 0;
  34.     f = new double * [rows];
  35.     //memory is allocated for the first cell of each row
  36.     if(f == NULL)
  37.     {
  38.         printf("Failed to allocate memory. \n");
  39.     }
  40.  
  41.     for(; i < rows; ++i)
  42.     {
  43.         //memory is allocated for each column
  44.         *(f + i) = new double[cols];
  45.         if((f + i) == NULL)
  46.         {
  47.             printf("Failed to allocate memory. \n");
  48.         }
  49.     }
  50.  
  51.     for(i = 0; i < rows; i++)
  52.     {
  53.         for(j = 0; j < cols; j++)
  54.         {
  55.             f[i][j] = 0.0;
  56.         }
  57.     }
  58.  
  59.     M.rows = rows;
  60.     M.cols = cols;
  61.     M.Body = f;
  62.  
  63.     return M;
  64.  
  65. }
  66.  
  67. void FreeMatrix(Matrix & M)
  68. {
  69.     int i;
  70.  
  71.     for(i = 0; i < M.rows; i++)
  72.     {
  73.         delete [] M.Body[i];
  74.     }
  75.     delete [] M.Body;
  76. }
  77.  
  78. Matrix InputMatrix() //this must be used with a decaired structure
  79. {
  80.     int i, j, rows, cols;
  81.     Matrix M;
  82.     printf("Enter the number of rows: ");
  83.     scanf("%d", &rows);
  84.     printf("Enter the number of columns: ");
  85.     scanf("%d", &cols);
  86.  
  87.     M = BuildMatrix(rows, cols);
  88.  
  89.     for(i = 0; i < M.rows; i++)
  90.     {
  91.         for(j = 0; j < M.cols; j++)
  92.         {
  93.             printf("Enter M[%d][%d]: ", i, j);
  94.             scanf("%lf",&M.Body[i][j]);
  95.         }
  96.     }
  97.     return M;
  98. }
  99. void PrintMatrix(const Matrix & M)
  100. {
  101.     int i, j;
  102.  
  103.     for(i = 0; i < M.rows; i++)
  104.     {
  105.         for(j = 0; j < M.cols; j++)
  106.         {
  107.             printf("%lf ", M.Body[i][j]);
  108.         }
  109.         printf("\n");
  110.     }
  111. }
  112.  
  113. Matrix MatrixAddition(const Matrix & M1, const Matrix & M2)
  114. {
  115.     Matrix M;
  116.     int i, j;
  117.  
  118.     M = BuildMatrix(M1.rows, M1.cols);
  119.     for(i = 0; i < M.rows; i++)
  120.     {
  121.         for(j = 0; j < M.cols; j++)
  122.         {
  123.             M.Body[i][j] = M1.Body[i][j] + M2.Body[i][j];
  124.         }
  125.     }
  126.     return M;
  127. }
  128.  
  129. Matrix MatrixSubtraction(const Matrix & M1, const Matrix & M2)
  130. {
  131.     Matrix M;
  132.     int i, j;
  133.  
  134.     M = BuildMatrix(M1.rows, M1.cols);
  135.     for(i = 0; i < M.rows; i++)
  136.     {
  137.         for(j = 0; j < M.cols; j++)
  138.         {
  139.             M.Body[i][j] = M1.Body[i][j] - M2.Body[i][j];
  140.         }
  141.     }
  142.     return M;
  143. }
  144. Matrix ScalarMultiplication(const Matrix & M, double scalar)
  145. {
  146.     Matrix N;
  147.     int i, j;
  148.  
  149.     N = BuildMatrix(M.rows, M.cols);
  150.     for(i = 0; i < M.rows; i++)
  151.     {
  152.         for(j = 0; j < M.cols; j++)
  153.         {
  154.             N.Body[i][j] = scalar * M.Body[i][j];
  155.         }
  156.     }
  157.     return N;
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement