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 8850115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:49:29+00:00 2026-06-14T12:49:29+00:00

I have to create a class Histogram and make operations on this class. The

  • 0

I have to create a class Histogram and make operations on this class. The input can be one dimensional array or a two dimensional array. The problem appears when i convert the array into a matrix. This what i have tried so far. The error is <Unable to read memory>

histrogram.h

#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include<iostream>
class Histogram
{
private:
    int** matrix;
    int lines;
    void SortMatrix();
public:
    Histogram(){ }
    Histogram(int elements[], int elementsNr);
    Histogram(int** m, int l);
    void Print();
};
#endif

historgram.cpp

#include"histogram.h"
using namespace std;
Histogram::Histogram(int** m, int l)
{
    matrix=m;
    lines=l;
    SortMatrix();
}

Histogram::Histogram(int elements[], int elementsNr)
{
    lines=0;
    //initialize matrix : elementrNr lines and 2 columns
    int** matrix=new int*[elementsNr];
    for(int i=0;i<elementsNr;i++)
    {
        matrix[i]=new int[2];
        matrix[i][0]=INT_MIN;
        matrix[i][1]=INT_MIN;
    }
    //search each element from the array in the matrix
    bool found=false;
    for(int i=0;i<elementsNr;i++)
    {
        found=false;
        for(int j=0;j<elementsNr;j++)
        {
            //the element was found in the matrix ( on the first column )
            if(matrix[j][0] == elements[i])
            {
                matrix[j][1]++;
                found=true;
                break;
            }
        }
        if(!found)
        {
            matrix[lines][0]=elements[i];
            matrix[lines][1]=1;
            lines++;
        }
    }
    SortMatrix();
}
void Histogram::SortMatrix()
{
    bool flag=true;
    int temp;
    for(int i=0;(i<lines) && flag;i++)
    {
        flag=false;
        if(matrix[i+1][0]>matrix[i][0])
        {
            temp=matrix[i][0];
            matrix[i][0]=matrix[i+1][0];
            matrix[i+1][0]=temp;
            flag=true;
        }
    }
}
void Histogram::Print()
{

    for(int i=0;i<lines;i++)
    {
        cout<<matrix[i][0]<<" : " <<matrix[i][1]<<endl;
    }

}

main.cpp

#include"histogram.h"
#include<iostream>
using namespace std;

int main()
{
    int arr[]={6,7,3,1,3,2,4,4,7,5,1,1,5,6,6,4,5};
    Histogram h(arr,17);
    h.Print();
}
  • 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-14T12:49:30+00:00Added an answer on June 14, 2026 at 12:49 pm

    Here

    int** matrix=new int*[elementsNr];
    

    replace with

    matrix=new int*[elementsNr];
    

    becausematrix is already a member variable. You are creating a new temporary variable double pointer named matrix and allocating memory to it rather than your member variable matrix

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

Sidebar

Related Questions

Hi have a problem with singleton Class in iPhone app. I have create a
I have create a class DatabaseInterface which will be used for Sqlite operations. DatabaseInterface.onCreate()
I have create a Java class extending LinearLayout as shown below public class CustomLinear
I have create a Java class extending LinearLayout as shown below public class News
I have create my own NSOpenGLView class, right now the data that i want
I have been curious about dynamically create class at runtime in C# and stumbled
I intended to create a class which only have static members and static functions.
I have a UIView with UITableView component and I want to create a class
I am trying to create a class that implements the IUnknown interface. I have
I have a question regarding dependency injection. say i want to create a class

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.