Invert (Reverse) Array function not working
May 14, 2014 at 7:05pm UTC
Hey, friends, thanks for your time. Below is my code. It is intended for practice and learn about loops and arrays so i cant use "reverse" as you would suggest.
Im working on a function called: "invertir" (Reverse or invert). So, my teaher gave me a hint so i could figure it out but still can't when i run the code it wont make the inversion ok, just the last 2 numbers get changed and throws some trash. I will put below the whole piece of code so you can run it. Thanks a lot. There are some words in spanish but i think that is not a problem, i would really need your help thanks a lot for your time. :)
SOURCE.cpp
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
#include "encabezado.h"
using namespace std;
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.invertir();
C1.imprimeContenedor();
// C1.cantidadPrimos();
//inserta en posicion
//C1.insertarPos(3);
//imprimiendo el contenedorC1
//C1.imprimeContenedor();
cout << "------------------------------------------------------------------" << endl;
system("pause" );
return 0;
}
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
};
Last file .h
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
#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< Cantidad; ++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 im making it too confusing thanks a lot again friends! have a great day.
May 14, 2014 at 7:28pm UTC
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;
void invertArray(char [], int );
int main()
{
char name[8] = "Michael" ;
cout << name << endl;
invertArray(name, 7);
cout << name << endl;
return 0;
}
void invertArray(char arr[], int count)
{
int temp;
for (int i = 0; i < count / 2; ++i)
{
temp = arr[i];
arr[i] = arr[count - i - 1];
arr[count - i - 1] = temp;
}
}
Michael
leahciM
May 15, 2014 at 4:54am UTC
Thanks you so much, Softrix. Beautiful solution! Have a great day. :)
May 15, 2014 at 10:41am UTC
Your welcome :)
Topic archived. No new replies allowed.