调用数组指针后无任何反应?解决思路
调用数组指针后无任何反应??
大家好,今天学到指针时遇到了一个小小的问题?是关于数组指针的,
刚开始是利用随机数反一个矩阵输出来A(5*5),然后再利用函数实现矩阵转置。
例如:
66 85 36 88 20
98 58 17 9 83
50 92 49 60 34
61 95 98 86 66
53 31 54 62 49
转置后的结果:
66 98 50 61 53
85 58 92 95 31
36 17 49 98 54
88 9 60 86 62
20 83 34 66 49
但程序运行后,输出的结果是一样的!!所以请大家帮帮我看看,程序出了什么问题?
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int i,j;
void A(int (*p)[5])
{
int temp;
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
{
temp=(*(p+i))[j];
(*(p+i))[j]=(*(p+j))[i];
(*(p+j))[i]=temp;
}
}
}
void main()
{
int a[5][5];
srand(time(NULL));
for (i=0;i <5;i++)
for (j=0;j <5;j++)
a[i][j]=rand()%99+1;
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
cout < <a[j][i] < < '\t ';
cout < <endl;
}
cout < < "\n\n\n " < <endl;
A(a);
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
cout < <a[i][j] < < '\t ';
cout < <endl;
}
}
------解决方案--------------------
帅哥,你是不是看花眼了啊?我找了半天,没有发现什么问题
然后我运行你的程序(一个字都没有改),给你看输出:
第一次:
69 85 34 87 39
3 29 79 6 53
2 47 88 2 7
84 51 73 48 57
15 10 73 11 33
69 3 2 84 15
85 29 47 51 10
34 79 88 73 73
87 6 2 48 11
39 53 7 57 33
Press any key to continue
第二次:
69 85 34 87 39
3 29 79 6 53
大家好,今天学到指针时遇到了一个小小的问题?是关于数组指针的,
刚开始是利用随机数反一个矩阵输出来A(5*5),然后再利用函数实现矩阵转置。
例如:
66 85 36 88 20
98 58 17 9 83
50 92 49 60 34
61 95 98 86 66
53 31 54 62 49
转置后的结果:
66 98 50 61 53
85 58 92 95 31
36 17 49 98 54
88 9 60 86 62
20 83 34 66 49
但程序运行后,输出的结果是一样的!!所以请大家帮帮我看看,程序出了什么问题?
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int i,j;
void A(int (*p)[5])
{
int temp;
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
{
temp=(*(p+i))[j];
(*(p+i))[j]=(*(p+j))[i];
(*(p+j))[i]=temp;
}
}
}
void main()
{
int a[5][5];
srand(time(NULL));
for (i=0;i <5;i++)
for (j=0;j <5;j++)
a[i][j]=rand()%99+1;
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
cout < <a[j][i] < < '\t ';
cout < <endl;
}
cout < < "\n\n\n " < <endl;
A(a);
for (i=0;i <5;i++)
{
for (j=0;j <5;j++)
cout < <a[i][j] < < '\t ';
cout < <endl;
}
}
------解决方案--------------------
帅哥,你是不是看花眼了啊?我找了半天,没有发现什么问题
然后我运行你的程序(一个字都没有改),给你看输出:
第一次:
69 85 34 87 39
3 29 79 6 53
2 47 88 2 7
84 51 73 48 57
15 10 73 11 33
69 3 2 84 15
85 29 47 51 10
34 79 88 73 73
87 6 2 48 11
39 53 7 57 33
Press any key to continue
第二次:
69 85 34 87 39
3 29 79 6 53