#array need help why is it not work?

9 messages · Page 1 of 1 (latest)

lone harness
#

#include <iostream>
using namespace std;

int main()
{

int array[num]; // Declare an array of size num
int sum = 0;   // Variable to store the sum

// Ask the user for the number of elements
cout << "Enter the number of elements: ";
cin >> num;


// Input elements
cout << "Enter " << num << " elements: ";
for (int i = 0; i < num; i++) 
{
    cin >> array[i]; // Read elements into array
    sum += array[i]; // Add each element to sum
}

// Output the sum
cout << "Sum of array elements: " << sum << endl;

return 0;

}

#

array need help why is it not work?

fallen fox
#

Is this really written in this order? If it is then you need to define the value of num before you declare the array. Whatever num equals at the time that the array is created is the size that the array will be.

#

Also. This channel is not for cpp help. Thats in the cpp-help channel.

neon rose
fallen fox
#

Yeah. I think in terms of order of operations being time. The time when the array declaration happens is before the time when the variable num gets defined.

deep urchin
#

int* array = new int[num];

dark quest
#

Dynamic array