Divide sum of values of a vector

Asked

Viewed 161 times

-2

I’m trying to divide/multiply all the numbers between themselves using vectors.

for example: type 4 numbers and divide them all: 5/5/2/1 but I can’t get the division. follow the program:

float numeros[4];
float resultado;
float soma;

printf("digite 4 numeros: \n");

for(int i = 0; i < 4; i++) {
    scanf("%f", &numeros[i]);
}

for(int i = 0; i < 4; i++) {
    soma += numeros[i];
    resultado = soma / numeros[i];
}
  • I didn’t understand very well what you want to calculate, even it doesn’t match the description, but you didn’t assign an initial value to the variable soma and so it’s leaving from memory junk.

  • i want to divide more than one number at once. For example: the program asks for 3 numbers and divide them, type 4 / 3 / 2

  • I don’t know if it’s possible that way, I wanted to know

  • At the same time it is not possible, consecutively it is possible, first 4/3 after the result divided by 2 and so on. This can be done in the same operation resultado = 4 / 3 / 2; which is equivalent to resultado = (4 / 3) / 2;. Not considering that you might be thinking of a program with different threads doing the divisions in parallel.

  • got it. Thank you very much friend

1 answer

0

next time post the code itself and not an image

I do not know if I understood very well but for example you receive the number 10,2,3,5 and that divide them in order ( ( ( 10/2 )/3 )/5 ).... that’s it ?

if there are any problems ....

1º in c the variables must be initialized with a value to avoid memory junk or

float resultado = 0;
float soma = 0;

the second problem is that your logic is wrong there is a base case that is the first number and it is not divisible by anything, ie when you get the first number that in the example would be 10 you do not divide it by anything and would be like this

 resultado = numero[0];

from the second number you begin to divide

for( var i=1 ; i<4 ; i++){
   resultado = (resultado/numero[i]);
}

NOTE: it’s been a long time since I’ve programmed in c but the logic is this and I’m sorry for syntax errors

  • it was very clear your explanation friend!

Browser other questions tagged

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