Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi i coded this pro...

C++
#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
C#
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?
Posted
Updated 1-Aug-11 10:11am
v3
Comments
Guyverthree 1-Aug-11 11:38am    
I would put the code above into Visual studio and use a debugger to step through the code so you can see what is going on at each step.
Something is obviously doing something you don't expect but since i don't know what you are trying to do I can't say what is wrong. A debugger with watch and step though abilities will be most useful in this endeavour.

When using indexes i and i+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.
 
Share this answer
 
Comments
DominoBoy 1-Aug-11 12:43pm    
thx you are right
YvesDaoust 1-Aug-11 13:25pm    
Glad to know
- Yves
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:
C++
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...
 
Share this answer
 
Comments
DominoBoy 1-Aug-11 12:14pm    
yeah you are right thx
Guyverthree 1-Aug-11 12:31pm    
I agree indentation is good practice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900