What is the mistake in logic?

Asked

Viewed 96 times

-4

Given the values of x real and n natural positive, calculate:

inserir a descrição da imagem aqui

#include <stdio.h>

int main(){

    float x = 0.0;
    int n = 0;
    int soma = 0;
    int fat = 1;

    printf("Digite um valor:");
    scanf("%f", &x);
    printf("Digite a quantidade de operações:");
    scanf("%d", &n);

    for(int i = 1; i <= n; i++){
        soma = (x + i)/fat;
        fat = fat * i;
}
    printf("A soma n pra S = x+n/n!\nÉ: %d", soma);
    return 0;
}
  • Question: does the sum have only 4 terms even or should all terms between 3 and n be summed as well? The way you’ve posted jumps from 3 to n.

  • 1

    And it shouldn’t be soma += (x + i)/fat? The way you override the value with each iteration.

  • The sum of the terms starts from 1 to n which is the number of terms chosen for example 5, went to 3 to show how it would be from the beginning of the calculation

  • Anderson applied the attribution operator by addition that was missing, but still continues with a wrong result

  • 1

    Marvin, are you sure soma will always be an integer?

  • I already changed the type to accept floats values, thank you!

Show 1 more comment

2 answers

1

Three things:

  1. You overwrite the value of soma every iteration of the loop;
  2. You define soma as a whole;
  3. You must first multiply the fat, then add up;

To correct (1), you will need to change soma = for soma +=. Already to fix the (2) just set soma as float. Item (3) needs to be done because otherwise you will have 2 times the denominator equal to 1. fat starts at 1, generating the term (x+1)/1! , then it is multiplied by i which is also 1, generating in the next iteration (x+2)/1!. In the next iteration it would be (x+3)/2! , etc. That is, the result would be different than expected.

#include <stdio.h>

int main(){

    float x = 0.0;
    int n = 0;
    float soma = 0;
    int fat = 1;

    printf("Digite um valor:");
    scanf("%f", &x);
    printf("Digite a quantidade de operações:");
    scanf("%d", &n);

    for(int i = 1; i <= n; i++){
        fat = fat * i;
        soma += (x + i)/fat;
    }

    printf("A soma n pra S = x+n/n!\nÉ: %f", soma);
    return 0;
}
  • In vdd the fat already starts with 1, so it would be necessary to just put signal assignment for addition in fat also.

  • But putting fat += after the sum the result is the same, it only includes the other fat previously calculated

0

Well, there’s my answer, the result is as expected

#include <stdio.h>
int main(){

    float x = 0.0;
    int n = 0;
    float soma = 0;
    int fat = 1;

    printf("Digite um valor:");
    scanf("%f", &x);
    printf("Digite a quantidade de operações:");
    scanf("%d", &n);

    for(int i = 1; i <= n; i++){

        soma +=(x + i)/fat;
        fat += fat * i;
    }

     printf("A soma de %.2f como x pra S = x+n/n!\nÉ: %.2f",x, soma);
return 0;
}
  • Friend the %.2f round the value up, just take it out and you’ll see the unwrapped result

Browser other questions tagged

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