2013-01-31 13:18:49 UTC
//This program merges two arrays into and deletes duplicate entries
#include
using namespace std;
void merge_arrays (double[], double[], double *, int);
//Preconditon: Array accepts two full and one empty
//Postcondition: Merges the arrays into the empty one
int main()
{
double *a, *b, *c; // declares three pointer variables of double
int i, size = 0;
a = new double [5]; //declares an array of 5
b = new double [5]; //declares an array of 5
cout << "Enter 5 values for the A array" << endl << endl;
for (i = 0; i < 5; i++) //loops to allow user to input elements
{
cin >> a[i];
size += 1; //increment size by 1
}
cout << endl << "Now enter 5 values for the B array" << endl << endl;
for (i = 0; i < 5; i++) //loops to allow user to input elements
{
cin >> b[i];
size +=1; //increment size by 1
}
c = new double [size]; //declares an array of "size" size
merge_arrays (a, b, c, size); //passes arrays and size to function
cout << endl << "The C Array contains:" << endl << endl;
for (i = 0; i < size; i++) //outputs the new array
{
cout << c[i] << " ";
}
cout << endl;
return 0;
}
void merge_arrays (double a[], double b[], double *c, int size)
{
int i, j;
double key;
for (i = 0; i < 5; i++) //loops for 5 elements to allow A to be input to C
{
c[i] = a[i];
}
for (i = 0; i < 5; i++) //loops for 5 elements to allow B to be input to C
{
c[size-(5-i)] = b[i]; //c[10-(end of last point - position of index)]
}
for (i = 0; i < size; i++) //looks for duplicates in the C array
{
key = c[i];
for (j = i+1; j < size; j++)
{
if (c[j] = key)
{
delete c[j];
}
}
}
}