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
 
You will need to call the array that you want to sort. I take it you want to sort the three differrent rows of numbers individually?

In that case you will want to call each row individually which can be done with a simple counting for loop as below.

Code:
     int k;
     int lengthOfArray = 5;
     
     for (k=1;k<=3;k++) {
         
         selectionSort(d[k][5],lengthOfArray);
         printElements(d[k][5],lengthOfArray);
                  
     }

Hope this helps, I am no expert :p so may be completely wrong!

Edit: my mistake you will also need to pass the length of the array to the selectionSort and printElements method. You can look at the sizeOf() function to establish this.
 
Last edited:
What line is the error referring to? You are setting 'array' as a pointer by using the * character before it. Unfortunately I am not sure why you would need to do this, due to my lack of experience.
 
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.
 
The methods are expecting a pointer to (i.e. the memory address of) the array. Use the 'addressof' operator (&) to pass in the address of the array:

Code:
   selectionSort(&d[k][5], lengthOfArray);
   printElements(&d[k][5], lengthOfArray);

Whenever you see a '*', read it as 'a pointer to'. Whenever you see a '&' then read it as 'the address of'.

[edit] just noticed something else. You want to use '&d[k][0]' rather than '&d[k][5]' to sort the array from the start.
 
Last edited:
Hmm, the bit that doesn't make sense to me is:

Code:
for(i=0;i<= [b]d[3][5][/b];i++)

It's less than or equal to the array? I believe it should be the length of the array here.

Code:
for(i=0;i<= length;i++)
 
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