#include <iostream>
#include “sort.h”
#include “file.h”
#include “array.h”
#include “myself.h”
using namespace std;
int main() {
// Start test sorting.
testSort(1);
testSort(2);
testSort(3);
testSort(4);
// Finish test sorting.
cout << endl << “————————————————” << endl;
// Start test file.
if (testFile() == 0) {
cout << endl << “File error!”;
}
else {
cout << endl << “The file is read and wrote successfully!” << endl;
}
// Finish test file.
cout << endl << “————————————————” << endl;
// Start test array.
if (testArray() == 0) {
cout << endl;
cout << endl << “Array error!”;
}
else {
cout << endl;
cout << endl << “Array successfully!”;
}
// Finish test array.
cout << endl << “————————————————” << endl;
// Start test myself.
if (testMyself() == 0) {
cout << endl << “Error myself!”;
}
else {
cout << endl << “Myself successfully!” << endl << endl;
}
// Finish test myself.
return 0;
}
array.h
#ifndef _ARRAY
#define _ARRAY
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
void printArr(const int arr[], int size) {
cout << endl << “Array: “;
for (int i = 0; i < size; i++) {
cout << arr[i];
if (i != size – 1) cout << “, “;
}
}
int compare_a(const void * x1, const void * x2) {
return ( *(int*)x1 – *(int*)x2 );
}
int getMedianOf(const int arr[], unsigned int size) {
int *NewArray = new int[size];
int median;
for (unsigned int i = 0; i < size; i++) {
NewArray[i] = arr[i];
}
qsort(NewArray, size, sizeof(int), compare_a);
median = NewArray[(size / 2) – 1];
delete []NewArray;
return median;
}
double getMeanOf(const int arr[], unsigned int size) {
return arr[(size / 2) – 1];
}
double getSDOf(const int arr[], unsigned int size) {
double average = 0, dispersion = 0;
int *NewArray = new int[size];
for (unsigned int i = 0; i < size; i++) {
average += arr[i];
}
average /= size;
for (unsigned int i = 0; i < size; i++) {
NewArray[i] = arr[i] – average;
dispersion += pow(NewArray[i], 2);
}
dispersion /= size;
delete []NewArray;
return pow(dispersion, 1 / 2);
}
long long getSumOf(const int arr[], unsigned int size) {
long long sum = 0;
for (unsigned int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int isArrSorted(const int arr[], unsigned int size) {
int *NewArray = new int[size];
bool flag = false;
for (unsigned int i = 0; i < size; i++) {
NewArray[i] = arr[i];
}
qsort(NewArray, size, sizeof(int), compare_a);
for (unsigned int i = 0; i < size; i++) {
if (arr[i] == NewArray[i]) {
}
else {
flag = true;
}
}
if (!flag) {
delete []NewArray;
return 1;
}
else {
flag = false;
}
int j = size – 1;
for (unsigned int i = 0; i < size; i++) {
if (arr[j] == NewArray[i]) {
j–;
}
else {
flag = true;
}
}
if (!flag) {
delete []NewArray;
return -1;
}
delete []NewArray;
return 0;
}
int isArrSame(const int arr1[], const int arr2[], unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
void fillWithRandom(int data[], unsigned int size, int min, int max) {
for (unsigned int i = 0; i < size; i++ ) {
srand( time( NULL ) + i ); // Dynamic random.
data [i] = rand() % min + (max – min);
}
}
void fillWithVal(int data[], unsigned int size, int value) {
for (unsigned int i = 0; i < size; i++) {
data[i] = value;
}
}
int testArray() {
int arr[10] = { 2, 5, 1, -4, 2, 0, 5, 1, 9, 4 };
int arr1[10] = { 1, 6, 0, -3, 3, 1, 4, 2, 8, 6 };
int arr2[10] = { 1, 6, 0, -3, 3, 1, 4, 2, 8, 6 };
printArr(arr, 10);
cout << endl << “Median = ” << getMedianOf(arr, 10);
cout << endl << “Mean = ” << getMeanOf(arr, 10);
cout << endl << “Standart Deviation = ” << getSDOf(arr, 10);
cout << endl << “Sum = ” << getSumOf(arr, 10);
if (isArrSorted(arr, 10) == 1) {
cout << endl << “Sorted in ascending order!”;
}
else if (isArrSorted(arr, 10) == -1) {
cout << endl << “Sorted in descending order!”;
}
else {
cout << endl << “Array is not sorted!”;
}
if (isArrSame(arr1, arr2, 10) == 1) {
cout << endl << “Arrays are the same!”;
}
else {
cout << endl << “Arrays not are the same!”;
}
fillWithRandom(arr, 10, 200, 400);
for (int i = 0; i < 10; i++) {
if (arr[i] < 200 || arr[i] > 400) {
return 0;
}
}
printArr(arr, 10);
fillWithVal(arr, 10, -9);
for (int i = 0; i < 10; i++) {
if (arr[i] != -9) {
return 0;
}
}
printArr(arr, 10);
return 1;
}
#endif
file.h
#ifndef _FILE
#define _FILE
#include <iostream>
#include <fstream>
using namespace std;
int readFromFile(int *data, unsigned int size, const char *filename) {
ifstream outFile(filename);
char buff[10];
size = 0;
while (outFile >> buff) {
data[size] = atoi(buff);
size++;
}
outFile.close();
return size;
}
int writeToFile(const int *data, unsigned int size, const char *filename) {
ofstream inFile;
inFile.open(filename);
unsigned int size_t = 0;
while (inFile << data[size_t] && size != size_t + 1) {
inFile << endl;
size_t++;
}
inFile.close();
return size_t;
}
int testFile() {
int data[100];
int size = 0;
int data2[9] = {43,7,-1,35,2,9,0,321,6};
for (int i = 0; i < 100; i++) {
data[i] = 0;
}
size = readFromFile(data, size, “file.txt”);
if (size == 0) {
return 0;
}
for (int i = 0; i < size; i++) {
if (data[i] != data2[i]) {
return 0;
}
}
size = writeToFile(data, size, “file.txt”);
if (size == 0) {
return 0;
}
return 1;
}
#endif
myself.h
#ifndef _MYSELF
#define _MYSELF
#include <iostream>
#include <cmath>
using namespace std;
int function1(const int arr[], unsigned int size) {
int Even(0);
for (unsigned int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
Even++;
}
}
return Even;
}
int function2(const int arr1[], const int arr2[], unsigned int size) {
bool flag = false;
int point = -1;
for (unsigned int i = 0; i < size; i++) {
if (arr1[i] != arr2[i]) {
flag = true;
}
else {
point = i;
}
}
if (flag && point == -1) {
return 0;
}
else {
if (flag && point != -1) {
return 1;
}
else {
return 2;
}
}
}
void function3(int arr[], unsigned int size, int value1, int value2) {
for (unsigned int i = 0; i < size; i++) {
if (value1 == arr[i]) {
arr[i] = value2;
}
}
}
int testMyself() {
int arr[10] = {2,5,1,-4,2,0,5,1,9,4};
int arr2[10] = {2,5,1,-4,1,0,5,1,9,4};
bool flag = false;
int Even = function1(arr,10);
if (Even != 5) {
return 0;
}
if (function2(arr,arr2,10) != 1) {
return 0;
}
function3(arr,10,1,-9);
for (int i = 0; i < 10; i++) {
if (arr[i] == -9) {
flag = true;
}
}
if (!flag) {
return 0;
}
return 1;
}
#endif
sort.h
#ifndef _SORT
#define _SORT
#include <iostream>
#define SHORT_SIZE_ARRAY 6
#define MEDIUM_SIZE_ARRAY 10
#define LONG_SIZE_ARRAY 17
using namespace std;
void quickSort(int arr[], int first, int last) {
int i = first, j = last;
int tmp, x = arr[first];
do {
while (arr[i] < x) i++;
while (arr[j] > x) j–;
if (i < j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
if(arr[i] == arr[j] && i != j) {
i++;
j–;
}
}
else {
i++;
j–;
}
} while (i < j);
if (i < last) quickSort(arr, i, last);
if (first < j) quickSort(arr, first, j);
}
void shellSort(int arr[], int size) {
int d = size / 2;
while (d >= 1) {
for (int j = 0; j <= size – d – 1; j++)
if (arr[j] > arr[j]) {
int a = arr[j];
arr[j] = arr[j+d];
arr[j+d] = a;
}
d = d / 2;
}
for (int i = 1; i < size; i++) {
for (int j = 0; j <= i – 1; j++)
if (arr[i] < arr[j]) {
int a = arr[j];
arr[j] = arr[i];
arr[i] = a;
}
}
}
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size; i++) {
for (int j = size – 1; j > i; j–) {
if (arr[j] < arr[j – 1]) {
int tmp = arr[j];
arr[j] = arr[j – 1];
arr[j – 1] = tmp;
}
}
}
}
void selectionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
for (int j = 0; j <= i – 1; j++) {
if (arr[i] < arr[j]) {
int a = arr[j];
arr[j] = arr[i];
arr[i] = a;
}
}
}
}
int testSort(int check) {
int short_origin[SHORT_SIZE_ARRAY] = { 9, -3, 3, 8, 2, 1 };
int medium_origin[MEDIUM_SIZE_ARRAY] = { 9, 3, -1, 0, 2, -9, 4, 5, 1, 7 };
int long_origin[LONG_SIZE_ARRAY] = { 3, -1, 5, -8, 2, 9, 1, 10, 0, -2, -5, 7, 2, -3, 1, 8, 11 };
cout << endl << “Before sorting:” << endl;
cout << “Short array: “;
for (int n = 0; n < SHORT_SIZE_ARRAY; n++)
printf (“%d “, short_origin[n]);
cout << endl;
cout << “Medium array: “;
for (int n = 0; n < MEDIUM_SIZE_ARRAY; n++)
printf (“%d “, medium_origin[n]);
cout << endl;
cout << “Long array: “;
for (int n = 0; n < LONG_SIZE_ARRAY; n++)
printf (“%d “, long_origin[n]);
cout << endl << endl;
int result = 0;
switch (check) {
case 1: {
quickSort(short_origin, 0, 5);
quickSort(medium_origin, 0, 9);
quickSort(long_origin, 0, 16);
cout << “After Quick sort:”;
cout << endl << “Short array: “;
for (int n = 0; n < SHORT_SIZE_ARRAY; n++)
printf (“%d “, short_origin[n]);
cout << endl << “Medium array: “;
for (int n = 0; n < MEDIUM_SIZE_ARRAY; n++)
printf (“%d “, medium_origin[n]);
cout << endl << “Long array: “;
for (int n = 0; n < LONG_SIZE_ARRAY; n++)
printf (“%d “, long_origin[n]);
cout << endl;
result = 1;
break;
}
case 2: {
shellSort(short_origin, SHORT_SIZE_ARRAY);
shellSort(medium_origin, MEDIUM_SIZE_ARRAY);
shellSort(long_origin, LONG_SIZE_ARRAY);
cout << “After Shell sort:”;
cout << endl << “Short array: “;
for (int n = 0; n < SHORT_SIZE_ARRAY; n++)
printf (“%d “, short_origin[n]);
cout << endl << “Medium array: “;
for (int n = 0; n < MEDIUM_SIZE_ARRAY; n++)
printf (“%d “, medium_origin[n]);
cout << endl << “Long array: “;
for (int n = 0; n < LONG_SIZE_ARRAY; n++)
printf (“%d “, long_origin[n]);
cout << endl;
result = 1;
break;
}
case 3: {
bubbleSort(short_origin, SHORT_SIZE_ARRAY);
bubbleSort(medium_origin, MEDIUM_SIZE_ARRAY);
bubbleSort(long_origin, LONG_SIZE_ARRAY);
cout << “After Bubble sort:”;
cout << endl << “Short array: “;
for (int n = 0; n < SHORT_SIZE_ARRAY; n++)
printf (“%d “, short_origin[n]);
cout << endl << “Medium array: “;
for (int n = 0; n < MEDIUM_SIZE_ARRAY; n++)
printf (“%d “, medium_origin[n]);
cout << endl << “Long array: “;
for (int n = 0; n < LONG_SIZE_ARRAY; n++)
printf (“%d “, long_origin[n]);
cout << endl;
result = 1;
break;
}
case 4: {
selectionSort(short_origin, SHORT_SIZE_ARRAY);
selectionSort(medium_origin, MEDIUM_SIZE_ARRAY);
selectionSort(long_origin, LONG_SIZE_ARRAY);
cout << “After Selection sort:”;
cout << endl << “Short array: “;
for (int n = 0; n < SHORT_SIZE_ARRAY; n++)
printf (“%d “, short_origin[n]);
cout << endl << “Medium array: “;
for (int n = 0; n < MEDIUM_SIZE_ARRAY; n++)
printf (“%d “, medium_origin[n]);
cout << endl << “Long array: “;
for (int n = 0; n < LONG_SIZE_ARRAY; n++)
printf (“%d “, long_origin[n]);
cout << endl;
result = 1;
break;
}
}
return result;
}
#endif
file.txt
43
7
-1
35
2
9
0
321
6