Click here to Skip to main content
15,885,904 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi ^_^
we can type
C++
array <int , 10> s = {0};

to declaration a 1D array, now how do I can declare a 2D array using this way ?
on the other hand how do i can declare a dynamic array using this way (I mean what is the way to let user type the length of the array after running the program (1D and 2D array)) ?


many thanks in advance.
Posted
Comments
Andreas Gieriet 5-Oct-15 15:28pm    
Homework assignment? What does your text book tell about this?
Andi
Member 11801336 5-Oct-15 15:39pm    
NOT HOMEWORK !
these are just questions in my mind..
Andreas Gieriet 5-Oct-15 15:41pm    
No shouting please... ;-)
Member 11801336 5-Oct-15 15:46pm    
I'm sorry..
Sergey Alexandrovich Kryukov 5-Oct-15 15:47pm    
What have you tried so far?
—SA

The question is not really clear (see my comment to the question, by the way), but all you need to understand is this: http://www.cplusplus.com/doc/tutorial/arrays[^]. In particular, pay attention for the section "Multidimensional arrays".

Also, it's really useful to start your text file with array data with some metadata which, in particular, describes the dimensions of the array(s). This way, the code can initialize the arrays properly before reading any array data. Failure to design the file structure this way will lead to the need to read the same file twice, which of course would be nothing good.

—SA
 
Share this answer
 
Comments
Maciej Los 5-Oct-15 16:10pm    
Short and to the point!
+5!
Sergey Alexandrovich Kryukov 5-Oct-15 16:47pm    
Thank you, Maciej.
Look at the Andi's comment and solution though, which is better, more adequate to the question.
—SA
Andreas Gieriet 5-Oct-15 16:28pm    
My 5 for the plain-old C-array description.
On the other hand, the OP asks about std::array and not plain-old C-arrays. The memory layout is identical but the initialization is weird for multidimensional std::array. See my solution #2.
Cheers
Andi
Sergey Alexandrovich Kryukov 5-Oct-15 16:45pm    
I agree, thank you for the note. I did not pay attention it was about std::...
—SA
The initialization of a multidimensional std::array is indeed a bit a surprise: you need to add an extra level of braces. E.g.
C++
#include <array>
...
using namespace std;
...
enum { OUTER = 3, INNER = 2 };

array<array<int, INNER>, OUTER> a =  { {{ 1, 2 }, { 3, 4 }, { 5, 6 }} } ;

for(size_t outer = 0; outer < OUTER; ++outer) {
	for(size_t inner = 0; inner < INNER; ++inner) {
		cout << "a[" << outer << "][" << inner << "] = " << a[outer][inner] << endl;
	}
}
Result:
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4
a[2][0] = 5
a[2][1] = 6
You may leave away all but the outer braces and initialize the array as flat sequence of elements.

See also http://stackoverflow.com/questions/17759757/multidimensional-stdarray[^] and the referenced C++14 proposal[^] to fix that "defect".

For dynamic "arrays" you may use nested std::vectors.
Regards
Andi
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 5-Oct-15 16:46pm    
Sure, a 5.
—SA
Andreas Gieriet 6-Oct-15 0:19am    
Thanks for your 5, Sergey!
Cheers
Andi
Maciej Los 5-Oct-15 16:49pm    
5ed!
Andreas Gieriet 6-Oct-15 0:20am    
Thanks for your 5, Maciej!
Cheers
Andi
Member 11801336 6-Oct-15 6:40am    
thank yo so much <3 this my question ^_^, but can I declaration a 2D array (Dynamic) using your code..
Dynamic: I mean user will enter (inner,outer) value...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900