An empty initializer is invalid for an array with unspecified bound


Joined
Jul 1, 2020
Messages
1
Reaction score
0
I am working on a program school assignment that requires a function that merges two sorted array-based lists into one sorted array-based list. The function takes two arrays as parameters, each holding a sorted list of integers, merges them into a single sorted list, and returns the result.

So this is my C++ code :

#include <iostream>

using namespace std;

int *merge(int A[], int B[], int lengthOfA, int lengthOfB)

{ // here A is the first array

// B is the second array

// A and B are merged into C array

int *C = new int[lengthOfA + lengthOfB]; // size of C is lengthOfA + lengthOfB

int a = 0, b = 0, c = 0;

// traversing both array

while (a < lengthOfA && b < lengthOfB)

{

if (A[a] < B)

C[c++] = A[a++];

else

C[c++] = B[b++];

}

while (a < lengthOfA)

{

C[c++] = A[a++];

}

while (b < lengthOfB)

{

C[c++] = B[b++];

}

return C;

}

// driver code to test the merge function

int main()

{

// Test case 1 - two empty lists

int A[] = {};

int B[] = {};

int length1 = sizeof(A) / sizeof(A[0]);

int length2 = sizeof(B) / sizeof(B[0]);

int *C = merge(A, B, length1, length2); //call merge function

int lenghtOfC = length1 + length2;

cout << "Array C after merging: " << endl;

for (int i = 0; i < lenghtOfC; i++)

cout << C << " ";

cout << endl;

delete[] C;

// Test case 2 - one empty only

int Z[] = {};

int D[] = {2, 6, 7, 12, 19};

length1 = sizeof(Z) / sizeof(Z[0]);

length2 = sizeof(D) / sizeof(D[0]);

C = merge(Z, D, length1, length2); //call merge function

lenghtOfC = length1 + length2;

cout << "Array C after merging: " << endl;

for (int i = 0; i < lenghtOfC; i++)

cout << C << " ";

cout << endl;

delete[] C;

// Test case 3 - lists with 1 element each

int E[] = {4};

int F[] = {2};

length1 = sizeof(E) / sizeof(E[0]);

length2 = sizeof(F) / sizeof(F[0]);

C = merge(E, F, length1, length2); //call merge function

lenghtOfC = length1 + length2;

cout << "Array C after merging: " << endl;

for (int i = 0; i < lenghtOfC; i++)

cout << C << " ";

cout << endl;

delete[] C;

// Test case 4 - lists with multiple elements

int G[] = {1, 4, 8, 9};

int H[] = {2, 6, 7, 12, 19};

length1 = sizeof(G) / sizeof(G[0]);

length2 = sizeof(H) / sizeof(H[0]);

C = merge(G, H, length1, length2); //call merge function

lenghtOfC = length1 + length2;

cout << "Array C after merging: " << endl;

for (int i = 0; i < lenghtOfC; i++)

cout << C << " ";

delete[] C;

return 0;

}

So the assignment requires to test all cases e.g. two empty lists, one empty only, lists with 1 element each, lists with multiple elements. Which I'm trying to do but Visual Studio complains about an empty initializer is invalid for an array with unspecified bound on int a,b,and z. So how can I get it to compile while I am feeling that I'm required to run it with those being empty.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top