Question: C++ PROGRAMMING Please address the error expression must have a constant value for the bolded line of code outlined below. ------------------------------------------------------------------------------------------------------------------------------------- #include #include #include using

C++ PROGRAMMING

Please address the error "expression must have a constant value" for the bolded line of code outlined below.

-------------------------------------------------------------------------------------------------------------------------------------

#include

#include

#include

using namespace std;

//Initialize_array should fill the array with random numbers in the range of 1-100.

void initialize_array(int array[], int count)

{

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

array[i] = rand() % 100 + 1;

}

//Prints the array.

void print_array(int array[], int count)

{

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

{

cout << array[i] << "\t";

if ((i + 1) % 10 == 0)

cout << endl;

}

cout << endl;

}

//Sorts the array, and stores the result in a new array.

int * sort_array(int array[], int count)

{

int *newArray = new int[count];

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

*(newArray + i) = *(array + i);

for (int i = 0; i < count - 1; i++)

for (int j = 0; j < count - i - 1; j++)

if (newArray[j] > newArray[j + 1])

{

int temp = newArray[j];

newArray[j] = newArray[j + 1];

newArray[j + 1] = temp;

}

return newArray;

}

int main()

{

int count =0;

//Start by prompting the user for the amount of numbers you want to work with.

cout << "Enter the count of numbers you want to work with: ";

cin >> count;

//Dynamically allocate an array of that size.

int array[count]; <---------THIS IS CAUSING ERROR: "EXPRESSION MUST HAVE CONSTANT VALUE"

//Fill the array by calling the function initialize_array.

srand(time(NULL));

initialize_array(array, count);

//Display the array

print_array(array, count);

//Call a function that creates a NEW array by sorting the current one.

int* newArray = sort_array(array, count);

//Display the contents of both arrays after the function call, with appropriate labeling.

cout << "The initial array is:" << endl;

print_array(array, count);

cout << endl << "The newly sorted array is:" << endl;

print_array(newArray, count);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!