Pointer use

Asked

Viewed 119 times

1

I need to use pointers, but I’m not sure how to express the syntax, it’s part of the goal of the task (change the value of the parameters passed by reference to the functions, such that these variables are printed in the main().

 #include <stdio.h>
 #define MAX 3

 void busca_menor( float *notas, float tam)
{
int i;
float m, valor;
m=notas[0];

for(i=0; i<tam; i++)
{
    if (notas[i]<m)
    {
        m=notas[i];
    }
}
 }

 int main()
{

int i;
float notas[MAX], valor;
printf("Informe a nota de 3 alunos: ");

for (i=0; i<MAX; i++)
{
    scanf("%f", &notas[i]);
}

busca_menor(notas, MAX);
printf(" A menor nota foi: ");
printf("%2.f", MAX);

}

  • What is the error shown?

  • It does not return the lowest value among those informed by the user.

  • You said you don’t even compile, so you make a mistake.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

I’ve said a few times that organizing the code helps you understand what’s going on. Besides being difficult to understand, meaningless names make it difficult to know where you want to go. Also, the code shown has nothing to do with pointers. You are using array.

#include <stdio.h>
#define MAX 3
float menor_nota(float valor[MAX]) {
    float menor = valor[0];
    for (int i = 1; i < MAX; i++) if (valor[i] < menor) menor = valor[i];
    return menor;
 }

int main() {
    float notas[MAX];
    printf("Informe a nota dos 30 alunos: ");
    for (int i = 0; i < MAX; i++) scanf("%f", &notas[i]);
    printf("\n A menor nota foi: %2.2f", menor_nota(notas));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you wanted to use pointers, you should do this from the beginning. You could even use pointer in the parameter, but it makes no sense in this case. If you want to use it, you can, It’s not wrong, but it’s not appropriate in this case. Still the errors are not related to pointers, they are errors of syntax, carelessness and algorithm.

This way it makes more sense to use pointer.

Read more on the subject.

Browser other questions tagged

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