There are two possible ways for the first array whether it contains non repeated elements or not. let's say for simplicity the first array doesn't contain repeated elements, then the idea is very simple track the total as in my following code
#include <iostream>
int main()
{
int arr1[5] = { 1, 2, 3, 4, 5};
int arr2[5] = { 5, 1, 3, 4, 2};
int total(0);
for (unsigned int i(0); i < 5; ++i)
{
for (unsigned int j(0); j < 5; ++j)
{
if ( arr1[i] == arr2[j] )
{
total += 1;
}
}
}
if ( total == 5 )
std::cout << "Same " << std::endl;
else
std::cout << "not Same " << std::endl;
std::cin.get();
return 0;
}
If the total is less than or greater than 5, then the arrays are not same. Now, it seems to me that we need to come up with an algorithm that calculates the proper total, so we need a function that accepts an array and check the total number. For example, in your case the total number should be 9 (1 for 3, 4 for 2, and 4 for 5). This is what came up to my mind. I hope the idea is clear. This is what I did for a general case
#include <iostream>
int getTotal(int arr[], const int size)
{
int *temp = new int[size];
int total(0);
for (int i(0); i < size; ++i)
temp[i] = arr[i];
for (int i(0); i < size; ++i)
{
for (int j(0); j < size; ++j)
{
if ( arr[i] == temp[j] )
total += 1;
}
}
std::cout << total << std::endl;
return total;
}
int main()
{
int arr1[5] = { 3, 5, 2, 5, 2};
int arr2[5] = { 2, 3, 5, 5, 2};
int total = getTotal(arr1, 5);
int track(0);
for (unsigned int i(0); i < 5; ++i)
{
for (unsigned int j(0); j < 5; ++j)
{
if ( arr1[i] == arr2[j] )
{
track += 1;
}
}
}
if ( track == total )
std::cout << "Same " << std::endl;
else
std::cout << "not Same " << std::endl;
std::cin.get();
return 0;
}
Another approach with using one loop. the idea is to get the total of an array and then divide the total by each element to get new total. If the new total of the first array is equal to the new total of the second array then, they are same. (Note: I haven't tested my code for all cases; however, my approach seems sensible to me.
#include <iostream>
double getTotal(double arr[], const int size)
{
double total1(0), total2(0);
for (int i(0); i < 5; ++i)
{
total1 += arr[i];
}
for (int i(0); i < 5; ++i)
{
total2 += total1/arr[i];
}
std::cout << total2 << std::endl;
return total2;
}
int main()
{
double arr1[5] = { 3, 5, 2, 4, 2};
double arr2[5] = { 2, 3, 5, 5, 2};
double total1 = getTotal(arr1, 5);
double total2 = getTotal(arr2, 5);
if ( total1 == total2 )
std::cout << "Same " << std::endl;
else
std::cout << "not Same " << std::endl;
std::cin.get();
return 0;
}