2Darray.cpp
#include <iostream> int main() { // dimensions int N = 3; int M = 3; // dynamic allocation int** ary = new int*[N]; for(int i = 0; i < N; ++i) ary[i] = new int[M]; // fill for(int i = 0; i < N; ++i) for(int j = 0; j < M; ++j) ary[i][j] = i; // print for(int i = 0; i < N; ++i) for(int j = 0; j < M; ++j) std::cout << ary[i][j] << "\n"; // free for(int i = 0; i < N; ++i) delete [] ary[i]; delete [] ary; return 0; } // OUTPUT 0 0 0 1 1 1 2 2 2
Relevant answer in Stackoverflow.
Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)
How to pass this in a function as a parameter?
Which one?
Thanks buddy for sharing this information with us. Very cool tutorial
http://www.mycodingland.com/
You are welcome!
Thanks a lot yo..this helped me a lot..
Very glad to hear that! 🙂
alert(“xss”)
Hey,
What’s the logic behind pre-increment instead of post-increment in the for loops ?
Thanks!
Good question. Here is your answer: http://stackoverflow.com/questions/4706199/post-increment-and-pre-increment-within-a-for-loop-produce-same-output: Given that i++ needs to remember the old value of i after incrementing, I think ++i may be shorter (on the order of 1-2 instructions).
Excellent example.
Thank you!
niceOne!
Thank you!
Pingback: Computer Science homework Assignment - Custom Elite Writers
How to take input from user
fgets(), scanf(), and so on..
Pingback: Computer Science homework Assignment - Academic Writing Company
Why is the output not in the matrix form?
Because in the double for loop that I print the values of the matrix, I append a newline after each element. I could of course modify the code there to have the output in matrix format.
thnx
HOW CAN WE PASS 2D ARRAY TO THE FUNCTION?
https://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c
Pingback: SEP:| Online Assignment Writing Service