From 29ee27dbab4b40aa39b7f25dfb055ff326d268bf Mon Sep 17 00:00:00 2001 From: ZennDev1337 Date: Mon, 5 Feb 2024 12:07:16 +0100 Subject: [PATCH] update --- src/io.h | 33 ++++++++++++++++++++++++++-- src/main.cpp | 61 ++++------------------------------------------------ 2 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/io.h b/src/io.h index e5ae628..16eba01 100644 --- a/src/io.h +++ b/src/io.h @@ -1,8 +1,37 @@ #pragma once - +#include // for std::cout void add_two_numbers(); int read_number(); void write_answer(int x); void convert_char_to_ascii(); void print_type_size_bytes(); -void bubble_sort(int *a); \ No newline at end of file + +template +void bubble_sort(T *a) +{ + for (int i = 0; i < sizeof(&a) / sizeof(a); i++) + { + for (int j = 0; j < 7 - 1 - i; j++) + { + if (a[j] > a[j + 1]) + { + T temp{a[j]}; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +} +template +void bubble_sort_print(int size, T *a) +{ + std::cout << "unsorted Array: "; + for (int i = 0; i < size; i++) + std::cout << a[i] << ", "; + std::cout << "\n"; + bubble_sort(a); + std::cout << "sorted Array: "; + for (int i = 0; i < size; i++) + std::cout << a[i] << ", "; + std::cout << "\n"; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 73e863b..92dd2f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,69 +1,16 @@ #include // for std::cout #include "io.h" -#define ARRAY_SIZE 7 - -template -void BubbleSort_T(T *a) -{ - for (size_t i = 0; i < 7; i++) - { - for (size_t j = 0; j < 7 - 1 - i; j++) - { - if (a[j] > a[j + 1]) - { - T temp{a[j]}; - a[j] = a[j + 1]; - a[j + 1] = temp; - } - } - } -} - -void bubble_sort_demo() -{ - int demo[ARRAY_SIZE]{11, 14, 3, 18, 8, 17, 43}; - for (int i = 0; i < 7; i++) - std::cout << demo[i] << ", "; - std::cout << "\n"; - bubble_sort(demo); - for (int i = 0; i < ARRAY_SIZE; i++) - std::cout << demo[i] << ", "; -} - -void bubble_sort_T_demo() -{ - int demo[ARRAY_SIZE]{11, 14, 3, 18, 8, 17, 43}; - double demo2[ARRAY_SIZE]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3}; - std::cout << "Demo: "; - for (int i = 0; i < 7; i++) - std::cout << demo[i] << ", "; - std::cout << "\n"; - std::cout << "Demo2: "; - - for (int i = 0; i < 7; i++) - std::cout << demo2[i] << ", "; - std::cout << "\n"; - - BubbleSort_T(demo); - BubbleSort_T(demo2); - std::cout << "Demo: "; - for (int i = 0; i < 7; i++) - std::cout << demo[i] << ", "; - std::cout << "\n"; - std::cout << "Demo2: "; - - for (int i = 0; i < 7; i++) - std::cout << demo2[i] << ", "; - std::cout << "\n"; -} int main() { + int demo[]{11, 14, 3, 18, 8, 17, 43, 6, 5, 4}; + double demo2[]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3}; // add_two_numbers(); // convert_char_to_ascii(); print_type_size_bytes(); - bubble_sort_T_demo(); + bubble_sort_print(sizeof(demo) / sizeof(demo[0]), demo); + bubble_sort_print(sizeof(demo2) / sizeof(demo2[0]), demo2); return 0; }