Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:40:56+00:00 2026-05-20T09:40:56+00:00

I’m trying to write code for an orthogonal linked sparse matrix. Here’s the question:

  • 0

I’m trying to write code for an orthogonal linked sparse matrix.

Here’s the question:
An alternative linked representation for sparse matrices uses nodes that have the fields down, right, row, col, and value. Each non-zero entry of the sparse matrix is represented
by a node. The zero terms are not explicitly stored. The nodes are linked together to form two circular lists. The rst list, the row list, is made up by linking nodes by rows and within rows by columns using the right field. The second,list, the column list, is made up by linking nodes via the down field. In this list, nodes are linked by columns and within columns by rows. These two lists share a common header node. In addition, a node is added to the dimensions of the matrix.

The input file looks like this:

// Matrix A

4 4 7
1 1 2
1 4 1
2 2 7
3 1 9
3 3 8
4 2 4
4 3 5

// Matrix B

4 4 5
1 3 4
2 1 6
2 3 3
3 2 5
4 4 9

This is my code for the operator>>:

istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x){

    in >> x.numRows >> x.numCols >> x.numTerms;
    in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;
    x.push_back(x.currentNode);
    if((x.currentNode->row == 1)&&(x.currentNode->col == 1)){

        x.hnode->right = x.currentNode;
        x.hnode->down = x.currentNode;
    }
    if(x.currentNode->col == 1){
        x.hnode->down = x.currentNode;
    }
    if(x.currentNode->row == 1){
        x.hnode->right = x.currentNode;
    }

    for (int i = 2; i <= x.numTerms; i++) {

        in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;

        x.push_back(x.currentNode);

    }


    return in;

}

It compiles fine. But when I try running it, I keep getting a segmentation fault error.
Can anyone help??
Thanks a bunch!

Here’s OrthogonalLinkedSparseMatrix.h:

#ifndef O_L_SPARSE_MATRIX_H

#define O_L_SPARSE_MATRIX_H



#include <iostream>

#include <fstream>

#include "node.h"

#include "myExceptions.h"



using namespace std;



class OrthogonalLinkedSparseMatrix;

ostream& operator<< (ostream&, OrthogonalLinkedSparseMatrix&);

istream& operator>> (istream&, OrthogonalLinkedSparseMatrix&);



class OrthogonalLinkedSparseMatrix{

public:

    friend ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x);

    friend istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x);

    OrthogonalLinkedSparseMatrix(){}

    ~OrthogonalLinkedSparseMatrix(){}

    void transpose(OrthogonalLinkedSparseMatrix &b);

    void add(OrthogonalLinkedSparseMatrix &a, OrthogonalLinkedSparseMatrix &c);

    void push_back(matrixNode *&mat_Node);

    void setDowns(matrixNode *&mat_Node);

private:

    matrixNode *hnode;

    int numRows, numCols, numTerms;

    matrixNode *currentNode;

    matrixNode *previousNode;

    matrixNode *nextNode;

};

 // code for operator >> & <<, etc goes here, but everything's commented out except operator >>  

#endif

EDIT: I’m including operator<< as well:

    ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x){

    if(x.numTerms == 0){

        out << "No non-zero terms" << endl;

        return out;

    }

    out << x.numRows << x.numCols << x.numTerms << endl;

    for (int i = 0; i < x.numTerms; i++) {

        out << x.currentNode->row << x.currentNode->col << x.currentNode->value << endl;

    }

    return out;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:40:57+00:00Added an answer on May 20, 2026 at 9:40 am

    I think your issue is with currentNode. You shouldn’t need it as a class variable, and instead you should create a new one each time you read input from the stream. For example,

    istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x)
    {
        in >> x.numRows >> x.numCols >> x.numTerms;
    
        int inRow, inCol, inValue;
        in >> inRow >> inCol >> inValue;         // Get the values from input
    
        // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the matrix.
        x.push_back(new matrixNode(inRow, inCol, inValue));
    
        if(x.currentNode->col == 1){
            x.hnode->down = x.currentNode;
        }
        if(x.currentNode->row == 1){
            x.hnode->right = x.currentNode;
        }
    
        for (int i = 2; i <= x.numTerms; i++) 
        {
            in >> inRow >> inCol >> inValue;         // Get the values from input
    
            // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the matrix.
            x.push_back(new matrixNode(inRow, inCol, inValue));
        }
    
        return in;
    }
    

    A few things to note here:

    • Each time push_back() is called, a new matrixNode is pushed on. In your implementation, the same node was always added, and was probably never initialized.
    • I assumed that matrixNode has a constructor that takes 3 arguments. This should be easy to add.

    In general, managing pointers yourself is very dangerous and prone to memory leaks. In this case, its important that you call delete on each pointer when you no longer need it. Most likely you’ll want to do this in your destructor.

    EDIT:
    It also looks like hnode may be being used with an invalid pointer. Make sure you assign/create this matrixNode pointer before using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.