PDA

View Full Version : آرایه 2 بعدی و اشاره گر



hosseinam1370
دوشنبه 04 اسفند 1393, 23:03 عصر
دوستان من اگه بخام با اشاره گر ، مقدار تک تک آرایه رو نشون بدم ، باید چیکار کنم؟
از چه روشی استفاده کنم؟

int main ()
{
int hossein[2][3] = {{3 , 2 , 6} ,{4,5,9}};
cout <<*hossein<< endl;

system ("pause");

}

مثلا فرض کنید من بخوام با اشاره گر،مقدار 6 یا 5 رو نشون بدم، باید چیو cout کنم؟

با تشکر.

[[[[[[[[[[...]]]]]]]]]]
سه شنبه 05 اسفند 1393, 04:28 صبح
تعدادی رو برات نوشتم. توابع در آخرِ تابع main صدا زده میشن. پس در ابتدا تابع main رو بررسی کن، و در انتها وقتی که متوجه شدی، به پارامترهای توابع توجه کن.




#include <stdio.h> /* for int printf (const char*, ...) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

void function1 (int arr[][9])
{
printf ("%i\n\n", arr[0][7]);
}

void function2 (float arr[][3])
{
printf ("%1.1f\n\n", arr[0][2]);
}

void function3 (char arr[][17+1])
{
printf ("%s\n\n", arr[1]);
}

void function4 (int (*arr)[])
{
printf ("%d\n\n", (*arr)[3]);
}

void function5 (int (*arr)[][9])
{
printf ("%d\n\n", (*arr)[1][2]);
}

void function6 (float (*arr)[][3])
{
printf ("%1.1f\n\n", (*arr)[2]);
}

void function7 (char *arr[])
{
printf ("%s\n\n", arr[2]);
}

int main ()
{
int x = 1, y = 999, t, *ptr[2]; /* Array of 2 pointers */
ptr[0] = &x; /* First pointer points to x */
ptr[1] = &y; /* Second pointer points to y */
t = x, x = y, y = t;
printf ("%d ", *ptr[0]);
printf ("%i\n\n", *ptr[1]);



float f = 16.61, d = 13.32, m, *ptr1[2];
ptr1[0] = &f;
ptr1[1] = &d;
m = f, f = d, d = m;
printf ("%2.2f ", *ptr1[0]);
printf ("%2.2f\n\n", *ptr1[1]);



int arr1[] = {1, 2, 555, 9876}, (*ptr2)[4]; /* Pointer to a 1D array[] */
ptr2 = &arr1;
printf ("%d %d\n\n", arr1[2], (*ptr2)[2]);



int arr2[2][9] = {
{1, 2, 3, 4, 2030, 6, 7, 115, 9}, /* First row and 9 columns */
{9, 8, 777, 6, 1408, 4, 3, 95, 1} /* Second row and 9 columns */

}, (*ptr3)[2][9]; /* Pointer to a 2D array[][] */
ptr3 = &arr2; /* Points to arr2[][] */
printf ("%i %i ", arr2[0][4], (*ptr3)[0][4]); /* First row and [4]th column */
printf ("%i %i\n\n", arr2[1][4], (*ptr3)[1][4]); /* Second row and [4]th column */



float arr3[2][3] = { {1.2, 1.4, 1.7}, {7.1, 4.1, 2.1} }, (*ptr4)[2][3];
ptr4 = &arr3;
printf ("%1.1f %1.1f ", arr3[0][2], (*ptr4)[0][2]);
printf ("%1.1f %1.1f\n\n", arr3[1][1], (*ptr4)[1][1]);



char arr4[4][17+1] = {"aBcdefghijklmnopq", "rStuv", "wX", "yZ"}; /* 17, 5, 2, 2 */
printf ("%s %s %s %s\n", arr4[0], arr4[1], arr4[2], arr4[3]);
printf ("%c %c %c %c\n\n", arr4[0][1], arr4[1][1], arr4[2][1], arr4[3][1]);



char *ptr5[] = {"aBcdefghijklmnopq", "rStuv", "wX", "yZ"};
printf ("%s %s %s %s\n", ptr5[0], ptr5[1], ptr5[2], ptr5[3]);
printf ("%c %c %c %c\n\n", ptr5[0][1], ptr5[1][1], ptr5[2][1], ptr5[3][1]);



function1 (arr2); /* int */
function2 (arr3); /* float */
function3 (arr4); /* char */

function4 (ptr2); /* int* */
function5 (ptr3); /* int* */
function6 (ptr4); /* float* */
function7 (ptr5); /* char* */

exit (EXIT_SUCCESS);
}

[[[[[[[[[[...]]]]]]]]]]
سه شنبه 05 اسفند 1393, 05:19 صبح
چاپ آرایه دو بعدی:

#include <stdio.h> /* for int printf (const char*, ...), int putchar (int) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

int main ()
{
int (*ptr)[2][3], i, j;
int arr[2][3] =
{
{4, 8, 15},
{3, 7, 16}
};

ptr = &arr;

for (i = 0; i < 2; ++i)
{
for (j = 0; j < 3; ++j)
printf ("%d ", (*ptr)[i][j]);

putchar ('\n');
}

exit (EXIT_SUCCESS);
}



چاپ آرایه سه بعدی:



#include <stdio.h> /* for int printf (const char*, ...), int putchar (int) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

int main ()
{
int (*ptr)[2][3][4], i, j, k;
int arr[2][3][4] =
{
{ {1,2,3,4}, {5,6,7,8}, {9,10,11,12} },
{ {13,14,15,16}, {17,18,19,20}, {21,22,23,24} }
};

ptr = &arr;

for (i = 0; i < 2; ++i)
{
for (j = 0; j < 3; ++j)
for (k = 0; k < 4; ++k)
printf ("%d ", (*ptr)[i][j][k]);

putchar ('\n');
}

exit (EXIT_SUCCESS);
}





چاپ آرایه چهار بعدی:



#include <stdio.h> /* for int printf (const char*, ...), int putchar (int) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

int main ()
{
int (*ptr)[2][2][3][4], i, j, k, g;
int arr[2][2][3][4] =
{
{ { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } },
};

ptr = &arr;

for (i = 0; i < 2; ++i)
{
for (g = 0; g < 2; ++g)
for (j = 0; j < 3; ++j)
for (k = 0; k < 4; ++k)
printf ("%d ", (*ptr)[i][g][j][k]);

putchar ('\n');
}

exit (EXIT_SUCCESS);
}



چاپ آرایه پنج بعدی:



#include <stdio.h> /* for int printf (const char*, ...), int putchar (int) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

int main ()
{
int (*ptr)[2][2][2][3][4], i, j, k, g, w;
int arr[2][2][2][3][4] =
{
{
{ { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
},
{
{ { {-1,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
}
};

ptr = &arr;

for (i = 0; i < 2; ++i)
{
for (w = 0; w < 2; ++w)
{
for (g = 0; g < 2; ++g)
for (j = 0; j < 3; ++j)
for (k = 0; k < 4; ++k)
printf ("%d ", (*ptr)[i][w][g][j][k]);
putchar ('\n');
}

putchar ('\n');
}

exit (EXIT_SUCCESS);
}



چاپ آرایه شش بعدی:



#include <stdio.h> /* for int printf (const char*, ...), int putchar (int) */
#include <stdlib.h> /* for void exit (int), EXIT_SUCCESS */

int main ()
{
int (*ptr)[2][2][2][2][3][4], i, j, k, g, w, r;
int arr[2][2][2][2][3][4] =
{
{
{
{ { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
},
{
{ { {-1,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
}
},
{
{
{ { {99,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
},
{
{ { {-99,2,3,4}, {5,6,7,8}, {9,10,11,12} }, { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } },
{ { {25,26,27,28}, {29,30,31,32}, {33,34,35,36} }, { {37,38,39,40}, {41,42,43,44}, {45,46,47,48} } }
}
}
};

ptr = &arr;

for (i = 0; i < 2; ++i)
{
for (r = 0; r < 2; ++r)
for (w = 0; w < 2; ++w)
{
for (g = 0; g < 2; ++g)
for (j = 0; j < 3; ++j)
for (k = 0; k < 4; ++k)
printf ("%d ", (*ptr)[i][r][w][g][j][k]);
putchar ('\n');
}

putchar ('\n');
}

exit (EXIT_SUCCESS);
}