Odd or Even counter in Array

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;

Code looks fine to me, maybe the problem is elsewhere.

Post the whole code maybe?
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
#include "encabezado.h"
using namespace 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;
}
This one is the header file
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
34
35
36
37
38
#include<iostream>
using namespace 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




};


and this one the complete part of the first post.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "encabezado.h"


Contenedor::Contenedor(){
	inicializar();
	Cantidad = 0;
	Tamano = 10;

}
Contenedor::~Contenedor(){

}


int Contenedor::getCantidad(){
	return Cantidad;
}
int Contenedor::getTamano(){
	return Tamano;
}


void Contenedor::inicializar(){
	cout << "--------Inicializando--------" << endl;
	for (int i = 0; i<Tamano; i++)
		Vector[i] = 0;

}
bool Contenedor::agregaElemento(int ele){
	if (Cantidad == Tamano)
		return false;
	else{
		Vector[Cantidad] = ele;
		Cantidad++;
		return true;
	}
}
void Contenedor::imprimeContenedor(){
	int x = 0;
	for (int i = 0; i < Cantidad; i++){

		cout << x++ << ") " << Vector[i] << endl;
	}
	cout << "------------------------------";
}

void Contenedor::invertir(){
	int k; //var temp.
	cout << "--------Invirtiendo--------" << endl;
	for (int i = 0, j = Tamano - 1; i<j; i++, j--){
		k = Vector[j];
		Vector[j] = Vector[i];
		Vector[i] = k;

	}
}

bool Contenedor::insertarPos(int pos, int ele){
	if (pos>Cantidad)
		//no hay nada que insertar
		return false;
	else{
		//hago el corrimiento hacia la derecha
		int k = Cantidad;
	}
}





		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;
		}
		

	/*	double Contenedor::cantidadPrimos(double num){
				
				int esPrimo = 0;
				for (int i = 0<; i <= sqrt(num); i += 2)
				{
					if (i % 2 == 0) i++){
				
				if ((int(num) % i) == 0)
					{
					esPrimo = 1;
					break;
					} }
				return esPrimo; }*/

//int Contenedor::eleMayor(){


//int Contenedor::sumaTotal(){


bool Contenedor::eliminarPos(int pos){
	int k = 0; // ok?
	int ele = 0; //ok?
		cout << "-----insertando-----" << endl;
		while (k > pos){
			Vector[k] = Vector[k - 1];
			k--;
		}
		Vector[pos] = ele;
		Cantidad++;
		return true;
	}

//bool Contenedor::eliminarEleRep(int ele){


//void Contenedor::ordenarElementos(){


//bool Contenedor::intercambioPos(int pos1, int pos2){ 



Sorry if its confusing :S thanks for your time really!

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int Contenedor::cantidadPares(){

	int impar = 0;
	int par = 0;

	for (int i = 0; i<Cantidad; ++i)
	{
		if (Vector[i] % 2 == 0)
			++par;
		// no funciona par.

		else
			++impar;
	}

	cout << "Pares:" << par << endl << "Impares: " << impar << endl;
	return 0;
}

------------------------------------------------------------------
--------Inicializando--------
Elementos:


0) 10
1) 20
2) 40
3) 7
4) 9
5) 5
------------------------------

Pares:3
Impares: 3
------------------------------------------------------------------
Press any key to continue . . .


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.
Softrix, thanks a lot, have a nice day and really thaks for your time, youre awesome.
Your welcome :)
Topic archived. No new replies allowed.