马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
系统:win10
环境:dev-c++/GCC4.9.2
#include <iostream>
using namespace std;
int main(){
//输入部分
int n;//购票次数
cin>>n;
int *num=new int [n-1];//每次的购票数量
for(int i=0;i<n;i++){
cin>>num[i];
}
int count1=0;
int (*zw)[4]=new int [19][4];//座位表
for(int i=0;i<20;i++){
for(int j=0;j<5;j++){
++count1;
zw[i][j]=count1;
cout<<"zw["<<i<<"]"<<"["<<j<<"]"<<" = "<<zw[i][j]<<endl;
}
}
cout<<"\n\n\n\n\n\n\n";
for(int i=0;i<20;i++){
for(int j=0;j<5;j++){
cout<<"zw["<<i<<"]"<<"["<<j<<"]"<<" = "<<zw[i][j]<<endl;
}
} //数组的值在读取时候发生了变化????
}
本机的运行结果:
zw[0][0] = 1
zw[0][1] = 2
zw[0][2] = 3
zw[0][3] = 4
zw[0][4] = 5
zw[1][0] = 6
zw[1][1] = 7
zw[1][2] = 8
zw[1][3] = 9
zw[1][4] = 10
zw[2][0] = 11
zw[2][1] = 12
zw[2][2] = 13
zw[2][3] = 14
zw[2][4] = 15
zw[3][0] = 16
zw[3][1] = 17
zw[3][2] = 18
zw[3][3] = 19
zw[3][4] = 20
zw[4][0] = 21
zw[4][1] = 22
zw[4][2] = 23
zw[4][3] = 24
zw[4][4] = 25
zw[5][0] = 26
zw[5][1] = 27
zw[5][2] = 28
zw[5][3] = 29
zw[5][4] = 30
zw[6][0] = 31
zw[6][1] = 32
zw[6][2] = 33
zw[6][3] = 34
zw[6][4] = 35
zw[7][0] = 36
zw[7][1] = 37
zw[7][2] = 38
zw[7][3] = 39
zw[7][4] = 40
zw[8][0] = 41
zw[8][1] = 42
zw[8][2] = 43
zw[8][3] = 44
zw[8][4] = 45
zw[9][0] = 46
zw[9][1] = 47
zw[9][2] = 48
zw[9][3] = 49
zw[9][4] = 50
zw[10][0] = 51
zw[10][1] = 52
zw[10][2] = 53
zw[10][3] = 54
zw[10][4] = 55
zw[11][0] = 56
zw[11][1] = 57
zw[11][2] = 58
zw[11][3] = 59
zw[11][4] = 60
zw[12][0] = 61
zw[12][1] = 62
zw[12][2] = 63
zw[12][3] = 64
zw[12][4] = 65
zw[13][0] = 66
zw[13][1] = 67
zw[13][2] = 68
zw[13][3] = 69
zw[13][4] = 70
zw[14][0] = 71
zw[14][1] = 72
zw[14][2] = 73
zw[14][3] = 74
zw[14][4] = 75
zw[15][0] = 76
zw[15][1] = 77
zw[15][2] = 78
zw[15][3] = 79
zw[15][4] = 80
zw[16][0] = 81
zw[16][1] = 82
zw[16][2] = 83
zw[16][3] = 84
zw[16][4] = 85
zw[17][0] = 86
zw[17][1] = 87
zw[17][2] = 88
zw[17][3] = 89
zw[17][4] = 90
zw[18][0] = 91
zw[18][1] = 92
zw[18][2] = 93
zw[18][3] = 94
zw[18][4] = 95
zw[19][0] = 96
zw[19][1] = 97
zw[19][2] = 98
zw[19][3] = 99
zw[19][4] = 100
zw[0][0] = 1
zw[0][1] = 2
zw[0][2] = 3
zw[0][3] = 4
zw[0][4] = 6
zw[1][0] = 6
zw[1][1] = 7
zw[1][2] = 8
zw[1][3] = 9
zw[1][4] = 11
zw[2][0] = 11
zw[2][1] = 12
zw[2][2] = 13
zw[2][3] = 14
zw[2][4] = 16
zw[3][0] = 16
zw[3][1] = 17
zw[3][2] = 18
zw[3][3] = 19
zw[3][4] = 21
zw[4][0] = 21
zw[4][1] = 22
zw[4][2] = 23
zw[4][3] = 24
zw[4][4] = 26
zw[5][0] = 26
zw[5][1] = 27
zw[5][2] = 28
zw[5][3] = 29
zw[5][4] = 31
zw[6][0] = 31
zw[6][1] = 32
zw[6][2] = 33
zw[6][3] = 34
zw[6][4] = 36
zw[7][0] = 36
zw[7][1] = 37
zw[7][2] = 38
zw[7][3] = 39
zw[7][4] = 41
zw[8][0] = 41
zw[8][1] = 42
zw[8][2] = 43
zw[8][3] = 44
zw[8][4] = 46
zw[9][0] = 46
zw[9][1] = 47
zw[9][2] = 48
zw[9][3] = 49
zw[9][4] = 51
zw[10][0] = 51
zw[10][1] = 52
zw[10][2] = 53
zw[10][3] = 54
zw[10][4] = 56
zw[11][0] = 56
zw[11][1] = 57
zw[11][2] = 58
zw[11][3] = 59
zw[11][4] = 61
zw[12][0] = 61
zw[12][1] = 62
zw[12][2] = 63
zw[12][3] = 64
zw[12][4] = 66
zw[13][0] = 66
zw[13][1] = 67
zw[13][2] = 68
zw[13][3] = 69
zw[13][4] = 71
zw[14][0] = 71
zw[14][1] = 72
zw[14][2] = 73
zw[14][3] = 74
zw[14][4] = 76
zw[15][0] = 76
zw[15][1] = 77
zw[15][2] = 78
zw[15][3] = 79
zw[15][4] = 81
zw[16][0] = 81
zw[16][1] = 82
zw[16][2] = 83
zw[16][3] = 84
zw[16][4] = 86
zw[17][0] = 86
zw[17][1] = 87
zw[17][2] = 88
zw[17][3] = 89
zw[17][4] = 91
zw[18][0] = 91
zw[18][1] = 92
zw[18][2] = 93
zw[18][3] = 94
zw[18][4] = 96
zw[19][0] = 96
zw[19][1] = 97
zw[19][2] = 98
zw[19][3] = 99
zw[19][4] = 100
在该结果中,第一列是赋值时的输出,第二次是循环输出整个数组,但是所有zw[4]的值在循环输出的时候都出现了问题,例如前面应为12345678910,在循环输出的时候变成了1234 6 678910,为什么所有下标为4的值会在输出的时候发生变化?
新手求教!
int (*zw)[4]=new int [19][4];//座位表
这一句在后面数组访问中出现行下标19和列下标4的时候越界,访问到了下一个元素, 如zw[0][4]这个数组元素并不存在,访问的是zw[1][0],而zw[1][0]访问的还是zw[1][0],也就重复了。
数组在内存中还是线性的,可以看下C语言的二维数组,传送门--> http://bbs.fishc.com/forum.php?m ... peid%26typeid%3D584
因此,按100个座位,应该是
int (*zw)[5]=new int [20][5];//座位表
表示20行5列,行下标是从0-19(共20个),列下标0-4(共5个)
|