-
January 11th, 2014, 09:46 AM
#1
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;
char ans1,ans2,answer;
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.
-
January 11th, 2014, 12:32 PM
#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?
Victor Nijegorodov
-
January 11th, 2014, 12:38 PM
#3
Re: sorting elements of matrix
Define "doesn't run well".
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 11th, 2014, 12:41 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|