Receive user vector in C

Asked

Viewed 594 times

1

Hello, everyone. I need help getting an array of integers (separated by spaces) from the user. Here’s my code:

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

/*Observacoes:
1, se a ordem nao e decrescente
-1, se a ordem nao e crescente
0, se nao ha ordem
99 se for constante*/

//declaracao de variaveis globais
int t; //tamanho do vetor
int* v; //ponteiro para vetor de inteiros

//prototipo funcao
int verificaOrdem(int* v);

int main(){
  //Receber vetor de tamanho 4 como input

  int rc = verificaOrdem(v); //chamada de funcao
  printf("\n Retorno da funcao verificaOrdem: %d \n", rc);

  return 0;  /*Isso serve para informar ao compilador que ocorreu tudo certo com a função main(). Se main retornasse um outro valor diferente de 0 haveria um problema em sua execução, que seria informada ao compilador*/
}

int verificaOrdem(int* v){
  bool aumentando = false;
  bool diminuindo = false;
  for(int i = 0; i < 4; ++i ){
    if(v[i] > v[i+1]) diminuindo = true;
    if(v[i] < v[i+1]) aumentando = true;
    if(aumentando && diminuindo) return 0;
  }
  if(aumentando) return 1;
  if(diminuindo) return -1;
  return 99;
}
  • To read n digits, you need to know n. Otherwise, if you are doing something event-oriented (for example, until you finish the file/streaming data), you capture the event and say you have finished the vector. You can do processing to find out if the vector is increasing or decreasing without needing to store more than two input data and order booleans.

  • Okay, Jefferson, I made the corrections pointed out. My question is to receive a size 4 vector

1 answer

1


That was quite a job to do, but it’s here:

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

/*Observacoes:
1, se a ordem nao e decrescente
-1, se a ordem nao e crescente
0, se nao ha ordem
99 se for constante*/

void separarInteiros(int *destino, int tamanho, char *entrada);
int verificaOrdem(int* v, int tamanho);

int main() {
  int v[4];

  char entrada[100];
  fgets(entrada, 100, stdin);
  separarInteiros(v, 4, entrada);
  int rc = verificaOrdem(v, 4);
  printf("\n Retorno da funcao verificaOrdem: %d \n", rc);

  return 0;
}

void separarInteiros(int *destino, int tamanho, char *entrada) {
  char *resto = entrada;
  for (int contador = 0; contador < tamanho; contador++) {
    int final = 0;
    while (1) {
      if (resto[final] == '\0' && final == 0) return;
      if (resto[final] == ' ' || resto[final] == '\0') {
        resto[final] = '\0';
        if (final != 0) destino[contador] = atoi(resto);
        resto = &(resto[final + 1]);
        if (final != 0) {
          final = 0;
          break;
        }
      } else {
        final++;
      }
    }
  }
}

int verificaOrdem(int* v, int tamanho) {
  bool aumentando = false;
  bool diminuindo = false;
  for (int i = 0; i < tamanho - 1; ++i) {
    if (v[i] > v[i + 1]) diminuindo = true;
    if (v[i] < v[i + 1]) aumentando = true;
    if (aumentando && diminuindo) return 0;
  }
  if (aumentando) return 1;
  if (diminuindo) return -1;
  return 99;
}

To read the input, I used the function fgets.

The function separarInteiros is responsible for making the magic happen. The for runs from 0 to 3, each iteration to mount a number. The while goes through the letters until finding an empty space, replaces it with \0, isolating a word from the rest of the input. Then, the function atoi is used to convert the word into a number and the analysis follows the rest of the input.

There was a bug in his function verificaOrdem. It ran items from 0 to 3. This means that item 3 would be compared to item 4, which is already outside the array. The solution is to go from 0 to 3 (or in the case of the code I posted, from 0 to 0 tamanho - 1).

  • Thank you, Victor Stafusa. Can you explain to me better this function separarInteiros? I didn’t understand this variable size. Size of what? I would also like to understand the rest pointer

  • 1

    The resto is the "rest of the entrance". The while works by chopping the string into pieces replacing the spaces by '\0'. At each iteration, it is chopped into two, one containing the number to be analyzed and the other containing the rest of the input. This tamanho is the size of the array represented by the pointer destino.

Browser other questions tagged

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