Hey so this part of the code is supposed to calculate how many odd numbers and even there are in the array. I searched and finally wrote this piece of code but it seems to be a problem with it since it onlycounts the odd well. Thanks for your time, really apreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int Contenedor::cantidadPares(){
int impar = 0;
int par = 0;
for (int i = 0; i<10; ++i)
{
if (Vector [i] % 2 == 0)
++par;
// no funciona par.
else
++impar;
}
cout << "Pares:" << par << endl << "Impares: " << impar << endl;
return 0;
#include "encabezado.h"
usingnamespace std;
this is the main. Should i give you the whole code? i mean, the three files? Header / cpp /cpp ?
int main(){
cout << "------------------------------------------------------------------" << endl;
//creacion de un contenedor
Contenedor C1;
//agragando elementos al contenedo
cout << "Elementos: " << endl;
C1.agregaElemento(10);
C1.agregaElemento(20);
C1.agregaElemento(40);
C1.agregaElemento(7);
C1.agregaElemento(9);
C1.agregaElemento(5);
cout << endl << endl;
//imprimiendo el contenedorC1
C1.imprimeContenedor();
cout << endl << endl;
C1.cantidadPares();
// C1.cantidadPrimos();
//inserta en posicion
//C1.insertarPos(3);
//imprimiendo el contenedorC1
//C1.imprimeContenedor();
cout << "------------------------------------------------------------------" << endl;
system("pause");
return 0;
}
#include<iostream>
usingnamespace std;
class Contenedor{
private:
int Vector[10];
int Cantidad;
int Tamano;
public:
//constructor
Contenedor();
~Contenedor();
//gets
int getCantidad();
int getTamano();
//metodos de calculo
void inicializar();
bool agregaElemento(int ele);
void imprimeContenedor();
void invertir();
int cantidadPares();
int cantidadPrimos(double n);
int eleMayor();
int sumaTotal();
bool insertarPos(int pos, int ele);
bool eliminarPos(int pos);
bool eliminarEleRep(int ele);
void ordenarElementos(); //de men a may
bool intercambioPos(int pos1, int pos2); //ejercicios de la pag 76 y 77
};
in your cantidadPares() function you have a loop of 10, what happens if you only entered 5 numbers.. you still are looping 10 times and the other 5 numbers will be random numeric values at those memory locations.
Use the Cantidad variable then you will loop the correct number of times:
what language is that? i could understand the whole code, but doesn't understand the meaning of each variable. Maybe, start using universal language to write codes. It will benefit you more in the future
what language is that? i could understand the whole code, but doesn't understand the meaning of each variable. Maybe, start using universal language to write codes. It will benefit you more in the future
Why would he not want to code his program using variable names in his own native language? - seems like a pointless comment to me.
I'm English and didn't have a problem reading his code - maybe if he was in a software house and a team of programmers were working together on a program but hes coding for his own enjoyment.