Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,057 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,572 people online right now. Registration is fast and FREE... Join Now!




Question- Clarification on problem needed

 
Reply to this topicStart new topic

Question- Clarification on problem needed

Aertle
17 Jul, 2008 - 06:32 PM
Post #1

New D.I.C Head
*

Joined: 17 Jul, 2008
Posts: 2


My Contributions
Evening, I have a question that I am looking for clarification on. My main problem is the book I am useing is not written very well, and I am looking for an "push" in the right dirrection.

Here is the question:
QUOTE

Write a function, removeAt, that takes 3 parameters: an array of intigers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output the appropriate msg. (Note that after deleting the element, the number of elements is reduced by 1). Assume the array is unsorted.


So though my searching I haven't found a way to delete an element from an array. I am sure that this is simple, but I am new to cpp and at a loss.

I am not looking for code help (yet), but here is what I have so far.
CODE

    int removeAt (const int array1 [], int elements, int index)
    {
        int find;
        //bool found = false;
        for (find =0; find < elements; find++)
            if (array1[find] == index) //so I look for/find index...
                //?????
        return -1;
    }


Please help

~A
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Question- Clarification On Problem Needed
17 Jul, 2008 - 06:47 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,458



Thanked: 10 times
Dream Kudos: 325
My Contributions
'find' is the index variable in your for-loop, you should be comparing that to 'index'.

You won't be able to modify any elements of the array because you have the "const" keyword in place, which makes a variable read-only.

Edit: In C, you can't really "remove" one box of an array without recreating it. You'll have to create another array, and then copy the elements from the first one over into it.

You could probably do something like create an array initially with one-less elements than the one passed to the function. Then you could copy all of the numbers into it except the one at index 'index'.

This post has been edited by Tom9729: 17 Jul, 2008 - 06:50 PM
User is online!Profile CardPM
+Quote Post

Aertle
RE: Question- Clarification On Problem Needed
18 Jul, 2008 - 07:54 AM
Post #3

New D.I.C Head
*

Joined: 17 Jul, 2008
Posts: 2


My Contributions
This is what I ended up coming up with... (I still think it is a bad question)
CODE
    int removeAt (int *array1, int elements, int index)
    {
        int find, newarraysize;
        for (find =0; find < elements; find++)
        {
            if (array1[find] == index)
            {
                for(int i=find; i<(elements-1); i++)
                {
                    array1[i] = array1[i+1];
                }
                newarraysize = elements-1;
                return 0;
            }
        }
        return -1;
    }    

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month