Sunteți pe pagina 1din 2

HEADER.

H
#ifndef NUMBER_H
#define NUMBER_H
const int SIZE = 6;
template<class T>
class Number{
private:
T num[SIZE];
public:
Number(T[]);
~Number();
void bubbleSort();
void display();
};
#endif
SOURCE.CPP
#include "Header.h"
#include <iostream>
using namespace std;
template<class T>
Number<T>::Number(T array[6]){
for (int i = 0; i < SIZE; i++)
num[i] = array[i];
}
template<class T>
Number<T>::~Number(){}
template<class T>
void Number<T>::bubbleSort(){
bool swap = 1;
T temp;
for (int j = 1; j < SIZE && swap; j++)
{
cout << "Pass " << j << endl;
swap = 0;
for (int k = 0; k < (SIZE - 1); k++)
{
if (num[k]>num[k + 1])
{
temp = num[k];
num[k] = num[k + 1];
num[k + 1] = temp;
swap = 1;
}
display();
cout << endl;
}
cout << endl;
}
}
template<class T>
void Number<T>::display(){

for (int i = 0; i < SIZE; i++)


cout << num[i]<<" ";
}
MAIN.CPP
#include <iostream>
#include "Header.h"
#include "Source1.cpp"
using namespace std;
void main()
{
double initialArray[] = { 23.1, 78.1, 45.1, 8.1, 32.1, 56.1 };
Number<double> digit(initialArray);
digit.bubbleSort();
cout << "The sorted list is: ";
digit.display();
}
*****************************************************************************
Pass 1
23.1 78.1 45.1 8.1 32.1 56.1
23.1 45.1 78.1 8.1 32.1 56.1
23.1 45.1 8.1 78.1 32.1 56.1
23.1 45.1 8.1 32.1 78.1 56.1
23.1 45.1 8.1 32.1 56.1 78.1
Pass
23.1
23.1
23.1
23.1
23.1

2
45.1 8.1
8.1 45.1
8.1 32.1
8.1 32.1
8.1 32.1

32.1
32.1
45.1
45.1
45.1

56.1
56.1
56.1
56.1
56.1

78.1
78.1
78.1
78.1
78.1

Pass 3
8.1 23.1
8.1 23.1
8.1 23.1
8.1 23.1
8.1 23.1

32.1
32.1
32.1
32.1
32.1

45.1
45.1
45.1
45.1
45.1

56.1
56.1
56.1
56.1
56.1

78.1
78.1
78.1
78.1
78.1

Pass 4
8.1 23.1
8.1 23.1
8.1 23.1
8.1 23.1
8.1 23.1

32.1
32.1
32.1
32.1
32.1

45.1
45.1
45.1
45.1
45.1

56.1
56.1
56.1
56.1
56.1

78.1
78.1
78.1
78.1
78.1

The sorted list is: 8.1 23.1 32.1 45.1 56.1 78.1 Press any key to continue . . .

S-ar putea să vă placă și