Folding Machine

1

I'm studying for a college programming challenge and my teacher gave some programming exercises to train. The bending machine was proposed, but I am not able to develop the input process, in which the person assigns several values for the machine to perform the processing. If anyone can help me here is the link of the test.

The snippet I'm having is this: "The next line will contain N integers v1, ..., vN, corresponding to the contents of the input tape". The difficulty I have is to manipulate the input data and throw them into a vector, each value being in a position.

The snippet of code I've been able to do so far is this:

#include <stdio.h>

#include <stdlib.h>

main() {

 int tamanho =0, i=0;


 printf("Qual o tamanho da Fita?\n");
 scanf("%i", &tamanho);

char variaveis[tamanho];

    printf("Insira os %i numeros", tamanho );
    scanf("%i", &variaveis[i]);

//  for(i=1; i<=tamanho; i++){
//      printf("Insira numero na posicao %i ", i );
//      scanf("%i", &variaveis[i]);
//      }

    for(i = 1; i<=tamanho; i++){
    printf("Os valores digitados foram: %i\n", variaveis[i]);   
    }
return 0;
}

The program will basically work like this:

    
asked by anonymous 07.05.2017 / 13:51

1 answer

0

You have two options:

  • Creates a loop / loop and prompts the user to enter a value at each iteration and save directly to the array; or
  • Prompts the user to enter the values on a single line, separated by a space. Using the strtok function extracts the numbers into an array.

I leave here the implementation of the second alternative (the first, simpler, stands for exercise)

Using your code as a base:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Esta função "parte" a linha passada na variável entrada e tenta obter um máximo de numEntradas. Os números extraidos são guardados no array saida
*/
int lerNumeros(int numEntradas, char *entrada, int saida[])
{
    int numAux;
    char *numero = strtok(entrada, " ");

    int i = 0;
    while (numero != NULL && i < numEntradas)
    {
        sscanf(numero, "%i", &numAux);
        saida[i++] = numAux;

        numero = strtok(NULL, " ");
    }
    return numEntradas == i;
}

int main()
{
    static const int TAMANHO_MAXIMO = 15;
    static const int ENTRADA_MAXIMA = 1000;

    int tamanho = 0;
    char entrada[ENTRADA_MAXIMA];
    int fita[TAMANHO_MAXIMO];

    //pedir o tamanho da fita;
    printf("Qual o tamanho da Fita? ");
    fgets(entrada, sizeof(int), stdin);
    sscanf(entrada, " %d", &tamanho);

    //pedir numeros ao utilizador
    int sucesso = 0;
    do
    {
        printf("Insira na mesma linha, separados por espaços, os %i numeros : ", tamanho);
        fgets(entrada, ENTRADA_MAXIMA, stdin);

        sucesso = lerNumeros(tamanho, entrada, fita);

    } while (sucesso == 0);

    //mostrar fita
    for (int i = 0; i < tamanho; i++) {
        printf("Os valores digitados foram: %i\n", fita[i]);
    }
    return 0;
}

Note that the code is incomplete, in particular no validation of the data entered by the user (for example, validate that the user entered an integer and did not only introduce garbage)

    
07.05.2017 / 20:17