My program hangs showing the same result

1

Hi, I'm doing a program that takes a number, subtracts it until I get the kaprekar constant, everything is fine and it works very well, but I do not understand why sometimes when I put certain numbers it hangs up like the number for example 8456 . Here I show how it hangs:

The topic esque is using global variables because I can not return three values from a function that would be mayor1 , menor1 and fin which would be the new value of the subtraction and that is why I think the problem arises. For example if I put another number lower eg. 1234 resolves me:

Here the code made from scratch by me for which it will not be very efficient but good I am learning, here it is:

#include <iostream>
#include <sstream>
using namespace std;

int num1,z,l,peque,grande;
int numero,division,resto,i,a,j;
int arr[4];
int mayor[4];
int menor[4];
int mayor1;
int fin = 0;
int menor1;
int contador = 0;

int prueba(int num){
    division = num;
    resto = num;

    for(i=0;i<=3;i++){
        resto = division%10;
        if(i==3){
            arr[i] = division;
        }else{
            arr[i] = resto;
        }
        division/=10;
    }
    int o,mini,maxi,u;


    mini=arr[0];
    maxi=mini;


    for(o=1;o<4;o++)
            {
            if (arr[o]<mini) mini=arr[o];
            if (arr[o]>maxi) maxi=arr[o];
            }

    peque = 0;

    for(z=0;z<4;z++){
        num1=arr[z];
        if(arr[z] != mini || arr[z] != maxi){
            for(l=0;l<4;l++){
                if(arr[l]<num1 && arr[l] != maxi && arr[l] != mini){
                    peque=arr[l];
                }else if(arr[l]>peque && arr[l] != maxi && arr[l] != mini){
                    grande = arr[l];
                }
            }
    }

    }


    mayor [0] = maxi;
    mayor [1] = grande;
    mayor [2] = peque;
    mayor [3] = mini;

    menor [0] = mini;
    menor [1] = peque;
    menor [2] = grande;
    menor [3] = maxi;

    stringstream ss;

    ss<<menor[0];   //this can be run as a loop
    ss<<menor[1];
    ss<<menor[2];
    ss<<menor[3];

    ss>>menor1;

    stringstream s;

    s<<mayor[0];   //this can be run as a loop
    s<<mayor[1];
    s<<mayor[2];
    s<<mayor[3];


    s>>mayor1;
    fin = mayor1 - menor1;
    return fin;
}

int main(){
/* Declaramos el array y le damos valores */
    cout << "Introduce un numero: ";
    cin >> numero;
    fin = numero;
    while(fin!=6174){
        fin=prueba(fin);
        cout << fin << " = " << mayor1 << " - " << menor1;
        cout << "\n";
        contador++;
    }
    cout << "Numero de intentos: " << contador;
    return 0;
}

The problem of getting the numbers in between.

for(z=0;z<4;z++){
        num1=arr[z];
        if(arr[z] != mini || arr[z] != maxi){
            for(l=0;l<4;l++){
                if(arr[l]<num1 && arr[l] != maxi && arr[l] != mini){
                    peque=arr[l];
                    cout << peque;
                }else if(arr[l]>num1 && arr[l] != maxi && arr[l] != mini){
                    grande = arr[l];
                    cout << grande;
                }
            }
    }

The only way that occurred to me is to make a loop and then take and check it with everyone if it is not minimum or maximum and knowing if it is bigger or smaller because I take the small or large, but for now I have not been able to optimize it correctly.

    
asked by Sergio Ramos 11.03.2017 в 00:44
source

1 answer

1

The kaprekar constant imposes a limitation and that is that the loop should not be repeated more than 7 times ... this avoids the endless loops like the one you are commenting on.

Replaces this:

while(fin!=6174){
    fin=prueba(fin);
    cout << fin << " = " << mayor1 << " - " << menor1;
    cout << "\n";
    contador++;
}

Because of this:

for( int ciclo=0; ciclo<7 && fin != 6174; ++ciclo )
{
    fin=prueba(fin);
    cout << fin << " = " << mayor1 << " - " << menor1;
    cout << "\n";
    contador++;
}

if( fin != 6174 )
{
  // El numero no posee solución
}
    
answered by 11.03.2017 / 16:23
source