A vector in C, which updates the values in real time, according to the numbers reported

Asked

Viewed 307 times

0

#include<stdio.h>
#include<stdlib.h>
#define tamanho 5

struct test {
  int vetor5;
}casa[tamanho];

//funcão para o vetor da struct
void recebe (void) {
  for (int p=0; p<tamanho; p++) {
    printf("| %d",casa[p].vetor5);
  }
}

int main (){
  //INICIALIZANDO VETOR DA STRUCT
  for (int i=0; i<tamanho; i++) {
    casa[i].vetor5=0;
  }

  recebe(); 
  for(int y=0 ; y<tamanho; y++) {
    //insiro os valores
    printf("\nInsira um novo valor para a struct:");                                                                     
    scanf("%d", &casa[y].vetor5);
  }

  return 0;
}
  • The problem is unclear. You want to dynamically increase/decrease the vector size?

  • The size remains the same. It would be like the vector is all 0. When I enter any number, it is updated in real time. Going to display the new number informed, along with the others.

  • I don’t know if I understood but isn’t it just a matter of printing (via function receives) the vector after the vector update? In fact, it’s a strange name for a function that only prints.

  • Maybe it doesn’t even have to be via function. But what I’m trying to do is that the vector, it stays on display while informing the new entered data.

  • 1

    can’t understand what you want...

1 answer

0


If I understand correctly, you want to show the current state of the vector and, as soon as you insert a new value, the terminal of a Reload and already shows the vector with the new value inserted.

If so, just pass the function call recebe(); into the for, by placing before the call a function that cleans the terminal each time you print on the screen, thus:

for(int y=0 ; y<tamanho; y++) {
  //insiro os valores
  system("cls"); //caso use windows, função que limpa a tela do terminal
  recebe();
  printf("\n\nInsira um novo valor para a struct: ");
  scanf("%d", &casa[y].vetor5);
}

As it stands above, the programme will close as soon as the number of items is fully filled in. If you want it to be infinite, you must implement a chained vector, where your storage limit will be the RAM of the PC. Or you can also put this one for which is on top within another loop, hence triggers a stop condition and a way for the counter to be reset when reaching the final size (5).

Browser other questions tagged

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