Advertisement
DenisBabarykin

Matrix works

Oct 12th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int **allocateMatrix(int nRows, int nCols)
  5. {
  6.     int **matrix = new int*[nRows];
  7.     for (int i = 0; i < nRows; i++)
  8.         matrix[i] = new int[nCols];
  9.     return matrix;
  10. }
  11.  
  12. void inputMatrix(int **matrix, int nRows, int nCols)
  13. {
  14.     for (int i = 0; i < nRows; i++)
  15.         for (int j = 0; j < nCols; j++)
  16.             scanf("%d", &matrix[i][j]); // "%d", not "%d " !!!
  17. }
  18.  
  19. void outputMatrix(int **matrix, int nRows, int nCols)
  20. {
  21.     for (int i = 0; i < nRows; i++)
  22.     {
  23.         for (int j = 0; j < nCols; j++)
  24.             printf("%d ", matrix[i][j]);
  25.         printf("\n");
  26.     }
  27. }
  28.  
  29. void freeMatrix(int **matrix, int nRows)
  30. {
  31.     for (int i = 0; i < nRows; i++)
  32.         delete [] matrix[i];
  33.     delete [] matrix;
  34. }
  35.  
  36. void main()
  37. {
  38.     int nRows = 0, nCols = 0;
  39.     printf("Input number of rows and columns: ");
  40.     scanf("%d%d", &nRows, &nCols);
  41.  
  42.     int **matrix = allocateMatrix(nRows, nCols);
  43.     inputMatrix(matrix, nRows, nCols);
  44.     printf("\n");
  45.     outputMatrix(matrix, nRows, nCols);
  46.     freeMatrix(matrix, nRows);
  47.  
  48.     getch();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement