0

I have Array A[9]= {1,2,3,4,5,6,7,8,9} and I need to delete the numbers which are not dividing by 2. The code I tried to do:

int main()
{
    int n;
    ifstream fd(Cdf);
    fd>>n; // read how many numbers are in the file.
    int A[n];
    for(int i = 0; i < n; i++)
    {
        fd >> A[i]; //read the numbers from file
    }
    for(int i = 0; i < n; i ++) // moving the numbers.
    {
        if(A[i] % 2 !=0)
        {
            for(int j = i; j < n; j++)
            {
                A[i] = A[i+1];
            }
        }
    }
    fd.close();
    return 0;
}

But I get numbers like 224466888. what I need to do to get 2,4,6,8?

I need to delete numbers in the same array.

7
  • First comment: meaningful variable names make code easier to read. Commented May 15, 2014 at 13:46
  • Why not use a better data structure, like std::vector or std::list? Commented May 15, 2014 at 13:48
  • @crashmstr Because of learning. Commented May 15, 2014 at 13:59
  • 1
    @molbdnilo yes, and learning use of good data structures is a good idea. Commented May 15, 2014 at 14:02
  • @crashmstr Yes it is, but that's not the purpose of this exercise. Commented May 15, 2014 at 15:00

4 Answers 4

2

First you should use std::vector for dynamic size arrays. Second, for removing numbers that are even in a vector, you can do :

std::vector<int> inf = {12,0,5,6,8};
auto func = [](int i){return i % 2 != 0;}; 
inf.erase(std::remove_if(inf.begin(),inf.end(),func), inf.end());

EDIT :

Ok, so you can still do this without std::vectors, but it will be uglier :

#include <algorithm>

int res[] = {2,5,9,8,6,7};
int size = 6;
auto func = [](int i){return i % 2 != 0;};
int new_size = std::remove_if(res,res + size, func) - res;

All the data you want is in [0, new_size[ range, the other part of your array is now garbage.

Sign up to request clarification or add additional context in comments.

5 Comments

i can't use the Vector. because i learning it at school. and my teacher give me to do this with the arrays...
@user3609104 Edited for an array version
#include <algorithm> // for std::remove_if
-1 because this answer is useless for a beginner. The exercise is obviously about exploring the basics of arrays and loops, not about using the higher-level parts of the standard library.
That's one way of seeing it. I personally think that beginner should be aware of portable and robust solution offered by the Standard library, instead of always (badly?) reinventing the wheel, especially on a website that lots of beginners could see for guidance. But i understand your point of view.
0

Your removal loop is indexing with the wrong variable:

for(int j = i; j < n; j++)
{
    A[i] = A[i+1];
}

You're using i, which doesn't change in the loop.
Change it to j. You also need to subtract one from the upper limit, as you'd step outside of the array otherwise when accessing A[j + 1].

for(int j = i; j < n - 1; j++)
{
    A[j] = A[j + 1];
}

1 Comment

this does still not remove anything
0

An array can't be used for your purpose. It is allocated on stack and its size can't be changed dynamically (you can't change the size of an array in general, not only when it is allocated on stack).

You could allocate a second array and keep reallocating it with realloc everytime you add a new element but that's not the good way to do it. You are working with C++ so just use a std::vector<int> and your problems will be solved:

std::vector<int> evenArray;
evenArray.reserve(sizeof(A)/sizeof(A[0])/2);

if (number is even) {
  evenArray.pushBack(number);
}

Mind that vector stores elements contiguously so this is legal:

int *evenA = &evenArray[0];

1 Comment

i can't use the Vector. because i learning it at school. and my teacher give me to do this with the arrays..
0

For your inner for loop you should be referencing j, not i.

for(int j = i; j < n - 1; j++)
{
    A[j] = A[j+1];
}

Otherwise, what's the point of creating j?

Of course, this also means if you read the whole array back you will display all the characters that were shifted (which will just be equal to the last number). So, you should probably keep track of the new length of the array and just iterate to that instead of the end of the array.

EDIT:
In the inner for loop you need to loop to n - 1 otherwise when you have A[j + 1] it will go off the end of the array when you to change it, which may or may not give you a runtime error.

2 Comments

so you say i just need to A[i] = A[i+1] write in the If sentence without For loop?
No, you need to change the inner for loop to use j instead of i when accessing the array. Otherwise you are just using the value of i, which hasn't changed. So, every time you hit an odd number all you're doing is replacing it with the number directly after it. Changing it to j and not i will make it so it will repeat that process down the array. Though, molbdnilo had a point and you need to make it n - 1 so you don't go off the end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.