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.
– Ivan Silva
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.
– Gabriel Lima