Like Tree1Likes
  • 1 Post By rogster001

could someone help me with my code

This is a discussion on could someone help me with my code within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I'm new to this board and fairly new to programming and I'm trying to understand some concepts on ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    could someone help me with my code

    Hello everyone, I'm new to this board and fairly new to programming and I'm trying to understand some concepts on arrays so I'm trying to make this little program. I have a future assignment coming up that has some of these concepts so I'm trying to understand them so I can apply it to my assignments.


    I'm trying to delete a number from a list of numbers in an array
    and the program is also suppose to delete duplicates but i have no idea how to do it in my for loop
    the numbers of the list are these

    0,1,4,4,5,2,1,7,7

    so if the user puts in 0 first

    the list becomes 1,4,4,5,2,1,7,7

    if they put 4 instead

    the list becomes

    0,1,5,2,1,7,7



    this is my code here
    can somebody help me with what is going on i tried 4 but the output is wrong i got
    0,1,4,5,5,2,1,7 instead of 0,1,5,2,1,7,7


    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    void Get_Number(int &Number_to_Delete){
    
            cout << endl << "Enter number to delete."<<endl;
            cin >> Number_to_Delete;
    
    }
    
    
    void Delete_Number(int Number_to_Delete, int array[],int &count){
    
            cout<< "The number " <<Number_to_Delete<< " is going to be deleted." <<endl;
        
           for(int i=0;i<count;i++){
    
                    if(Number_to_Delete==array[i]){
    
                           array[i]=array[i+1];
                  
                    }
    
                }
    
           count--;
    }
    
    
    void Display_Numbers(int array[],int &count){
       
             for(int i=0;i<count;i++){
    
                     cout <<array[i];
    
             }
    
    }
    
    int main(){
    
    
        int list[9]={0,1,4,4,5,2,1,7,7};
        int Number_to_Delete;
        int count = 9;
    
    
    
    
        Display_Numbers(list,count);
    
    
        Get_Number(Number_to_Delete);
    
    
        cout << "You want to delete the number "<< Number_to_Delete << endl;
    
    
        Delete_Number(Number_to_Delete,list,count);
    
    
        Display_Numbers(list,count);
    
    
        system("pause");
    
    
    
    
    
    
    }

    My output for this code


    014452177
    Enter number to delete.
    4
    You want to delete the number 4
    The number 4 is going to be deleted.
    01455217
    Press any key to continue . . .
    Last edited by c++noob145; 02-23-2013 at 08:04 PM.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    you're replacing a[2] with a[3], where a[2] = a[3] = 4, so it seems like the 4 is not going anywhere. Then the code replace a[3] with a[4] which replicate the "5" in a[4] and give the "55" in the sequence. The array size is the same, but you decrement count by 1, so the last 7 went awol.

    The easiest way is to use two array of the same size. Iterate over the first array, if the deleted value equals an element of the first array, don't copy it into the second array, else push the value into the end of the second array.

    Another way is to use a linked list, and delete all the nodes that store the "delete_value"
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    In your function Delete_Number() you iterate through the array with only one variable. You should use two variables; one for the reading position and another one for the writing position. They both would start at 0 and the reading position (i) would be incremented every time, but the writing position (j) would be incremented only when there's something worth to be written:

    Code:
        for (i=j=0; i<count; i++)
            if (array[i] != Number_to_Delete)
                array[j++] = array[i];
    After this, you should set the size of the array to j.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    You might consider a vector instead of c style arrays. Also i am not fond of function names with underscores in them, but thats just personal preference
    Elysia likes this.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 02:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 03:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 08:51 PM

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