From dd20bf94427392ea0baa428ecf2587d6a8a7a131 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 09:37:36 -0400 Subject: [PATCH] fixed dynamic array --- others/sparse_matrix.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/others/sparse_matrix.cpp b/others/sparse_matrix.cpp index de4759a3e..d9878fda4 100644 --- a/others/sparse_matrix.cpp +++ b/others/sparse_matrix.cpp @@ -13,7 +13,9 @@ int main() { std::cin >> m; std::cin >> n; - int a[m][n]; + int **a = new int *[m]; + for (int i = 0; i < m; i++) a[i] = new int[n]; + std::cout << "Enter matrix elements:"; std::cout << "\n"; @@ -37,4 +39,8 @@ int main() { std::cout << "Sparse matrix"; else std::cout << "Not a sparse matrix"; + + for (int i = 0; i < m; i++) delete[] a[i]; + delete[] a; + return 0; }