84 409
Assignments Done
99,1%
Successfully Done
In March 2020

# Answer to Question #53187 in C++ for jemal

Question #53187
Write and test the function void insert(float a[],int& n, float x) This function inserts the item x into the sorted array a of n elements and increments n.The new item is inserted at the location that maintains the sorted order of the array. This requires shifting elements forward to make room for the new x. (Note that this requires the array to have at least n+1 elements allocated.)
1
2015-07-01T06:54:14-0400
Solution.
#include <iostream>
using namespace std;

const int MaxSize = 10;

// Implementation of functions
void insert(float a[], int& n, float x)
{
if (n == 0 || a[n - 1] <= x)
{
a[n] = x;
}
else
{
a[n] = a[n - 1];
int i = n - 1;
insert(a, i, x);
}

if (n < MaxSize)
{
n++;
}
}

void main()
{

float a[MaxSize];
int n = 0;

// Test
float testArray[] = { 20.0, 5.0, 30.0, 50.0, 2.0, 50.5, 25.5, 40.0, 100.0, 80.0, 70.0 };
for (int i = 0; i < 11; i++)
{
insert(a, n, testArray[i]);
}

for (int i = 0; i < n; i++)
{
cout << a[i] << "\n";
}

// To pause
cin.get();
cin.get();

return;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!