Program stops reading before the for and "hangs" during the run (C)

Asked

Viewed 43 times

-3

I am performing this exercise in C which is a challenge for college. No note is worth. The purpose is to use the for and find pi. The statement is in the body. For some reason, when I give run in the program, it only stops before reading the for And it stays there. There’s no fatal error, nothing. It just doesn’t keep going. I ran it through both Codeblocks and an online compiler to see what the problem was. I took a look at the go’s command a few times, but I couldn’t identify the problem.

The code:

*#include <stdio.h>
#include <math.h>
#include <locale.h>*

int main (){

    int i, vezes, n_termos;
    float pi;
    float soma;

    setlocale(LC_ALL, "Portuguese");

    printf("Este programa utiliza a fórmula de Leibniz para calcular a constante pi:\n\n");

    printf("        1         1         1         1        1        1              pi \n");
    printf("       ---   -   ---   +   ---   -   ---   +  ---   -  ---  +  ... = -----\n");
    printf("        1         3         5         7        9        11             4   \n\n");

    printf("Para tal, insira um número de termos a serem considerados. O programa fica \n");
    printf("          mais preciso conforme mais termos são inseridos!                 \n");

    printf("\n\n///////////////////////////////////////////////////////////////////////////////\n\n");

    printf("NÚMERO DE TERMOS = ");
    scanf("%d", &n_termos);

    soma = 1;
    vezes = 0;

    for (vezes; vezes = n_termos - 1; vezes + 1){

        if (vezes % 2 != 0){
        i = (3 * vezes) + 2;
        soma = soma - (1/i);}

        if (vezes % 2 == 0){ 
        i = (3 * vezes) + 2;
        soma = soma + (1/i);}
}

    pi = 4 * soma;

    printf("\nA constante pi é aproximadamente %f\n", pi);

    return 0;
}

Anyone have any suggestions what to do? Thank you!

  • vezes = n_termos - 1 returns n_termos - 1, which is true unless n_termos is equal to 1. Replace with vezes < n_termos. And vezes + 1, replace with vezes++.

  • @Vandersantos made the replacement, thank you!

2 answers

2

In the second argument of for the variable vezes should be expressed in: vezes<n_termos or vezes <= n_termos-1, because that is the stopping criterion when you increase the vezes in the last parameter for that variable to reach the amount of loop terms.

  • All right, I’ll make it right!

0

Good Afternoon. I think I just fix this part below

vezes = n_termos - 1

within the first clause of for, you have the initialization criterion (this can be i=1 or anything or until it is empty if you have already initialized your variable)

within the second, is the stopping criterion, this must have a boolean expression or return a boolean

IN THE ABOVE CASE, YOU USE AN ASSIGNMENT SQUEEZE, WHICH DOES NOT RETURN VALUE FOR THE CLAUSE, THAT IS, IT ALWAYS STAYS FALSE LOCKING IN A LOOP, IN SOME CASES NOR RUNS EVEN WITHOUT SHOWING ERROR IN THE COMPILATION OR EXECUTION, THIS USUALLY HAPPENS MAINLY IN LANGUAGE C

IT IS IMPORTANT TO BE CAREFUL TO USE ONLY ( == , >= , <= , > , < OR !=) TO MAKE COMPARISONS.

and at the last the increment criterion, which rotates at the end of each execution, except for the expression ++(variável) which increments the variable before executing what is inside the for

  • Thanks, I’ll check it out.

Browser other questions tagged

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