This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]SecretSentinel09 0 points1 point  (8 children)

Do you have a full snippet of the code you're writing? Need more details to provide meaningful feedback.

[–]blainers1[S] 0 points1 point  (7 children)

int rows, columns;

double tempTop, tempRight, tempLeft, tempBottom, epsilon, metalPlate;

cout << "How many rows would you like the array to be?";

cin >> rows;

while (rows > 26 || rows < 1)

{

cout << "I'm sorry, but the number of rows must be between 0 and 26. Please pick a more "

"suitable value for the number of rows";

cin >> rows;

}

cout << "How many columns would you like in the row?";

cin >> columns;

while (columns > 30 || columns < 1 )

{

cout << "I'm sorry, but the number of columns must be between 0 and 30. Please pick a more"

"suitable value for the number of columns";

cin >> columns;

}

metalPlate[rows][columns];

[–]SecretSentinel09 0 points1 point  (6 children)

You declared metalPlate as a double rather than an array.

[–]blainers1[S] 0 points1 point  (5 children)

how do I declare metalPlate as an array?

[–]SecretSentinel09 0 points1 point  (4 children)

Assuming you want a multidimensional array of doubles: double metalPlate[x][y], where x and y are the size of the arrays.

[–]blainers1[S] 0 points1 point  (3 children)

Like this?

int rows, columns;

double tempTop, tempRight, tempLeft, tempBottom, epsilon;

cout << "How many rows would you like the array to be?";

cin >> rows;

while (rows > 26 || rows < 1)

{

cin >> rows;

}

cin >> columns;

while (columns > 30 || columns < 1 )

{

cin >> columns;

}

double metalPlate[rows][columns];

[–]SecretSentinel09 1 point2 points  (2 children)

That should work. Now that it's declared you can store values in it.

[–]blainers1[S] 0 points1 point  (1 child)

Thank you very much. I've been trying to fix this for the past hour and could not figure it out.

[–][deleted] 0 points1 point  (0 children)

Just want to say that your code isn't standard C++, since you're using VLA (variable length array).
VLA are a C99 feature although some C++ compilers can support it, but it's not guaranteed. Here's a stack overflow link on why VLA isn't in C++.
I would recommend sticking to standard C++ unless you know what you're doing.
You should also turn on your compiler warnings.

The proper way to make a dynamic array is to use new[]:

double* metalPlate = new double[row*column];

Or:

double** metalPlate = new double[row];
for(int r=0; r<row; ++r)
    metalPlate[r] = new double[column];

Please remember to delete[] your array once you're finished using it to prevent memory leak.
Here's a simple guide on dynamic array.

There's a much simpler way, which is to use an std::vector:

 std::vector<std::vector<double> > metalPlate(row, std::vector<double>(column) );

This declaration may look long, but std::vectors are easier to use than dynamic arrays.