#include <iostream>
template <typename T>
void fill_array(T *array, size_t size = 1)
{
for (T *iter = array; iter != array + size; iter++)
*iter = rand();
}
void print_array(T *array, size_t size, const char *sep = " ")
std::cout << *iter;
if (iter != array + size - 1)
std::cout << sep;
void sort_array(T *array, size_t size)
for (size_t i = 0; i < size; i++)
bool test = true;
for (T *iter = array + 1; iter != array + size; iter++)
if (*(iter - 1) > *iter)
test = false;
std::swap(*(iter - 1), *iter);
if (test)
break;
int main(int argc, char *argv[])
const size_t size = 11;
int arr[size];
fill_array(arr, size);
print_array(arr, size);
sort_array(arr, size);
std::cout << "\n";
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
template <typename T>
void fill_array(T *array, size_t size = 1)
{
for (T *iter = array; iter != array + size; iter++)
{
*iter = rand();
}
}
template <typename T>
void print_array(T *array, size_t size, const char *sep = " ")
{
for (T *iter = array; iter != array + size; iter++)
{
std::cout << *iter;
if (iter != array + size - 1)
std::cout << sep;
}
}
template <typename T>
void sort_array(T *array, size_t size)
{
for (size_t i = 0; i < size; i++)
{
bool test = true;
for (T *iter = array + 1; iter != array + size; iter++)
{
if (*(iter - 1) > *iter)
{
test = false;
std::swap(*(iter - 1), *iter);
}
}
if (test)
break;
}
}
int main(int argc, char *argv[])
{
const size_t size = 11;
int arr[size];
fill_array(arr, size);
print_array(arr, size);
sort_array(arr, size);
std::cout << "\n";
print_array(arr, size);
}