Help me with my C++ code please

This is a discussion on Help me with my C++ code please within the C++ Programming forums, part of the General Programming Boards category; void Compact(apvector<int> &List, int &N) //Pre:List contains integers from 0...N-1 //PostCondition all 0's have been removed from the list; N ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Help me with my C++ code please

    void Compact(apvector<int> &List, int &N)
    //Pre:List contains integers from 0...N-1
    //PostCondition all 0's have been removed from the list; N is reduced as appropriate
    {
    for(int i = 0; N > i; i++)
    {
    while(List[i] == 0)
    {
    for(int c = 0; c < N; c++)
    {
    List[i] = List[i+1];
    N--;
    }
    }
    }

    List.resize(N);
    for(int b = 0; b != List.length(); b++)
    {
    cout << List[b] << " ";
    }
    }

    and now I am in an endless loop...I am trying to make it so it reads the numbers off an array and removes all the zero and then put everything back into an array of vectors. and then resizes it to save memory...also a parameter is not to use a second array. I don't know whats wrong...this should work but it doesn't someone help me....also I am a high school student...also I am using very basic class's such as iostream, iomanip, apvector, math, fstream, limits and apstring. Also I am using MSVC 6(the latest one) if that helps

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Red face

    Code:
    void Compact(apvector<int> &List, int &N)
    //Pre:List contains integers from 0...N-1
    //PostCondition all 0's have been removed from the list; N is reduced as appropriate
    {
         for(int i = 0; N > i; i++)
         {
              while(List[i] == 0)
              {
                   for(int c = 0; c < N; c++)
                   {
                        List[i] = List[i+1];
                        N--;
                   }
              }
         }
    
         List.resize(N);
         for(int b = 0; b != List.length(); b++)
         {
               cout << List[b] << " ";
         }
    }

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Errr.. your logic is a little messy. If you want to do it this way you need to change a few things. First off you have i swapping with its next partner N times over and over again instead of moving up the list. Also, you don't want to decrease N until you are done with all the swapping. You have it decreasing every swap. Also, you only want to swap the items from i to N, not from zero. When doing code like this it is very important at this stage in you programming to take a piece of paper and trace through your code step by step keeping track of what variable is equal to what. This will give you a better idea of what is going on and how to change it. Try this:

    Code:
         for(int i = 0; N > i; i++)
         {
    
              while(List[i] == 0 && i<N)
              {
                   for(int c = i; c < N-1; c++)
                   {
                        List[c] = List[c+1];
                   }
                   N--;
              }
         }
    Haven't tested this so I wouldn't be surprised if theres a bug...

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    thanks...I've been cracking at this program for some bit of time...I guess I'm not that logical...or I should write down my programs before hand instead of hacking away at code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21