Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 3rd, 2005, 5:34 PM   #1
mariogarcia
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 mariogarcia is on a distinguished road
[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);


   }
}
some of it it's in french but it really doens't matter.. I just need to know why my code of public void reverse() doesn't work... and how should I proceed to debug the program..
thanks in advance,
Mario
__________________
printf("\n I might need you to help me tonight");
getch();
mariogarcia is offline   Reply With Quote
Old Nov 3rd, 2005, 6:05 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 3rd, 2005, 6:39 PM   #3
mariogarcia
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 mariogarcia is on a distinguished road
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();
mariogarcia is offline   Reply With Quote
Old Nov 4th, 2005, 12:11 AM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 4th, 2005, 3:12 AM   #5
mariogarcia
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 mariogarcia is on a distinguished road
that's wonderful!!! thank you very much.... really thank you.
__________________
printf("\n I might need you to help me tonight");
getch();
mariogarcia is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC