It is necessary to create 2 matrices of size m, n and vector B. How to make it right through the pointer?

#include<iostream> int main() { int m,n; printf("Enter m "); scanf("%f", m); printf("Enter n "); scanf("%f", n); int *B = new int B[m]; int **A = new int *(*(A + m) + n); int **C = new int *(*(C + m) + n); for (int i = 0; i < m; ++i) for(int j = 0; j < n; j++) { A[i][j] = new int[i][j]; C[i][j] = new int[i][j]; } } 
  • Well, for starters, what do you want to do *(*(A + m) + n) here? Then A[i][j] = new int definitely not the case here. Generally would you write int ** A = (int *)[n]; A[0] = new int[m] int ** A = (int *)[n]; A[0] = new int[m] something like that ... - pavel

2 answers 2

I think you mean the following

 #include <iostream> int main() { size_t m; std::cout << "Enter m: "; std::cin >> m; size_t n; std::cout << "Enter n: "; std::cin >> n; int *B = new int [m]; int **A = new int * [m]; int **C = new int * [m]; for ( size_t i = 0; i < m; i++ ) { A[i] = new int [n]; C[i] = new int [n]; } // some processing of A, B, and C for ( size_t i = 0; i < m; i++ ) { delete [] A[i]; delete [] C[i]; } delete [] C; delete [] A; delete [] B; return 0; } 

That is, you want to dynamically allocate a one-dimensional array, addressed by pointer B of int * type, and arrays of arrays addressed by pointer A and C , of type int ** .

An alternative approach is instead of manually allocating and freeing memory, you could use the standard std::vector container declared in the <vector> header. For example,

 #include <iostream> #include <vector> int main() { size_t m; std::cout << "Enter m: "; std::cin >> m; size_t n; std::cout << "Enter n: "; std::cin >> n; std::vector<int> B( m ); std::vector<std::vector<int>> A( m, std::vector<int>( n ) ); std::vector<std::vector<int>> C( m, std::vector<int>( n ) ); return 0; } 

You can work with vectors as with arrays using the index operator. for example

 #include <iostream> #include <iomanip> #include <vector> int main() { size_t m; std::cout << "Enter m: "; std::cin >> m; size_t n; std::cout << "Enter n: "; std::cin >> n; std::vector<int> B( m ); std::vector<std::vector<int>> A( m, std::vector<int>( n ) ); std::vector<std::vector<int>> C( m, std::vector<int>( n ) ); for ( size_t i = 0; i < B.size(); i++ ) { B[i] = i % 2 == 0 ? i / 2 : m - i / 2 - 1; } for ( size_t i = 0; i < A.size(); i++ ) { for ( size_t j = 0; j < n; j++ ) A[i][j] = i * n + j; } for ( size_t i = 0; i < C.size(); i++ ) { for ( size_t j = 0; j < n; j++ ) C[i][j] = m * n - 1 - i * n - j; } std::cout << '\n'; for ( int x : B ) std::cout << std::setw( 2 ) << x << ' '; std::cout << "\n\n"; for ( const auto &row : A ) { for ( int x : row ) std::cout << std::setw( 2 ) << x << ' '; std::cout << '\n'; } std::cout << '\n'; for ( const auto &row : C ) { for ( int x : row ) std::cout << std::setw( 2 ) << x << ' '; std::cout << '\n'; } std::cout << std::endl; return 0; } 

The output of the program may look like this.

 Enter m: 10 Enter n: 10 0 9 1 8 2 7 3 6 4 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 
     int * B = new int[m]; int ** A = new int*[m]; for(int i = 0; i < m; ++i) A[i] = new int[n]; .... for(int i = 0; i < m; ++i) delete[] A[i]; delete[] A; 

    Like that...

    I think that the teacher is waiting for this from you.