The purpose of the program is to ask the user for two digits and generate a list of sequences, each containing information from the previous one.
For example:
The user writes 1 1
and the generated list is:
- 21, because the previous sequence has two 1
- 1211, because the previous sequence has "a" 2 and "a" 1.
- 3112, because the previous sequence has "three" 1 and "one" two
- etc.
When I try to run the program, it crashes into the 50th line and I have no idea why.
Thank you for your help and I apologize for any irrelevance.
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;
}