CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: sorting elements of matrix

1. Junior Member
Join Date
Nov 2013
Posts
11

## sorting elements of matrix

hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
here's the code
Code:
```#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int main()
{
int i,j,p,q,c1,c2,r1,r2,k,temp=1;
int matrix1[100][100];
int matrix2[100][100];
int sum[100][100];
int multi[100][100];
int min[100][100];
char func[80];
for(i=c1,p=0;i>0,p<r1;i--,p++)
{
for(j=0,q=0;j<c1,q<r1;j++,q++)
if(matrix1[i][j]>matrix1[i][j+1])
{
k=matrix1[i][j];
matrix1[i][j]=matrix1[i][j+1];
matrix1[i][j+1]=k;
}
}
cout<<"\nsorted matrix1 = ";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
cout<<matrix1[i][j]<<"   ";
if(j==r1-1)
cout<<"\n\n";
for(i=c2,p=0;i>0,p<r2;i--,p++)
{
for(j=0,q=0;j<c2,q<r2;j++,q++)
if(matrix2[i][j]>matrix2[i][j+1])
{
k=matrix2[i][j];
matrix2[i][j]=matrix2[i][j+1];
matrix2[i][j+1]=k;
}
}
cout<<"\nsorted matrix2 = ";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
cout<<matrix2[i][j]<<"    ";
if(j==r2-1)
cout<<"\n\n";
}  }
cout<<"\n";
getch ();
return 0;
}```
Last edited by Faraz95; January 11th, 2014 at 10:45 AM.

2. ## Re: sorting elements of matrix

Originally Posted by Faraz95
hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
Just find the bugs and fix them!
Did you try to debug your code?

3. ## Re: sorting elements of matrix

but it doesn't run well
Define "doesn't run well".

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: sorting elements of matrix

Originally Posted by Faraz95
hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
First, your two-dimensional arrays are uninitialized. They contain garbage data, so how are you going to sort garbage data that can change on each run of the program?

Second, assuming that those matrices contain valid data in all the cells, usage of std::sort simplifies this problem:
Code:
```#include <algorithm>
//...
for(size_t i = 0; i < 100; ++i)
std::sort(&matrix1[i][0], &matrix1[i][0] + 100);```
That sorts all of the rows of matrix1 in ascending order.

If this is a homework problem, and you're supposed to write the sort code, then instead of creating large matrices with 100 elements, start out with one with 3,4, or 5 elements. Then write very simple code that attempts to sort the matrix. That is much easier than trying to figure out where a 100 x 100 element matrix fails to sort.

Also, you should be using the debugger to step through the code to see where the sort breaks down if it doesn't work. You should be doing this instead of just writing code, running it, and asking us to debug the code, all without any indication that you've attempted to debug the code yourself (if you did debug the code, where does the sort go wrong and why?).

Regards,

Paul McKenzie

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•