A little help with this code please!!!

Associate
Joined
6 Mar 2009
Posts
495
I am trying to write this C++ code which will sort a 2D array, but not sure what parameters to pass when im calling it in the main. Here is the code.

Code:
#include<iostream>

using namespace std;

int d[3][5]={{1,2,3,4,5},{10,1,2,1,5},{3,1,3,4,2}}; // array to sort

 void selectionSort(int *array,int length)//selection sort function 
{
	int i,j,min,minat;
	for(i=0;i<(length-1);i++)
	{
		minat=i;
		min=array[i];

      for(j=i+1;j<(length);j++) //select the min of the rest of array
	  {
		  if(min>array[j])   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element 
			  min=array[j];
		  }
	  }
	  int temp=array[i] ;
	  array[i]=array[minat];  //swap 
	  array[minat]=temp;
		
	}
}

void printElements(int *array,int length) //print array elements
{
	int i=0;
	for(i=0;i<= d[3][5];i++)
    cout<<array[i]<<endl;
}

void main()
{ 
    selectionSort();                 //call to selection sort  
	printElements();               // print elements 


	system("pause");
	//return 0;
}

Thanks
 
Im getting an error on both selectionSort, and printElements where the d is.

I found most of this code online and not sure what some for it does either.
 
Sorry here is the more updated code. I changed a few bits.

#include<iostream>

using namespace std;

int d[3][5]={{1,2,3,4,5},{10,1,2,1,5},{3,1,3,4,2}}; // array to sort

void selectionSort(int *array,int length)//selection sort function
{
int i,j,min,minat;
for(i=0;i<(length-1);i++)
{
minat=i;
min=array;

for(j=i+1;j<(length);j++) //select the min of the rest of array
{
if(min>array[j]) //ascending order for descending reverse
{
minat=j; //the position of the min element
min=array[j];
}
}
int temp=array ;
array=array[minat]; //swap
array[minat]=temp;

}
}

void printElements(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<=4;i++)
cout<<d<<endl;
}

int main(int *array,int length)
{
int k;
int lenghtOfArray=5;

for (k=1;k<=3;k++) {

selectionSort(&d[k][0],lenghtOfArray);
printElements(&d[k][0],lenghtOfArray);

}


system("pause");
return 0;
}


 
Back
Top Bottom