Function to sum elements of an array does not return correct value

Asked

Viewed 1,747 times

4

I am making a program for a shopping list, the user passes to the program the values of each product and each one is stored in a vector. Then we added the elements of the vector to give the total value of the purchase, so I created a function:

float valor(float *k, int *t){
int i;
float soma = 0;
for(i = 0; i<*t; i++){
    soma += k[i];
   }
return(soma);
}

And in the main, I’ll call the program;

teste = valor(&vTotal, &quant);
printf("Valor total: %f", teste);

The argument vTotal is the vector where are the values of each product given by the user and Quant is the amount of vector items.

The printf is shows 0.00000 on the screen, now printi the value of the vector directly on the screen and being correctly allocated in the vector, but I don’t know why the sum is zeroing.

Full example:

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

int main()
{
float *vTotal;
int quant;
int i;
int teste = 0;

printf("Digite a quantidade: ");
scanf("%d", &quant);

vTotal = aloca(&quant);

for(i = 0; i<quant; i++){
    printf("Digite o valor %d: ", i+1);
    scanf("%f", &vTotal[i]);
}
    teste = valor(&vTotal, &quant);
    printf("\n %.2f", teste);
    free(vTotal);
}

"ghe. h"

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

float *aloca(int *num);
float valor(float *k, int *t);

float valor(float *k, int *t){
int i;
float soma = 0;
for(i = 0; i<*t; i++){
soma += k[i];
   }
return(soma);
}

float *aloca(int *num){
float *u;
u = malloc(*(num)*sizeof(float));
return(u);
}

1 answer

3


The code had several problems, even to compile I had to mess with a lot of things. I ended up getting the final result that worked. I can not say what was the problem reported in the question, in theory I have not changed any logic, only fixed what prevented the compilation. There’s a chance there’s a conflict with the function aloca() that exists in C. I did in a way that I consider simpler and more organized trying to maintain the intention.

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

float valor(float *k, int t){
    float soma = 0;
    for (int i = 0; i < t; i++){
        soma += k[i];
    }
    return soma;
}

int main() {
    int quant;
    printf("Digite a quantidade: ");
    scanf("%d", &quant);
    float *vTotal = malloc(quant * sizeof(float));
    for(int i = 0; i < quant; i++){
        printf("Digite o valor %d: ", i + 1);
        scanf("%f", &vTotal[i]);
    }
    float teste = valor(vTotal, quant);
    printf("\n %.2f", teste);
    free(vTotal);
}

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

  • Man, thank you so much. I’m starting now with dynamic allocation and I’m still kind of lost with pointers, thanks anyway. Just a question. For functions that I declare one of the arguments as a pointer, it is not necessary to use &? After this your answer, I realized that when I do the value(&Vtotal, &Quant), gives zero, but when I take &, the function returns the correct value

  • When the parameter expects a pointer and the argument passed is already a pointer it cannot use the operator & to pass. The second I took off because it doesn’t need to be a pointer to anything.

Browser other questions tagged

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