Dynamic allocation and use of C pointers

Asked

Viewed 41 times

-1

I am very doubtful about the dynamic allocation and use of C pointers. I need to develop some exercises, but they are stuck because I am having some difficulty moving forward with the use of pointers and dynamic allocation. The following exercise calls for the following:

Make a program that reads a vector of real numbers of size chosen by the user and find out which is the largest and smallest value in the vector, together with its index.

I built it the way down. Could someone point out the error, please?

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

float *alocaVetor(int n)
{
  float *v;
  v = (float *)malloc(n * sizeof(float));
  return v;
}

void leVetor(float *v, int n)
{
  int i;
  for (i = 0; i < n; i++)
  {
    printf("Elemento %d: \n", i);
    scanf("%f", &v[i]);
  }
}

float *comparaVetor(float *v, int n)
{
  float maior = v[0];
  float menor = v[0];

  for (int i = 0; i < n; ++i)
  {
    if (v[i] > maior)
      maior = v[i];
    if (v[i] < menor)
      menor = v[i];
  }
  printf("%f", &maior);
  printf("%f", &menor);

  return v;
}

int main()
{
  int n;
  float *v;
  float *resultado;

  printf("\n Digite o numero de elementos do vetor: \n");
  scanf("%d", &n);
  v = alocaVetor(n);
  printf("\n Digite: \n");
  leVetor(v, n);
  resultado = comparaVetor(v, n);

  free(v);

  return 0;
}
  • It is mandatory to use pointers in this exercise?

  • Give one studied in scope of variables. A variable declared in a function has the scope restricted to this function. In the case of alocaVetor is best to pass the float pointer as the function parameter. return v; in comparaVetor.

1 answer

1

  • Avoid returning void. In general it’s a waste, often a mistake.

  • The printf() of a float does not use & and that’s a mistake.

  • It lacked to follow the indices of the highest and lowest value. And this is another error.

  • It failed to explain better what it is printing in each case. It even jumped line or left a space when printing, were correct printf().

  • declare loop control variables on itself for. Never leave a variable i loose in the middle of the program. It is a time bomb. Since the end of the '80 no longer use this. And before that it was used because it had no option to declare in for. The ISO committee took years to finally include this in the language. But long ago ;) it included. Use. Since '17 for example, in C++, one can declare variables within the clause of if() and of switch() besides the for(), all in the sense of decreasing the scope (life time) of the variables.

  • Prompts in the same row of the question are nicer to read. I suggest changing

  • main() should be the first function of your program. Not the last. You’ll like it when your programs get bigger or when you read a program someone else wrote

this is the output of the program below

Digite o numero de elementos do vetor: 4

Digite: 
Elemento 1 de 4: -12.34
Elemento 2 de 4: 2
Elemento 3 de 4: -3
Elemento 4 de 4: 55
        menor valor:   -12.34 em v[0]
        maior valor:    55.00 em v[3]

the test program. Compare with the original

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


float*      alocaVetor(unsigned);
void        comparaVetor(float*, unsigned);
void        leVetor(float*, unsigned);

int main(void)
{
  unsigned n;
  float *v;
  float *resultado;

  printf("\nDigite o numero de elementos do vetor: ");
  scanf("%d", &n);
  v = alocaVetor(n);
  printf("\nDigite: \n");
  leVetor(v, n);
  comparaVetor(v, n);
  free(v);
  return 0;
}


float*      alocaVetor(unsigned n)
{
  return (float*) malloc (n * sizeof(float));
}


void        comparaVetor(float *v, unsigned n)
{
  float maior_valor = v[0];
  float menor_valor = v[0];
  int indice_do_maior = 0;
  int indice_do_menor = 0;

  for (unsigned i = 0; i < n; ++i)
  {
    if (v[i] > maior_valor)
    {
      maior_valor = v[i];
      indice_do_maior = i;
    }
    else
    {
        if (v[i] < menor_valor)
        {
            menor_valor = v[i];
            indice_do_menor = i;
        }
    };  // if()
  }
  printf("\
  \tmenor valor: %8.2f em v[%d]\n\
  \tmaior valor: %8.2f em v[%d]\n",
    menor_valor, indice_do_menor,
    maior_valor, indice_do_maior );
  return;
}


void        leVetor(float *v, unsigned n)
{
  for (int i = 0; i < n; i++)
  {
    printf("Elemento %d de %d: ", 1+i, n);
    scanf("%f", &v[i]);
  }
  return;
}


/*
/questions/481773/aloca%c3%a7%c3%a3o-din%c3%a2mica-e-uso-de-ponteiros-em-c
*/

Browser other questions tagged

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