2
The goal of the program is to ask the user for two digits and generate a list of sequences, each containing information from the previous.
For example:
User writes 1 1
and the generated list is:
- 21, because the previous sequence has two 1.
- 1211, because the previous sequence has "one" 2 and "one" 1.
- 3112, because the previous sequence has "three" 1 and "one" two
- etc..
When I try to run the show, crash on the 50th line and I have no idea why.
I appreciate the help and I apologize for the possible irrelevance of the issue.
Source:
#define Max_num 5000
typedef struct {
int numero;
int count;
}Numero;
typedef Numero Sequencia[Max_num];
// retorna comprimento da sequencia
int add_num ( Sequencia numeros , int n , int num){
if ( n == 0) {
numeros[0].numero = num;
numeros[0].count = 1;
return n+1;
}
int i;
for ( i = 0 ; i < n ; i++){
if ( numeros[i].numero == num){
numeros[i].count++;
return n;
}
}
numeros[n].numero = num;
numeros[n].count = 1;
return n+1;
}
void print_vetor(int vetor[] , int n){
int i;
for ( i = 0 ; i < n; i++){
printf("%d",vetor[i]);
}
printf("\n");
}
void ler_sequencia(Sequencia numeros , int vetor[], int n){
int i; //controla vetor recetor
int x = 0;//controla sequencia
for ( i = 0 ; i < n; i++){
vetor[i] = numeros[x].count;
i++;
vetor[i] = numeros[x].numero;
x++;
}
}
void processar_vetor( int vetor[] , int n ){
int i;
Sequencia numeros;
int c = 0;// comprimento da sequencia
for ( i = 0 ; i < n; i++){
c = add_num( numeros , c , vetor[i]);
}
int vetor_final[2*c];
ler_sequencia( numeros , vetor_final , 2*c);
print_vetor(vetor_final , 2*c);
processar_vetor( vetor_final, 2*c);
}
int main(){
int vetor[2];
printf("Escreve dois algarismos: ");
scanf( "%d %d",&vetor[0] ,&vetor[1]);
processar_vetor( vetor , 2 , 1);
return 0;
}
I’m going to say something that I’m almost sure will be ignored for some reason. The problem with this algorithm is that it’s not suitable for recursion. Then it gets tricky to get it right. I know it may be that someone sent it, that it may be that they are doing it to learn, but the ideal is to learn to do something that is inherently recursive and not force the bar on something that is not. See http://answall.com/q/21551/101 and http://answall.com/q/107657/101
– Maniero
I understand , but I would like to understand what is the real problem with the code I wrote, why does it only work until the 50th recursion?
– Gonçalo Mateus
(1) I think that this program neither compiles, the declaration of "parse_vector" is different from the use (2) the function "parse_vector" need to have a stop criterion, otherwise it is in infinite loop and will crash even
– zentrunix