![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
[beginner] how to reverse the contents of a table
Hello,
I really am blocked now... I can't figure out the algorith to inverse the contects of a table... and I have been working on it for quite a while now but I don't get decent results.. here's my code class Table{
int contenu[];
int taille;
Table(){
System.out.println("quelle est la taille du tableau à créer");
taille = Clavier.lireInt();
contenu = new int[taille];
initValue();
}
Table(int n){
taille = n;
contenu = new int[taille];
initValue();
}
private void initValue(){
System.out.println("valeur de chacune des cases");
for(int i=0;i<taille;i++){
contenu[i]= Clavier.lireInt();
}
}
// fonction d'affichage
public String toString(){
String s="[";
for(int i=0;i<taille-1;i++)
s=s+contenu[i]+",";
s=s+contenu[taille-1]+"]";
return(s);
}
public void reverse(){
int tab2[]= new int[taille];
for(int i = taille-1; i>=0;i--){
int k =0 ;
tab2[k] = contenu[i];
k++;
}
for (int i=0;i<taille;i++){
contenu[i]= tab2[i];
}
}
public void sort(){
int j;
for(int i=1;i<taille;i++){
for(j=0;contenu[j]<contenu[i];j++);
int tampon = contenu[i];
for (int k=i;k>j;k--)
contenu[k] = contenu[k-1];
contenu[j] = tampon;
}
}
}
public class Tableau{
public static void main(String args[]){
Table tab = new Table(5);
tab.sort();
System.out.println( "tableau trié " + tab);
Table tab1 = new Table();
System.out.println("tableau non inverse " +tab1);
tab1.reverse();
System.out.println("tableau inversé " +tab1);
}
}thanks in advance, Mario
__________________
printf("\n I might need you to help me tonight"); getch(); |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I haven't compiled it, but the error looks like its happening with your variable k. Every time the loop iterates, k gets set back to 0. If you wanted to use k, you would have to declare it outside of the loop. Alternatively, you could say:
tab2[tallie-1-i] = contenu[i]; That should work, as well as your k method if you bring k outside the loop.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
hello
it does work; I removed the variable k and put the code you suggested instead. now as my goal is learning; do you mind if I ask you explain to me, why do you use the code you suggested? I am not very good in algorithmic and it's really causing me troubles in my progression whether is in java, c, or other objet or procedural programming; thank you very much.. mario
__________________
printf("\n I might need you to help me tonight"); getch(); |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Ok, well, let us say for simplicity sake that contenu's length is 5, therefore tallie is 5. Now, what you're trying to do is reverse the contents of contenu into tab2. So we're saying tabt2[tallie-1-i] = contenu[i] -- well check out the value of tallie-1-i with respect to i
value of i: Value of tallie-1-i: 0 (5)-1-(0) = 4 1 (5)-1-(1) = 3 2 (5)-1-(2) = 2 3 (5)-1-(3) = 1 4 (5)-1-(4) = 0 Hope that helps. Edit: I can't get it to align properly, for some reason.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
that's wonderful!!! thank you very much.... really thank you.
__________________
printf("\n I might need you to help me tonight"); getch(); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|