Currently I'm trying to create a sparse matrix with this declaration:
typedef int Data;
struct Element
{
int i;
int j;
Data value;
};
I'm allowing user to input number of row they want:
int size;
cout << "Enter the number of element: ";
cin >> size;
// Function to construct sparse matrix
constructSparseMatrix(size,3);
My try for constructSparseMatrix function:
void constructSparseMatrix(int row, int col) {
int** arr = new int*[row];
for(int i = 1; i <= row; i++) {
arr[i] = new int[col];
cout << arr[row][col];
}
}
But when I input any number then the application output nothing but stop working. My expected output look something like this if I input 2:
Insert (1 ,2, 3)
Insert (4, 5, 6)
Can someone point me in the correct direction? Maybe this is easy but I'm kinda new to C++ so please bear with my ignorance.
Elementhas coordinates and a data thing. Because it has coordinates already, a 2D-array isn´t necessary at all. You could store all elements in a normal 1Dstd::vectorand search it through if you need to find a specific element. Or, for better performance, a nested 2D vector. But you don´t neednewat all, and if you have trouble with it, just avoid it.for(int i = 1; i <= row; i++)=>for(int i = 0; i < row; i++)std::vector<...>andstd::pair<>?vectorbut only with a normal loop? Can you post an answer?