I wanted to know how to convert 1D array to 2D array and 2D array back to 1D array in c programming. Again i wanted to Know how to convert 3D array to 4D array and 4D array back to 3D array. please help me, i have an interview on wednesday. All these programs should be worked in c language
How to convert 1D array to 2D array %26amp; vice versa, To convert 3D to 4D array %26amp; vice versa. Pls send me c progs
Memory is linear so you really only have one address after the other in a one dimensional space. 2D arrays show you a different view of this space. A 2D array of [n][m] elements means you have n "chunks" of m elements one after the other in memory. Addressing element [1][0] of an array of [2][3] means you are addressing the first entry (entry 0) of the second "chunk" (of 3 elements) in memory (chunk 1), or address 3*1 + 0 = 3 from the beginning of the array (remember indexes start at 0).
Here is a program to illustrate this. It prints the addresses of memory locations a[i][j] and b[i*m + j] which are the same.
#include %26lt;stdio.h%26gt;
int main()
{
const int n=10;
const int m=15;
int a[n][m];
int *b = %26amp;a[0][0];
int i = 3;
int j = 4;
printf("%p\n%p\n",
%26amp;a[i][j],
%26amp;b[i*m + j]);
}
Going from 3D to 4D follows a similar approach. I'll let you figure it out.
clematis
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment