Doubt in activity with POO

Asked

Viewed 56 times

0

Hello, basically the exercise is to have the user type numbers in an array and then I have to have the array separated into another 2 : one for pairs and the other for odd ones. I’ll leave the code below, because I can’t get the numbers to be separated.

How to separate I know, but when it comes to putting the numbers in the arrays is that of the problem. If someone can help me I would appreciate.

#include <iostream>
using namespace std;

class separar{

    int p[9]; int im[9]; int v[9];

    public:


    int inserir(){

    cout << "Digite os 10 numeros para preencher o vetor" << endl;
    for(int i = 0; i<10; i++){

        cout << "Digite o numero da posicao " << i + 1 << " :" << endl;
        cin >> v[i];
        }
    return 0;
    }

    int dividir(){

        for(int i=0; i <10; i++){
            if(v[i] % 2 == 0){
            p[i] = v[i];
            p[i++];
            }
            else{
                im[i] = v[i];
                im[i++];
            }
        }
        return 0;
    }

    int mostrar(){
        cout << "Vetor inicial: " << endl;
        for(int i=0; i <10; i++){

            cout << "[" << v[i] << "]" << endl;
        }
        cout << "Vetor par: " << endl;
        for(int i=0; i <10; i++){

            cout << "[" << p[i] << "]" << endl;
        }
        cout << "Vetor impar: " << endl;
        for(int i=0; i <10; i++){

            cout << "[" << im[i] << "]" << endl;
        }
        return 0;
    }
};

int main(){
    int v[9];
    int p[9]; int im[9];
    int menu;
    separar vetores;


    while(4){

        cout << "O que deseja fazer: " << endl;
        cout << "1. Inserir elementos." << endl;
        cout << "2. Separar em pares e impares." << endl;
        cout << "3. Visualizar arrays." << endl;
        cout << "4. Sair." << endl;
        cin >> menu;

        switch(menu){

            case 1:
                vetores.inserir();
                break;   

            case 2:
                vetores.dividir();
                break;

            case 3:
                vetores.mostrar();
                break;

            case 4:
                exit(4);
                break;      
    }       
    }
    return 0;
}
  • Good evening, Gabriel! Edit your question and include a possible error message, so the solution to your problem can be streamlined.

  • Good night to you, Ivan. The program compiles everything right, the problem is that the separation of pairs and odd does not happen, and when it shows on the screen, random numbers appear.

1 answer

0

Hello. I managed to solve the problem, I will leave the solution here, in case someone needs.

#include <iostream>
using namespace std;

class separar{

int v[9];
int p[9]; 
int q[9];
int k;
int j=0;

public :

int inserir(){
    cout << "Digite os 10 numeros para preencher o vetor" << endl;
        for(int i = 0; i<10; i++){

        cout << "Digite o numero da posicao " << i + 1 << " :" << endl;
        cin >> v[i];
}
}
    int dividir(){
    for(int i=0; i < 10; i++){
        if(v[i] % 2 == 0){
            p[k] = v[i];
            k++;
        }
        else {
            q[j] = v[i];
            j++;
        }
}   
}

int mostrar(){

    cout << "Vetor inicial: " << endl;
        for(int i=0; i <10; i++){

        cout << "[" << v[i] << "]" << endl;
    }
    cout << "Vetor par: " << endl;
        for(int i=0; i <k; i++){

        cout << "[" << p[i] << "]" << endl;
    }
    cout << "Vetor impar: " << endl;
        for(int i=0; i <j; i++){

        cout << "[" << q[i] << "]" << endl;
    }

}


};




int main(){

separar vetor;
int menu;


while(4){

    cout << "O que deseja fazer: " << endl;
    cout << "1. Inserir elementos." << endl;
    cout << "2. Separar em pares e impares." << endl;
    cout << "3. Visualizar arrays." << endl;
    cout << "4. Sair." << endl;
    cin >> menu;

    switch(menu){

        case 1:
            vetor.inserir();
            break;   

        case 2:
            vetor.dividir();
            break;

        case 3:
            vetor.mostrar();
            break;

        case 4:
            exit(4);
            break;      
}       
}
return 0;
}
  • 2

    Explain what was wrong Gabriel.

  • It would be nice to comment on where the problem was and mark your answer as accepted, so the subject is "closed" here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.