1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
using namespace std;
void ins_b_b4_a(double *arr, int b, int a);
void swap(double *arr, int b, int a);
void print(double *arr, int n);
int main ()
{
double arr[] = {4, 6, 3, 8, 9, 2, 7, 3, 5, 1};
print(arr, 10);
ins_b_b4_a(arr, 6, 3);
print(arr, 10);
cout << "\n";
return 0;
}
void ins_b_b4_a(double *arr, int a, int b)
{
double temp = arr[b];
memcpy(arr + a + 1, arr + a, sizeof(double) * (b-a));
arr[a] = temp;
}
void swap(double *arr, int b, int a)
{
double temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void print(double *arr, int n)
{
cout << "\n" << arr[0];
for (int i=1; i<n; i++) cout << ", " << arr[i];
}
|