1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
#define WIDTH 4
#define HEIGHT 4
using namespace std;
int jimmy [HEIGHT][WIDTH];
int n,m;
void bubblesort (int jimmy[HEIGHT][WIDTH], int length);
int main ()
{
srand ( static_cast<unsigned int>(time(NULL)) ); //initialize time
for (n=0;n<HEIGHT;n++) //set HEIGHT
for (m=0;m<WIDTH;m++) //set WIDTH
{
jimmy[n][m]=(rand( )%(99)); //Initilaize random Matrix
}
//Print Out Random Matrix
cout <<"The Random Generated Matrix \n";
cout <<"|"<< jimmy[0][0]<<" , "<< jimmy[0][1]<<" , "<< jimmy[0][2]<<" , "<< jimmy[0][3]<<"|"<<endl;
cout <<"|"<< jimmy[1][0]<<" , "<< jimmy[1][1]<<" , "<< jimmy[1][2]<<" , "<< jimmy[1][3]<<"|"<<endl;
cout <<"|"<< jimmy[2][0]<<" , "<< jimmy[2][1]<<" , "<< jimmy[2][2]<<" , "<< jimmy[2][3]<<"|"<<endl;
cout <<"|"<< jimmy[3][0]<<" , "<< jimmy[3][1]<<" , "<< jimmy[3][2]<<" , "<< jimmy[3][3]<<"|"<<endl;
bubblesort(jimmy, 4);
cout <<"The Sorted Matrix\n ";
cout <<"|"<< jimmy[0][0]<<" , "<< jimmy[0][1]<<" , "<< jimmy[0][2]<<" , "<< jimmy[0][3]<<"|"<<endl;
cout <<"|"<< jimmy[1][0]<<" , "<< jimmy[1][1]<<" , "<< jimmy[1][2]<<" , "<< jimmy[1][3]<<"|"<<endl;
cout <<"|"<< jimmy[2][0]<<" , "<< jimmy[2][1]<<" , "<< jimmy[2][2]<<" , "<< jimmy[2][3]<<"|"<<endl;
cout <<"|"<< jimmy[3][0]<<" , "<< jimmy[3][1]<<" , "<< jimmy[3][2]<<" , "<< jimmy[3][3]<<"|"<<endl;
system ("pause");
return 0;
}
void bubblesort (int jimmy[HEIGHT][WIDTH], int length)
{
int index;
int iteration;
int temp;
for(iteration = 1; iteration < length; iteration++)
{
for(index = 0; index < length - iteration;index++)
if(jimmy[index][index] > jimmy[index + 1][index + 1])
{
temp = jimmy[index][index];
jimmy[index][index] = jimmy[index + 1][index + 1];
jimmy[index + 1][index + 1] = temp;
}
}
}
|