¿Cómo puedo sacar los numeros pares de un vector/arreglo?

May 21, 2014 at 6:52pm

Tengo diez numeros en un array, lo único que tengo que hacer es sacar de allí los numeros pares.

Muchas gracias.
May 21, 2014 at 7:08pm
Entonces, ¿cuál es tu pregunta?
May 21, 2014 at 7:08pm
closed account (j3Rz8vqX)
You want their values? or to remove them?

Modding by 2 would determine if they're even or not.

1
2
3
4
if(numbers[i]%2==0)
{
    //Do something...
}
May 21, 2014 at 7:11pm
Este es mi codigo.
Y digamos si tengo los numeros: 2,4,3,5,6,11,8,7,10.

Sacar los numeros pares. En este caso 2,4,6,8,10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Uso de arreglos en C++
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
  int arregloEntero[10] = {0};
  int arreglo_Enter;

    cout << "Introduzca 10 digitos " << endl;
  for (int i = 0 ; i < 10 ; i++)
        {
      cout << " Introduzca nuevo valor para arregloEntero["<<i<<"]" << endl;
      cin >> arregloEntero[i];
    }
    ofstream outFile;
    outFile.open("Numeros.txt");
  cout << "Guardados en Numeros.txt " << endl;
  for (int i = 0 ; i < 10 ; i++){
    outFile<<arregloEntero[i]<<endl;}
          //Notar el menor estricto (<) para ir desde 0 hasta 9


    outFile.close();


  return 0;
}
May 21, 2014 at 7:37pm
closed account (j3Rz8vqX)
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> numbers;
    for(int i=0;i<10;i++)
        numbers.push_back(i);
    for(int i=0;i<numbers.size();i++)
    {
        if(numbers[i]%2==0)
        {
            numbers.erase(numbers.begin()+i);
            --i;
        }
    }
    for(int i=0;i<numbers.size();i++)
        cout<<numbers[i]<<" ";
    cout<<endl;
    cout<<"Custom Exit: Press enter: ";
    cin.get();
    return 0;
}
May 21, 2014 at 7:39pm
Yes, like that. But i only can use <iostream>,<fstream> and <cstdlib>
May 21, 2014 at 8:06pm
closed account (j3Rz8vqX)
Examples:

Scope narrowing scheme:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int numbers[10], keep[10], maxSize=10, arraySize=0;
    for(int i=0;i<10;i++)
        numbers[i]=i;
    for(int i=0;i<maxSize;i++)
    {
        if(numbers[i]%2==1)
        {
            keep[arraySize]=numbers[i];
            ++arraySize;
        }
    }
    for(int i=0;i<arraySize;i++)
        cout<<keep[i]<<" ";
    cout<<endl;
    cout << "Custom Exit: Press enter:" ;
    cin.get();
    return 0;
}


OR

Dynamic array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main()
{
    int numbers[10], *keep, maxSize=10, arraySize=0;
    for(int i=0;i<10;i++)
        numbers[i]=i;
    for(int i=0;i<maxSize;i++)
    {
        if(numbers[i]%2==1)
        {
            int *temp = keep;
            keep = new int[arraySize+1];
            for(int j=0;j<arraySize;j++)
                keep[j]=temp[j];
            keep[arraySize]=numbers[i];
            delete []temp;
            ++arraySize;
        }
    }
    for(int i=0;i<arraySize;i++)
        cout<<keep[i]<<" ";
    cout<<endl;
    cout << "Custom Exit: Press enter:" ;
    cin.get();
    return 0;
}
Last edited on May 21, 2014 at 8:09pm
May 21, 2014 at 8:09pm
I need something like this but with my code.
'cause i need an outfile.txt. with my even numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
int main() {
    int input, remainder, even = 0, odd = 0;
    int evenArray[even];
    int oddArray[odd];
    cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
    cin >> input;
    while (input != 0) {
        remainder = input % 2;
        if (remainder == 0) {
            evenArray[even] = input;
            even++;
        }
        else {
            oddArray[odd] = input;
            odd++;
        }
        cout << "Enter another integer: ";
        cin >> input;
    }
    cout << "\nThe number of evens is " << even << ".\n";
    cout << "The even values are: ";
    for(int i = 0; i < even; i++) {
        cout << evenArray[i] << " ";
    }
    cout << endl;
    cout << "The number of odds is " << odd << ".\n";
    cout << "The odd values are: ";
    for(int i = 0; i < odd; i++) {
        cout << oddArray[i] << " ";
    }
}
May 21, 2014 at 8:13pm
closed account (j3Rz8vqX)
That is up to you, I've already given examples of parsing an array of even and odd numbers.

Use an else to put all non even numbers into the other array, if you are seeking odds.

Good luck.
Topic archived. No new replies allowed.