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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:40:50+00:00 2026-06-14T14:40:50+00:00

I have to sort a matrix by column; as input I have an one-dimensional

  • 0

I have to sort a matrix by column; as input I have an one-dimensional array and O convert it into a matrix:

int arr[] = { 6, 7, 3, 1, 3, 2, 4, 4, 7, 5, 1, 1, 5, 6, 6, 4, 5 };

The first element in the column is the value and the second is the number of apparences. I have to sort them by the value (by the first column). This is what i have tried so far.

histogram.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

histogram.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
    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 temp1;
    int temp2;
    int i=0;
    for(int i=0;(i<lines-1) && flag;i++)
    {
        flag=false;
        if(matrix[i][0]>matrix[i+1][0])
        {
            temp1=matrix[i][0];temp2=matrix[i][1];
            matrix[i][0]=matrix[i+1][0];
            matrix[i][1]=matrix[i+1][1];
            matrix[i+1][0]=temp1;matrix[i+1][1]=temp2;  
            flag=true;
        }   
    }
}
void Histogram::Print()
{

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

}

but the output for this code is:

6:3
7:2
3:2
1:3
2:1
4:3
5:3

instead:

1:3
2:1
3:2
4:3
5:3
6:3
7:2
  • 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-14T14:40:52+00:00Added an answer on June 14, 2026 at 2:40 pm

    Try this:

    for (int i = 0; (i < lines - 1); i++) {
    
        for (int o = 0; (o < lines - 1); o++) {
    
            if ( matrix[ i ][0] < matrix[ o ][0] ) {
    
                temp1 = matrix[ i ][0];
                temp2 = matrix[ i ][1];
    
                matrix[ i ][0] = matrix[ o ][0];
                matrix[ i ][1] = matrix[ o ][1];
    
                matrix[ o ][0] = temp1;
                matrix[ o ][1] = temp2;
    
            }
    
        }
    
    }
    

    You need two loops to sort. I believe you’re not implementing this correctly because the below demo shows that it works.

    Ideone Demo

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

Sidebar

Related Questions

I have a 2D-array that I want to sort based on the second column.
I have a n x 2 matrix of integers. The first column is a
I have this following numpy matrix that I want to sort in ascending order
I have sort of a tricky problem I'm attempting to solve. First of all,
I have to sort out my data by some column, such that some specific
I have a 2-by-3 matrix, and I want to sort it according to the
I have a data frame with 22239 rows & 200 columns. The first column
I have a list of string array as input. The array dimension are static
I have a simple array, sort of like this: 1 2 3 4 5
I have a matrix of data (with row names and column names). I would

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.