I'm trying to learn how to sort arrays. I tried to learn select sort, but I couldn't code it right...
so I tried the easiest, bubble sorting and kind of got the hang of it! But with a few problems and questions.
I'm trying to print an array with random numbers that are assorted. But the numbers even though they are random get stuck at one number after randomly selecting two random numbers. like so:
1239
9999
9999
9999
Theres something wrong or something with the way its sorted thats messing with the way the random numbers are output.
Any help would be appreciated!
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>
using namespace std;
const int ROW = 9;
const int COL = 5;
int array[ROW][COL];
void sortit()// what does the sorting
{
int* p = (int*)array;
int r,c;
for(r = 0; r < ((ROW * COL)-1);r++)
{
for(c = r+1; c < (ROW * COL); c++)
{
if(p[r] > p[c])
{
int hold = p[r];
p[c] = p[c];
p[c] = hold;
}
}
}
}
void reg()//unsorted array
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
array[i][j]= rand()%10;
cout << array[i][j] << "";
}
cout<<endl;
}cout <<"\n\n\n";
}
int main()
{
srand(time(NULL));
// unsorted array
reg();
// array
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
array[i][j]= rand()%10;// random numbers
sortit();//Sorts array
cout << array[i][j] << "";// prints array
}
cout<<endl;
}
return 0;
}