1

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.

6
  • Element has 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 1D std::vectorand search it through if you need to find a specific element. Or, for better performance, a nested 2D vector. But you don´t need new at all, and if you have trouble with it, just avoid it. Commented Jul 21, 2015 at 17:54
  • Why are you making a 2d array if you want it be sparse? Those two ideas are mutually exclusive... Commented Jul 21, 2015 at 17:54
  • ...and another thing: Array indices start at 0. for(int i = 1; i <= row; i++) => for(int i = 0; i < row; i++) Commented Jul 21, 2015 at 17:55
  • Is it acceptable to make use of the STL? Specifically std::vector<...> and std::pair<>? Commented Jul 21, 2015 at 18:00
  • @deviantfan How can I apply your suggestion without using vectorbut only with a normal loop? Can you post an answer? Commented Jul 21, 2015 at 18:33

1 Answer 1

1

The segmentation fault you are receiving is caused by this line:

cout << arr[row][col];

In that context, row and col are the dimensions of the matrix, therefore the elements go from 0 to row - 1 and from 0 to col - 1. When you access arr[row][col] you accessing elements out of bounds.

Not to mention that your for loop also goes out of bounds:

for(int i = 1; i <= row; i++) {

and will start from 1 instead of 0.

A correct function would look more like:

int** constructSparseMatrix(int row, int col) {
    int** arr = new int*[row];
    for(int i = 0; i < row; i++) {
        arr[i] = new int[col];
        for (int k = 0; k < col; k++)
            arr[i][k] = /* initialize element (i, k) */;
    }
    return arr;
}

Live demo

Finally, I'd recommend a good book on C++. It's clear that you are missing a lot of the basics of C++. new and delete are highly advanced topics. I'd recommend starting with std::array, std::vector and std::map instead.


Sparse matrices are often not implemented with an actual matrix (array) of NxM elements; that is used for dense matrices (where most of the elements are different from 0).

A possible implementation is with an std::map<std::pair<int, int>, element>, where you map 2D indeces to a specific element. For other possible implementations look at this page.

An easier alternative it to use Boost.uBLAS which provides various kind of matrices.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way I can achieve the result using a normal loop?
@Pewds What's a normal loop?
Thank you for your answer, if not using new as your suggestion, is there any other way? Normal loop I mention here is like loop inside a loop to populate the value of row and col for later usage without using any advance feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.