Máquina Dobradora

Asked

Viewed 54 times

1

I’m studying for a programming challenge at College and my teacher gave some programming exercises to train. It was proposed the folding machine, but I am not able to develop the input process, in which the person assigns various values for the machine to perform the processing. If anyone can help me here’s the link of the test.

The section I’m having difficulty with is this: "The next line will contain N integers v1, . . . , vn , corresponding to the content of the input tape". The difficulty I have is to manipulate the input data and throw it into a vector, each value being in a position.

The excerpt of the code that 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:

inserir a descrição da imagem aqui

1 answer

0

You have two options:

  • Creates a loop/loop and asks the user to enter a value for each iteration and stores it directly in the array; or
  • Asks the user to type the values in a single line, separated by a space. Using the Strtok function extracts the numbers for an array.

I leave here the implementation of the second alternative (the first, simpler, is as exercise)

Using your code as a basis:

#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 user input is performed (for example, validate that user entered an integer and not just garbage)

  • Thank you for the help Rune, I did the Lasso to ask for numbers individually, but I think it would come out of the proposed, I would need the second option you mentioned. I will test soon less, and mark here your question as accepted or not Ok. thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.