How to declare array with no constant variable?

How to declare an array with no constant variable? like:
cin << a;
int Mass[a];

I want to write a programm that take all numbers in bunch of arrays and delete dublicates. And write it to a new array.

Input:
4 // numbers of array
6 2 26 64 88 96 96 // first number is amount of array
4 8 20 65 86
7 1 4 16 42 58 61 69
1 84

Output:
1 2 4 8 16 20 26 42
58 61 64 65 69 84 86
88 96 96

Here is my code using vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, b, a;
	cin >> n;
	vector<int> pale(101, -1);
	for (int i = 0; i < n; ++i) {
		cin >> b;
		for (int j = 0; j < b; ++j) {
			cin >> a;
			pale[a] = 1;
		}
	}
	for (int i = 0; i < pale.size(); ++i) {
		if (pale[i] != -1) cout << i << " ";
	}
	cin.get();
}
Last edited on
1
2
cin << a;
int* Mass = new int[a];
What's wrong with vector?

You can erase an element from a vector. See:

http://www.cplusplus.com/reference/vector/vector/erase/
That's fine. But in my task I should only use arrays. I just need to find the best possible algorithm.
Last edited on
Hello paleblueeye,

1
2
3
4
5
6
int arrSize{};
int* arrPtr{ nullptr };

std::cin >> arrSize;

arrPtr = new int[arrSize];


http://www.cplusplus.com/reference/new/operator%20new[]/

Your use of int Mass[a]; is not allowed in C++. C++ does not allow a VLA unless that has changed in C++17 or 19.

And if you use "new" do not forget to use "delete" when finished with the array.

http://www.cplusplus.com/reference/new/operator%20delete[]/

Another possibility is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	constexpr size_t MAXSIZE{ 20 };

	int arr[MAXSIZE]{};
	int arrUsed{};

	std::cin >> arrUsed;

	for (size_t index = 0; index < arrUsed; index++)
	{
		std::cin >> arr[index];
	}
}


Hope that helps,

Andy
very simple and efficient way to get rid of duplicates in arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <set>
#include <array>
#include <iostream>

int main()
{
	// a bunch of arrays with a bunch of duplicates
	std::array<int, 7> arr1 = { 0, 0, 1, 2, 3, 3, 6 };
	std::array<int, 5> arr2 = { -44, -3, -22, -3, -22 };
	std::array<int, 4> arr3 = { 1, 6, 6, 6 };

	// grab only uniuques out of all 3 arrays
	std::set<int> uniques(arr1.begin(), arr1.end());
	uniques.insert(arr2.begin(), arr2.end());
	uniques.insert(arr3.begin(), arr3.end());

	// show result
	for (const auto& ref : uniques)
		std::cout << ref << std::endl;

	std::cin.get();
}

if you cannot use anything other than arrays, this problem is annoying.
the double buffer algorithm is inefficient, but very simple.
step 1) put all the numbers from all the arrays into one large array.
step 2) sort this large array.
step 3) copy into a second large array, skipping duplicates (easy to spot when sorted, they are adjacent).

you can have solid performance with a little more complexity in your code:
1) sort all the input arrays
2) merge the inputs into target, by taking the smallest topmost element from the input arrays each time, and only inserting it if it is not equal to the last thing inserted.

these values seem to be small. if you know your data range and its fairly small (even 0 to 1 million) you can do the counting sort to solve it too. Let us know if you want to try that.
Last edited on
Topic archived. No new replies allowed.