Exercise to sequence numbers with Struct

Asked

Viewed 55 times

-2

I’m doing an exercise to sequence numbers using a struct, but what’s happening is that in my for the variable i is being incremented in a very strange way, in the second passage of scanf, its value becomes 104352, instead of becoming 1... I can’t understand why, but I imagine it’s the way I stated numeros[] in struct

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

typedef struct{
    int qtdeNumeros;
    int qtdeRepetidos;
    float numeros[];
}Sequencia;


int main()
{

    Sequencia sequencia;

    printf("Quantos numeros deseja colcoar na sequencia? ");
    scanf("%d", &sequencia.qtdeNumeros);

    for(int i=0; i < sequencia.qtdeNumeros; i++){
        printf("Digite o numero [%d]: ", i+1);
        scanf("%f", &sequencia.numeros[i]);
    }

    for(int i = 0; i<sequencia.qtdeNumeros; i++){
        printf("Numeros [%d]: %f\n", i+1, sequencia.numeros[i]);
    }
    return 0;
}
  • You wouldn’t need to initialize sequencia?

  • Initialize in what sense?

  • like this: Sequencia *sequencia;&#xA;sequencia = malloc(sizeof(Sequencia));

  • 1

    I even understood what you meant, but we can not use malloc since it was not a concept learned

  • So there’s no way to fix it this way.

2 answers

1

Try it like this:

typedef struct{
    int qtdeNumeros;
    int qtdeRepetidos;
    float *numeros;
}Sequencia;


printf("Quantos numeros deseja colcoar na sequencia? ");
scanf("%d", &sequencia.qtdeNumeros);
sequencia.numeros = (float *) malloc(sequencia.qtdeNumeros * sizeof(float));

If you cannot use dynamic allocation declare the array numbers with an arbitrarily large number, e.g..: float numeros[10000]; and test whether the quantity read is not greater than that declared.

1


The way it’s set I don’t think you can do it.

Following the "anonymous" tip, what you can do is put a maximum size default in the floats array.

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

#define ARRAY_SIZE 50

typedef struct
{
  int qtdeNumeros;
  int qtdeRepetidos;
  float numeros[ARRAY_SIZE];
} Sequencia;

int main(void)
{
  int n;
  Sequencia sequencia;

  for (;;)
  {
    printf("*\n");
    printf("* quantos numeros deseja colocar na sequencia? ");
    n = scanf("%d", &sequencia.qtdeNumeros);
    if (n != 1)
    {
      printf("* erro na entrada de dados, tente novamente...\n");
      continue;
    }
    if (sequencia.qtdeNumeros > ARRAY_SIZE)
    {
      printf("* numero muito grande, tente novamente...\n");
      continue;
    }
    break;
  }

  for (int i=0; i < sequencia.qtdeNumeros; i++)
  {
    printf("* digite o numero [%d]: ", i+1);
    for (;;)
    {
      n = scanf("%f", &sequencia.numeros[i]);
      if (n != 1)
      {
        printf("* erro na entrada de dados, tente novamente...\n");
        continue;
      }
      break;
    }
  }

  for (int i = 0; i<sequencia.qtdeNumeros; i++)
    printf("* numeros [%d]: %f\n", i+1, sequencia.numeros[i]);

  return 0;
}  
  • Thank you! The solution gave a great help, I ended up solving in another way a little simpler.

  • it would be interesting you also put an answer with the solution you used

  • is that the answer I used would totally modify the type of structure, I don’t know if it would add much, and as I did the exercise during the list, I overwrote it with another code :(

Browser other questions tagged

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