Help with fix number in C

2

I'm trying to do the following program with the use of a one-dimensional array:

Create an array of integer type, ask the user how many integer values he will capture, that amount will be the length of the array. Have a menu with the following options:

  • Capture values

  • Sort values

  • print original values

  • print ordered values

  • Exit

  • The code I have is the following, but how do you make the user enter the amount of arrays he wants?

    #include <stdio.h>
    #define TAMANIO 15 /*define los tamaños de los arreglos*/
    
    void procesa( int resp[] ); /*prototipo de funcion*/
    void ordena_arreglo( int a[] ); /*prototipo de funcion*/
    void imprime_arreglo( const int a[] ); /*prototipo de funcion*/
    
    int main(){/*la función main comienza la ejecución del programa*/
        int respuesta[TAMANIO];
        int i, total=0;
    
        printf("\n\tPrograma que almacena en arreglos digitos para despues ser ordenados\n\n\n");
    
        printf("Ingrese los 15 numeros enteros:\n");
        for( i=0; i < TAMANIO; i++ ){
            printf("\n\t\t\tIntroduzca valor [%d]: ", i);
            scanf("%d", &respuesta[i]);
        }/*fin de for*/
    
        procesa( respuesta );/*fución qu procesa las respuestas*/
    
        printf("\n\n");
        system("pause");
        return 0;
    }/*fin de la función main*/
    
    void procesa( int resp[] ){/*ordena el arreglo e imprime por pantalla*/
        printf("\n\nEl arreglo llamado respuestas desordenado es: \n");
        imprime_arreglo( resp );/*muestra el arreglo desordenado*/
    
        ordena_arreglo( resp );/*ordena el arreglo*/
    
        printf("\n\n\nEl arreglo llamado respuesta ordenado es: \n");
        imprime_arreglo( resp );/*muestra el arreglo ordenado*/
    }/*fin de la función procesa*/
    
    void ordena_arreglo( int a[] ){/*función que ordena un arreglo*/
        int pasada; /*contador de pasadas*/
        int j; /*contaodr de pasadas*/
        int almacena;/*ubicación temporal utilizada para intercambiar los elementos*/
    
        for ( pasada = 1; pasada < TAMANIO; pasada++ ){/*ciclo para controlar el número de pasadas*/
            for ( j = 0; j < TAMANIO - 1; j++ ){/*ciclo para controlar el número de comparaciones por pasada*/
                if ( a[ j ] > a[ j + 1 ] ){/*intercambia los elementos si no se encuentran en orden*/
                    almacena = a[ j ];
                    a[ j ] = a[ j + 1 ];
                    a[ j + 1 ] = almacena;
                }/*fin de if*/
            }/*fin del for interno*/
        }/*fin del for externo*/
    }/*fin de la función ordena_arreglo*/
    
    void imprime_arreglo( const int a[] ){/*muestra el contenido del arreglo (5 valores por línea)*/
        int j; /* contador */
    
        for ( j = 0; j < TAMANIO; j++ ){/*muestra el contenido del arreglo*/
            if ( j % 5 == 0 ){/* comienza una nueva línea cada 5 valores*/
                printf( "\n" );
            }/*fin de ifend if*/
            printf( "%3d", a[ j ] );
        }/*fin de for*/
    } /* fin de la función imprime_arreglo */
    
        
    asked by Raul Samperio 19.10.2016 в 00:45
    source

    1 answer

    1

    You need to use dynamic memory. I recommend you investigate the functioning of the malloc () function

    Dynamic memory is used when the size of memory required by a program may vary after it has begun execution and that is exactly your case.

    If the user wanted an array of 1000 positions it would occupy a completely different memory space than if he wanted 15, that is why you need to ask for memory during the execution of the program and that is why you should use malloc

    int tamanio;
    int * comienzo;
    
    printf ("Ingresé tamaño de array");
    scanf ("%d",&tamanio);
    
    comienzo = (int*) malloc (sizeof (int)*tamanio);
    

    In that small example, start would be the memory address where your array starts and you should be able to use it to manipulate the array.

        
    answered by 19.10.2016 в 01:05