本文介绍了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<< "  \ n请选择"<<(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<< "  \ nplz输入"<<(j +1)<< " 播放器" ;
cout<< "  \您要输入多少对年份?" ;
cin> p;
 for ( int  v =  0 ; v< ; p; ++ v){>
cout<< "  \ nplz输入年份" <<(v + 1)< < " :" ;
cin> y1;
cout<< "  \ nplz输入年份'" <<(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<< "  \ n" ; //  
 for ( int  j =  0 ; j< ; 4; ++ j)//  
cout<< " " <<< chart1 [i] [j]; //  
} //  

 cout<< "  \ n" ;

 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<< "  \ n" ; //  
 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<< "  \ n" ; //  
 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


为什么在添加该代码后输出更改?

解决方案



hii 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?

解决方案



这篇关于C ++中的数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 17:47