#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to sort a mattrix[2][3] in descending order

Member Posts: 10
I am trying to sort this mattrix in descending order and list to screen.
Please review my coding. The code as shown only shows one number, not the entire set of numbers.

int twoarray::sort(int s[2][3])
{
//sort elements in descending order
int i;
int j;
int temp;

for (i=0; i<1; ++i)

{
for (j=0; i<j; ++j)

{

if (s[i][j] > s [i][j-1])

temp = s[i][j];
s[i][j] = s[i][j-1];
s[i][j-1] = temp;

}
}

cout <<"The mattrix sorted in descending order is:" <<endl;

cout <<s[i][j] <<endl;

return (s[i][j]);
}

int main()

{

twoarray obj;
int i;
int j;
int s[2][3];

//Call functions

obj.sort(obj.s);

for (i=0; i<=1; ++i)

for (j=0; j<=2; ++j)

s[i][j] =0;

return 0;
}//End of this program!!

• Member Posts: 109
: I am trying to sort this mattrix in descending order and list to screen.
: Please review my coding. The code as shown only shows one number, not the entire set of numbers.
:
:
: int twoarray::sort(int s[2][3])
: {
: //sort elements in descending order
: int i;
: int j;
: int temp;
:
:
: for (i=0; i<1; ++i)// this line means that your
// first array dimension has 1 element since its index is 0. But you
//wanted two dimension array right?
// with 1 element since 0 is highest index number.
:
: {
: for (j=0; i<j; ++j)//what do you mean by i<j? 0<0 makes
// no sense
:
: {
:
: if (s[i][j] > s [i][j-1])//since j=0,
//[i] [j-1] is [0] [-1]
:
:
: temp = s[i][j];
: s[i][j] = s[i][j-1];
: s[i][j-1] = temp;
:
:
:
: }
: }
:
: cout <<"The mattrix sorted in descending order is:" <<endl;
:
:
: cout <<s[i][j] <<endl;
:
:
: return (s[i][j]);
: }
:
:
:
:
:
: int main()
:
: {
:
: twoarray obj;
: int i;
: int j;
: int s[2][3];
:
:
: //Call functions
:
:
:
: obj.sort(obj.s);
:
: for (i=0; i<=1; ++i)//I thought you had one element.
:
: for (j=0; j<=2; ++j)
:
: s[i][j] =0;
:
:
: return 0;
: }//End of this program!!
:
Hello,
: I think you need a bigger array, or you either somehow deal with the line I commented on. Look through your code carefully. You may have became mixed up on array arithmetic. I hope this helps.

• Member Posts: 0

__ \ http://forcoder.org \ free video tutorials and ebooks about / C, Objective-C, Perl, Assembly, R, Ruby, JavaScript, MATLAB, Java, C#, Delphi, PL/SQL, Go, Visual Basic .NET, PHP, Visual Basic, C++, Swift, Python, Scratch Rust, SAS, Logo, Dart, Kotlin, ABAP, Bash, Erlang, Awk, Clojure, Julia, LabVIEW, Hack, Alice, Lua, VBScript, Scala, F#, COBOL, FoxPro, Scheme, Transact-SQL, Lisp, Fortran, D, Crystal, Ada, Apex, Prolog, ML / ___________