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.readdata(obj.s);
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!!
Comments
: 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.readdata(obj.s);
: 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.