Comparing two Arrays

My job is to compare two Arrays (Array A, Array B) and set another Array (Array C) to elements of the other two arrays in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void setBigger(double bigger[], const double a[], const double b[], unsigned els) {
	double big = 0;
	cout << "{";
	for (unsigned pos = 0; pos < els; pos++) {
		double big = 0;
		for (unsigned posA = 0; posA < els; posA++) {
			if (a[posA] > big || b[posA] > big)
				if (a[posA] != big && b[posA] != big)
				big = max(b[posA], a[posA]);
		}
		bigger[pos] = big;
		cout << bigger[pos];
		if (pos >= 0 && pos < els-1)
			cout << ", ";
	}
	cout << "}";
}


This is what I have done so far. My issue is that it is setting all parts of Array C to the largest number rather than excluding that number and moving on. Any suggestions?

a[] is set to { 1, 2, 3, 4, 5, 6, 7 }
b[] is set to {10, 8, 6, 4, 2, 0, -2 }

All help is appreciated.
and set another Array (Array C) to elements of the other two arrays in descending order.


This being C++, there is a lot the language provides. std::copy and std::sort from including algorithm, and std::greater from including functional.

Assuming that the arrays are all ready to go and the right sizes:

1
2
3
 std::copy ( a, a+els, bigger );
 std::copy ( b, b+els, bigger+els );
 std::sort(bigger, bigger+els*2, std::greater<int>());
Last edited on
You could merge the arrays A + B into C and sort C.
Example:
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
37
38
39
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
  const int NUM_ELEMS = 7;
  int a[NUM_ELEMS] = { 1, 2, 3, 4, 5, 6, 7 };
  int b[NUM_ELEMS] = {10, 8, 6, 4, 2, 0, -2 };
  int c[NUM_ELEMS * 2] = {0};
  
  for (int i = 0; i < NUM_ELEMS; i++)
  {
    c[i] = a[i];
  }
  for (int i = NUM_ELEMS, j=0; i < NUM_ELEMS * 2; i++, j++)
  {
    c[i] = b[j];
  } 
  cout <<"\nArray c unsorted: ";
  for (int i = 0; i < NUM_ELEMS * 2; i++)
  {
    cout << c[i] << " ";
  } 
  cout << "\n\n";
  std::sort(c, c + NUM_ELEMS * 2, greater<int>());

  cout <<"\nArray c sorted: ";
  for (int i = 0; i < NUM_ELEMS * 2; i++)
  {
    cout << c[i] << " ";
  } 
  cout << "\n\n";
  system("pause");
  return 0;
}
well it depends if the given array are not sorted then you can use the approach given by Thomas1965 : merge the arrays and sort the merged array ...... complexity : O(n*long(n))

If yes then compare element by element and keep adding elements to the new array that way you will be able the solve the problem in O(n)
First, recognize that if arrays a and b have els elements each, then array bigger will have 2*els elements.

What you're describing is called "merge sort" you solve it recursively.
I should have mentioned that array C also needs to be 7 elements long. This is why I am having so much troubles with this.
array C also needs to be 7 elements long


This is not possible. If A and B have both 7 elements then combinded they will have 14 elements.
Topic archived. No new replies allowed.