/*
It should be noted that when passing an array to a function,
the array decays into pointers.
That is, only the option of passing an array to a function by a pointer is possible
Passing an array by value and by reference can only be simulated
*/
#include <iostream>
// Simulate input by value
void sortArray1(double arr[10] , int size)
{
for (int i = 0; i < size - 1; ++i)
{
int small = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] < arr[small])
small = j;
}
double temp=arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
// Simulate input by reference
void sortArray2(double arr[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
int small = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] < arr[small])
small = j;
}
double temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
int main()
{
// example
double arr1[10] ={ 1.2, 34, 3.7, 48.6, -5, 9, 14.87, 17,0.89,1 };
sortArray1(arr1, 10);
// output in display
for (int i = 0; i < 10; ++i)
std::cout << arr1[i] << " ";
std::cout << "\n";
double arr2[10] = { 1.2, 34, 3.7, 48.6, -5, 9, 14.87, 17,0.89,1 };
sortArray2(arr2, 10);
// output in display
for (int i = 0; i < 10; ++i)
std::cout << arr2[i] << " ";
return 0;
}
Comments
Leave a comment