C ++中的数组问题 [英] array problem in c++
问题描述
#include >< iostream.h >
#include < conio.h >
main(){
字符 q;
int y1,y2,p,o = 0 ;
int m = 0 ,d = 0 ,g = 0 ,s = 0 ;
int chart1 [ 10 ] [ 4 ] ;
int chart3 [ 10 ] [ 4 ] ;
int G [ 10 ] [ 4 ] ;
int M [ 10 ] [ 4 ] ;
int S [ 10 ] [ 4 ] ;
int D [ 10 ] [ 4 ] ;
int temp [ 4 ];
for ( int i = 0 ; i< ; 10; ++ i)
for ( int j = 0 ; j< ; 4; ++ j){
G [i] [j] = 0 ;
M [i] [j] = 0 ;
S [i] [j] = 0 ;
D [i] [j] = 0 ;
}
字符 chart2 [ 10 ] [ 23 ] ;
for ( int i = 0 ; i< ; = 9 ; ++ i){
chart1 [i] [ 0 ] =(1 + i);
}
for ( int j = 0 ; j< ; 10; ++ j){
int sum = 0 ;
cout<< " <<<(j + 1)<< " ;
cin>> chart1 [j] [ 1 ];
cout<< " <<(j + 1)<<< ; " ;
cin> q;
如果(q == ' G' )chart1 [j] [ 3 ] = ' G' ;
如果(q == ' M' )chart1 [j] [ 3 ] = ' M' ;
如果(q == ' S' )chart1 [j] [ 3 ] = ' S' ;
如果(q == ' D' )chart1 [j] [ 3 ] = ' D' ;
cout<< " <<(j +1)<< " ;
cout<< " ;
cin> p;
for ( int v = 0 ; v< ; p; ++ v){>
cout<< " <<(v + 1)< < " ;
cin> y1;
cout<< " <<(v + 1)< ;< " ;
cin> y2;
sum =和+(y2-y1);
chart1 [j] [ 2 ] = sum;
}
}
for ( int j = 0 ; j< ; 20; ++ j){
for ( int i = 0 ; i< ; 10; ++ i){
如果(chart1 [i + 1] [ 2 ]> chart1 [i] [ 2 ]){
temp [ 0 ] = chart1 [i] [ 0 ];
temp [ 1 ] = chart1 [i] [ 1 ];
temp [ 2 ] = chart1 [i] [ 2 ];
temp [ 3 ] = chart1 [i] [ 3 ];
// ---------------------
chart1 [i] [ 0 ] = chart1 [i + 1] [ 0 ];
chart1 [i] [ 1 ] = chart1 [i + 1] [ 1 ];
chart1 [i] [ 2 ] = chart1 [i + 1] [ 2 ];
chart1 [i] [ 3 ] = chart1 [i + 1] [ 3 ];
// ---------------------- 跨度>
chart1 [i + 1] [ 0 ] = temp [ 0 ];
chart1 [i + 1] [ 1 ] = temp [ 1 ];
chart1 [i + 1] [ 2 ] = temp [ 2 ];
chart1 [i + 1] [ 3 ] = temp [ 3 ];
// ---------------------
}}}
for ( int i = 0 ; i< ; 10; ++ i){//
cout<< " ; //
for ( int j = 0 ; j< ; 4; ++ j)//
cout<< " <<< chart1 [i] [j]; //
} //
cout<< " ;
for ( int i = 0 ; i< ; 10; ++ i){//
如果(图表1 [i] [ 3 ] == ' D'){// 跨度>
for ( int j = 0 ; j< ; 4; ++ j){//
D [o] [j] = chart1 [i] [j];} //
++ o;}} //
//
for ( int i = 0 ; i< ; 10; ++ i){//
cout<< " ; //
for ( int j = 0 ; j< ; 4; ++ j)//
cout<< " <<< D [i] [j]; //
} //
///
getch();
}
在我添加这部分之前
for ( int i = 0 ; i< 10; ++ i){//
如果(图表1 [i] [ 3 ] == ' D'){// 跨度>
for ( int j = 0 ; j< ; 4; ++ j){//
D [o] [j] = chart1 [i] [j];} //
++ o;}} //
//
for ( int i = 0 ; i< ; 10; ++ i){//
cout<< " ; //
for ( int j = 0 ; j< ; 4; ++ j)//
cout<< " <<< D [i] [j]; //
} //
///
chart1的输出如下所示:
6 1 7 71
5 20 4 83
8 24 4 68
10 80 4 77
7 23 3 68
9 17 3 68
2 8 2 68
3 14 2 83
4 16 2 77
1 9 1 68
但是添加这些代码后,chart1的输出为:
4239560 1638200 4207616 4263780
5 20 4 83
8 24 4 68
10 80 4 77
7 23 3 68
9 17 3 68
2 8 2 68
3 14 2 83
4 16 2 77
并且输入是:
1 9 1 D
2 8 2 D
3 14 2 S
4 16 2 M
5 20 4 S
6 1 7 G
7 23 3 D
8 24 4 D
9 17 3 M
10 80 4 D
为什么在添加该代码后输出更改?
使用索引i
和i+1
时,请确保您没有超出允许的范围.这与(int i= 0;i<10;++i)
不兼容!
数组溢出会产生非常意外的结果.
这不会回答您的问题,但是...
帮自己(和我们)一个巨大的忙,缩进您的代码.
当它看起来像这样时,它非常容易阅读和理解正在发生的事情:
for ( int j = 0 ; j< 20; ++ j){ for ( int i = 0 ; i< ; 10; ++ i){ 如果(chart1 [i + 1] [ 2 ]> chart1 [i] [ 2 ]){ temp [ 0 ] = chart1 [i] [ 0 ]; temp [ 1 ] = chart1 [i] [ 1 ]; temp [ 2 ] = chart1 [i] [ 2 ]; temp [ 3 ] = chart1 [i] [ 3 ]; // --------------------- chart1 [i] [ 0 ] = chart1 [i + 1] [ 0 ]; chart1 [i] [ 1 ] = chart1 [i + 1] [ 1 ]; chart1 [i] [ 2 ] = chart1 [i + 1] [ 2 ]; chart1 [i] [ 3 ] = chart1 [i + 1] [ 3 ]; // ---------------------- 跨度> chart1 [i + 1] [ 0 ] = temp [ 0 ]; chart1 [i + 1] [ 1 ] = temp [ 1 ]; chart1 [i + 1] [ 2 ] = temp [ 2 ]; chart1 [i + 1] [ 3 ] = temp [ 3 ]; // --------------------- } } }比所有代码都适合屏幕的LHS ...
hi
i coded this pro...
#include <iostream.h>
#include <conio.h>
main(){
char q;
int y1,y2,p,o=0;
int m=0,d=0,g=0,s=0;
int chart1 [10][4];
int chart3 [10][4];
int G [10][4];
int M [10][4];
int S [10][4];
int D [10][4];
int temp[4];
for(int i=0;i<10;++i)
for(int j=0;j<4;++j){
G[i][j]=0;
M[i][j]=0;
S[i][j]=0;
D[i][j]=0;
}
char chart2 [10][23];
for(int i=0;i<=9;++i){
chart1[i][0]=(1+i);
}
for(int j=0;j<10;++j){
int sum=0;
cout<<"Enter the "<<(j+1)<<" player number:";
cin>>chart1[j][1];
cout<<"\nplease select the "<<(j+1)<<" player position(please ENTER IN CAPITAL FORM):";
cin>>q;
if(q=='G') chart1[j][3]='G';
if(q=='M') chart1[j][3]='M';
if(q=='S') chart1[j][3]='S';
if(q=='D') chart1[j][3]='D';
cout<<"\nplz enter the year1-year'1 of "<<(j+1)<<" player";
cout<<"\nhow many pair years do you want to enter?";
cin>>p;
for(int v=0;v<p;++v){>
cout<<"\nplz enter the year"<<(v+1)<<":";
cin>>y1;
cout<<"\nplz enter the year'"<<(v+1)<<":";
cin>>y2;
sum = sum +(y2-y1);
chart1[j][2]=sum;
}
}
for(int j=0;j<20;++j){
for(int i=0;i<10;++i){
if(chart1[i+1][2]>chart1[i][2]){
temp[0]=chart1[i][0];
temp[1]=chart1[i][1];
temp[2]=chart1[i][2];
temp[3]=chart1[i][3];
//---------------------
chart1[i][0]=chart1[i+1][0];
chart1[i][1]=chart1[i+1][1];
chart1[i][2]=chart1[i+1][2];
chart1[i][3]=chart1[i+1][3];
//----------------------
chart1[i+1][0]=temp[0];
chart1[i+1][1]=temp[1];
chart1[i+1][2]=temp[2];
chart1[i+1][3]=temp[3];
//---------------------
} }}
for(int i=0;i<10;++i){//
cout<<"\n";//
for(int j=0;j<4;++j)//
cout<<" "<<chart1[i][j];//
}//
cout<<"\n";
for(int i=0;i<10;++i){//
if(chart1[i][3]=='D'){//
for(int j=0;j<4;++j){//
D[o][j]=chart1[i][j];}//
++o;}}//
//
for(int i=0;i<10;++i){//
cout<<"\n";//
for(int j=0;j<4;++j)//
cout<<" "<<D[i][j];//
}//
///
getch();
}
before i add this part
for(int i=0;i<10;++i){//
if(chart1[i][3]=='D'){//
for(int j=0;j<4;++j){//
D[o][j]=chart1[i][j];}//
++o;}}//
//
for(int i=0;i<10;++i){//
cout<<"\n";//
for(int j=0;j<4;++j)//
cout<<" "<<D[i][j];//
}//
///
the output of chart1 is something like this:
6 1 7 71
5 20 4 83
8 24 4 68
10 80 4 77
7 23 3 68
9 17 3 68
2 8 2 68
3 14 2 83
4 16 2 77
1 9 1 68
but after adding those codes the output of chart1 is:
4239560 1638200 4207616 4263780
5 20 4 83
8 24 4 68
10 80 4 77
7 23 3 68
9 17 3 68
2 8 2 68
3 14 2 83
4 16 2 77
and also the input is:
1 9 1 D
2 8 2 D
3 14 2 S
4 16 2 M
5 20 4 S
6 1 7 G
7 23 3 D
8 24 4 D
9 17 3 M
10 80 4 D
WHY after adding that code the output change?
what should i do?
When using indexesi
andi+1
, make sure that you don''t exceed the allowed range. This is not compatible with(int i= 0;i<10;++i)
!
Array overflow can have quite unexpected effects.
This isn''t going to answer your question, but...
Do yourself (and us) a massive favour and indent your code.
It is much, much easier to read - and to work out what is going on - when it looks like this:
for(int j=0;j<20;++j){ for(int i=0;i<10;++i){ if(chart1[i+1][2]>chart1[i][2]){ temp[0]=chart1[i][0]; temp[1]=chart1[i][1]; temp[2]=chart1[i][2]; temp[3]=chart1[i][3]; //--------------------- chart1[i][0]=chart1[i+1][0]; chart1[i][1]=chart1[i+1][1]; chart1[i][2]=chart1[i+1][2]; chart1[i][3]=chart1[i+1][3]; //---------------------- chart1[i+1][0]=temp[0]; chart1[i+1][1]=temp[1]; chart1[i+1][2]=temp[2]; chart1[i+1][3]=temp[3]; //--------------------- } } }Than when all the code is flat to the LHS of the screen...
这篇关于C ++中的数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!