Doubt Exercise - Academic

Asked

Viewed 59 times

0

My question is in the letter B, I am asked to count between the first 20 terms, how do I do this limitation of 20 terms of the account? I even put the formula in is (i = 5, j = 3; i += 2, j += 2) But it remains to introduce the i e j interval that is my doubt. Thank you

The question follows: Calculate the sum of the following series : a) S = 1 + 1/3 + 1/5 + 1/7 + .........+ 1/99

b) S = 1 - 1/3 + 1/5 - 1/7 + ............ ... for the first 20 terms

int i, j;
double soma = 1, result = 1;
printf ("Somatorio dos termos: S = 1 + 1/3 + 1/5 + 1/7 + ............+ 1/99 \n");
for (i = 3; i <= 99; i += 2) {
    soma = soma + (1.0/i);
}
printf ("a) Soma = %lf \n\n", soma);

for (i = 5, j = 3; ;i += 2, j += 2) {
    result = result - 1.0/j + 1.0/i; 
}
printf ("b) Resultado = %lf \n\n", result);

system ("PAUSE");
  • Thanks for the tips guys, I managed to do otherwise, probably not in the "correct" way but the result of the same. for (k = 5, j = 3, cont = 0; cont < 20 ; k += 2, j += 2, cont++) { result = result - 1.0/j + 1.0/k; } At least it’s the way I can think without bugging my mind

2 answers

0

Try to use the repeat command correctly. For example.:

int i, j;
double soma = 0, result = 1, sinal=1;
printf ("Somatorio dos termos: S = 1 + 1/3 + 1/5 + 1/7 + ............+ 1/99 \n");
for (i = 1; i <= 20; i++) {
    soma +=  1.0/(float) 2*i-1;
}
printf ("a) Soma = %lf \n\n", soma);

for (i = 1; i<=20; i++) {
   result += (float) sinal / (float) 2*i-1; 
    sinal =-sinal;
}
printf ("b) Resultado = %lf \n\n", result);
  • I’ve bugged my mind now

0

for(int count = 0, k = 0; count < 20; count++){
if(count == 0){
    resultado += 1;
}else{
    if(count % 2 == 0){
        resultado -= (float) 1/(3 + k);
        k += 2;
    }else{
        resultado += (float) 1/(3 + k);
        k += 2;
    }

}

count += 1;
}
  • I would bring an explanation along with the code to facilitate understanding of your response.

  • Vlww, I’ll do it next time since the guy already got it sorted.

Browser other questions tagged

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