impliment the selection sort algorithm to sort the elements in a two dimentional array of order 3*4 in descending order. the program should prompt the user to input 12 element, sort and display the elements in the matrix format.

What have you gotten done so far? After that, what is the specific problem you are having? :)

``````clrscr();
int matrix[3][4];
int rowindex, colindex;
cout<<"\n input 12 elements:";
for(rowindex=0; rowindex<=3; rowindex++)
{
for (colindex=0; colindex<=2; colindex++)
{
cin>>matrix[rowindex][colindex];
}
}
cout<<"\n elements are \n:";
for (rowindex=0; rowindex<=3; rowindex++)
{
for(colindex=0; colindex<=2; colindex++)
{
cout<<matrix[rowindex][colindex]<<"\t";
}
cout<<"\n";
}
cout<<"\n display the selection sort \n:";
``````

i did this much i dont know the selection sort part and how to decend the order

``````#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int matrix[3][4];
int rowindex, colindex;
cout<<"\n input 12 elements:";
for(rowindex=0; rowindex<=3; rowindex++)
{
for (colindex=0; colindex<=2; colindex++)
{
cin>>matrix[rowindex][colindex];
}
}
cout<<"\n elements are \n:";
for (rowindex=0; rowindex<=3; rowindex++)
{
for(colindex=0; colindex<=2; colindex++)
{
cout<<matrix[rowindex][colindex]<<"\t";
}
}
}
cout<<"\n";
``````

For this program the selection sort part i didnt do i tried doing it but it was not displaying the correct format which i want

2-dimension sort

1. for each member of first element, sort second.
2. sort each member of first element, and maintain order of second.

Enjoy!

And BTW, you have your rows (first element) and columns (second element) reversed! Your 3x4 array should be 3 rows of 4 columns, not the other way around... Think of it as a 2 dimensional array of x and y, where x is the row, and y is the column. You CAN do it the other way around, but it gets messier when you go beyond 2 dimensions and add a z (3rd) dimension... :-)

Getting back to the math - in reality it really doesn't matter, but your matrix[rowindex][colindex] is reversed from what you think it should be. Eyes crossing yet?

i did my best. ok i will change it but can't u check my program code and correct it? please. I did what i know.

``````#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int array[4][3];
int rindex,cindex;
int index_of_smallest,temp,position;
cout<<"input 12 elements:";
for(rindex=0;rindex<4;rindex++)
{
for(cindex=0;cindex<3;cindex++)
{
cin>>array[rindex][cindex];
}
}
cout<<"\n elements are\n:";
for(rindex=0;rindex<4;rindex++)
{
for(cindex=0;cindex<3;cindex++)
{
cout<<array[rindex][cindex]<<"\t\t";
}
cout<<"\n";
for(position=;position>=1;position--)
{
index_of_smallest=0;
for(int index=1;index<=position;index++)
{
if(array[index]<array[index_of_smallest]);
index_of_smallest=index;
}
temp=array[index_of_smallest];
array[index_of_smallest]=array[position];
array[position]=temp;
}
cout<<"\n after sorting";
for(index=0;index<=11;index++);
cout<<array[index]<<"   ";
getch();
}
``````

this program is not working i tried putting the selection sort algorithm in my program but it shows error

The most important thing is to learn how to indent your code to make it easier to read. Some of the most respected responders on the site won't look at your code if yout dont' do it.

Then, it doesn't look like you have initialized the variable called position on line 25.

i have initializing code in the begining for position in line 8

it has 4 to 5 errors due to which it is not working.

i have initializing code in the begining for position in line 8

But in line 25 you need to assign it a value in order for the for loop to work

Also your If statement in 30 shouldn't have a semi-colon.

Your using a keyword to identify a variable, try renaming 'array' to 'myarray'.

'array' is 2d but you're trying to assign a value to temp using the index of only 1 dimension in line 33. In fact you seem to have forgotten about the second dimension all together after line 25.

Also if I'm not mistaken, the reason it's called 'insertion sort' is, that you try to insert each element in it's proper place as it's added to the collection.

By using a 2d array you're complicating it quite a bit. The values in (1,0),(1,1), and (1,2) don't really have any relation to the values in (0,0),(0,1), and (0,2), unless you specify the relationship between them, which adds an extra layer of complexity to your code.

can't you correct the mistakes. PLEASE! Its a request. as you said i have to show my effort then u will be able to correct the program.

``````#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int myarray[4][3];
int rindex,cindex;
int index_of_smallest,temp,position;
cout<<"input 12 elements:";
for(rindex=0;rindex<4;rindex++)
{
for(cindex=0;cindex<3;cindex++)
{
cin>>myarray[rindex][cindex];
}
}
cout<<"\n elements are\n:";
for(rindex=0;rindex<4;rindex++)
{
for(cindex=0;cindex<3;cindex++)
{
cout<<myarray[rindex][cindex]<<"\t\t";
}
cout<<"\n";
for(position=0;position>=1;position--)
{
index_of_smallest=0;
for(int index=1;index<=position;index++)
{
if(myarray[index]<myarray[index_of_smallest])
index_of_smallest=index;
}
temp=myarray[index_of_smallest];
myarray[index_of_smallest]=myarray[position];
myarray[position]=temp;
}
cout<<"\n after sorting";
for(int index=0;index<=11;index++);
cout<<myarray[index]<<"   ";
getch();
}
}
``````

Now it is saying that i have error in line 33 34 and 35. i don't understand the error

Here's some code that will work. The biggest problem is working with a multidimension array, because by default they're not sequential, but your sort needs to treat them like they are. When you look at the code you might want to make note of a couple of things.

See how using indents makes it much easier to read. I did this on Visual C++ Express, which has a menu option to format the code like this. The same can de done in Code::Blocks, both of which are free.

Using the namespace std, gives you access to the standard librairies for C++ and makes it easier for someone to help you since they don't need your specific set up to run the code. You should specifically stay away from `conio.h`, that header is not part of a standard set up and in general is no longer used.

You can notice as well, the use of sub routines to compartmentalize the code. This makes debugging much easier.

All that being said, there are probably other opinions on different ways to do this, but this does work, and it is quite simple. Hope all this helps.

``````#include <iostream>
using namespace std;
int myarray[4][3];
const int Row = 4;
const int Col = 3;
int arraymin[2];
void Display()
{
for(int i=0;i<Row;i++)
{
for(int j = 0; j <Col;j++)
{
cout << myarray[i][j] << "\t";
}
cout << "\n";
}
cout << "\n\n";
}
void FindMin(int rstart, int cstart)
{
arraymin[0] = rstart;
arraymin[1] = cstart;
for (int i=rstart; i< Row;i++)
{
for(int j = 0;j< Col; j++)
{
if(((j >= cstart) && (i == rstart)) || (i>rstart))
{
if (myarray[i][j] <= myarray[arraymin[0]][arraymin[1]])
{
arraymin[0] = i;
arraymin[1] = j;
}
}
}
}
}
void Swap(int rstart, int cstart, int rend, int cend)
{
int temp = myarray[rend][cend];
myarray[rend][cend] = myarray[rstart][cstart];
myarray[rstart][cstart] = temp;
}
void main()
{
//Default values for testing
myarray[0][0] = 56;
myarray[0][1] = 65;
myarray[0][2] = 897;
myarray[1][0] = 84;
myarray[1][1] = 23;
myarray[1][2] = 10;
myarray[2][0] = 91;
myarray[2][1] = 74;
myarray[2][2] = 82;
myarray[3][0] = 29;
myarray[3][1] = 19;
myarray[3][2] = 43;
//cout<<"input 12 elements:";
//for(rindex=0;rindex<4;rindex++)
//{
//  for(cindex = 0; cindex <3;cindex ++)
//  {
//      cin>>myarray[rindex][cindex];
//  }
//}
cout<<"The input:\n";
Display();
for(int i=0;i<Row;i++)
{
for(int j=0;j<Col;j++)
{
FindMin(i,j);
Swap(arraymin[0],arraymin[1],i,j);
}
}
cout << "The sorted input:\n";
Display();
cout << "Press Enter to exit.";
cin.get();
}
``````

thank you so much :)