Hi everyone,
So I have finally got my 2d array put together and now I need to sort the random numbers that are in the elements. I feel like I am close because I put together a one dimensional array and sorted it with no problems.
My problem is that I am a little unsure how to implement the knowledge I have about selection sort with one dimensional arrays on a two dimensional platform. I want them to sort left to right, top to bottom. So essentially, I need it to sort in the fashion that a nested for loop inserts numbers into the array elements.
I have the concept of using nested for loops, but I am having trouble wrapping my head around working it into the void function.
As usual and per the tradition of this forum, I did my best to get it, but I am getting errors and at this point it feels like I am jamming variables into spots that I am unsure if they even go there.
I would greatly appreciate any help on this issue.
Thank you, and here is my code so far. Techgique.
PS - If needed, Win 7, DevC, i7
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
// Random 2d array 100 numbers sorted by Steve P.
const int amount = 100;
int main()
{
void selectionSort(int arr[][8], int);
int row, col, possible;
int arr[13][8];
for (row=0;row<14;row++)//will go out of bound u have defined arr[13][8]
{
cout<<endl;
for (col=0;col<8;col++)
{
arr[row][col]=600+rand()%199;
if ((row==13)&&(col==3))
{
col=9;//what are u trying to do here
}
}
}
selectionSort (arr, amount);
cout<<"Your sorted array of random numbers ranging from 600 to 799."<<endl;
for (row=0;row<13;row++)
{
cout<<endl;
for (col=0;col<8;col++)
{
cout<<arr[row][col]<<" ";
if ((col+1) % 8 == 0)
cout << endl<<endl;
if ((row==12)&&(col==3))
{
col=9;
row=14;
}
}
}
cout<<endl;
cin.get();
return 0;
}
// something feels out of order here
//and it seems that I am flinging values around more than I should be
void selctionSort( int array [][8], int size)//is this a typo
//Not sure about this 8, but again, only comfortable with 1d arrays so far,
//and no values were needed using the 1d version (for the first number)
{
int startScan, startScan2, minIndex, minIndex2, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex=startScan;
minValue = array[startScan][startScan2];//startScan2 is not initalized
for (int index = startScan+1; index<size; index++)//starts from 1..do u need that to start from 1...then shouldnt it be index<=size..not sure
{
for (int index2 = startScan2+1; index2<size; index2++)
{
if (array[index][index2] < minValue)
{
minValue = array [index][index2];
minIndex = index;
minIndex2 = index2;
}
}
}
array[minIndex][minIndex2] = array[startScan][startScan2];
array[startScan][startScan2] = minValue;
}
}