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

  • SEARCH
  • Home
  • 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 8905063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:17:49+00:00 2026-06-15T02:17:49+00:00

I am making a class of matrix and trying to display by creating a

  • 0

I am making a class of matrix and trying to display by creating a new 2D matrix.When i compile and run this code,the program just exits and nothing appears on the screen.
I can’t understand the problem.Any help would be appreciated.thanks

#include <iostream>
using namespace std;

//making a class of matrix
class Matrix{
private:
    int **array;
    int row,col;
public:
    void initialize(int r,int c);  //function to initialize the array
    //dynamically
    Matrix(int r,int c);           //this function initializes a matrix of r
    //rows and c columns
    ~Matrix();                    //delete the matrix by this
    void display_matrix();         //display matrix
    int get_rows();                //get rows of matrix
    int get_columns();             //get columns of matrix
};

//function to initialize the matrix
void Matrix::initialize(int r,int c)
{
    r=row;
    c=col;

    array=new int*[r];         //creating a 1D array dynamically
    for (int i=0;i<r;i++)
    {
        array[i]=new int[c];      //making the array 2D by adding columns to it
    }

    for (int i=0;i<r;i++)
    {
        for (int j=0;j<c;j++)
        {
            array[i][j]=0;        //setting all elements of array to null
        }
    }
}

//initializing NULL matrix
Matrix::Matrix()
{
    initialize(0,0);
}

//setting row aand columns in matrix
Matrix::Matrix(int r,int c)
{
    initialize(r,c);   //function used to initialize the array
}

//deleting matrix
Matrix::~Matrix()
{
    for (int i=0;i<row;i++)
    {
        delete[]array[i];
    }
    delete[]array;
}

int Matrix::get_rows()
{
    return row;        //return no. of rows
}

int Matrix::get_columns()
{
    return col;      //return columns
}

//display function
void Matrix::display_matrix()
{
    int c=get_columns();
    int r=get_rows();
    for (int i=0;i<r;i++)
    {
        for (int j=0;j<c;j++)
        {
            cout<<array[i][j]<<" ";      //double loop to display all the elements of
            //array
        }
        cout<<endl;
    }
}

int main()
{
    Matrix *array2D=new Matrix(11,10);   //making a new 2D matrix
    array2D->display_matrix();   //displaying it
    system("PAUSE");
    return 0;
}
  • 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-06-15T02:17:51+00:00Added an answer on June 15, 2026 at 2:17 am

    Your bug is in initialize:

    void Matrix::initialize(int r,int c)
    {
        r=row;
        c=col;
    
        ...
    

    You are setting the function variables ‘r’ and ‘c’ to whatever values the class variables ‘row’ and ‘col’ have. I’m pretty sure you meant to do the opposite.

    Also are you sure this is the actual code you compile? Your class is missing a declaration for Matrix()

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

Sidebar

Related Questions

I was making a simple class for a small project and decided to just
I have been making a matrix class (as a learning exercise) and I have
I'm making a very dumb mistake just wrapping a pointer to some new'ed memory
This is making me feel dumb, but I am trying to produce a single
I find that making a class non-copyable helps me a lot with my code
I followed this tutorial and I'm currently trying to create a class to draw
I am very new to Scala. I want to implement a generic matrix class
I just making a class thats supposed to be a simple Do you want
I am making a class Customer that has the following data members and properties:
Here is my problem. I have created a pretty heavy readonly class making many

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.