How can I re-load a C program if I have set a wrong value or want to run the program several times

Asked

Viewed 27 times

-1

I’m doing a program in c that calculates the root of a function but sometimes I get the wrong values (e.g. 33.3333 instead of 3.333333) and I don’t want to run the program with the wrong values or exit and re-enter, so doing a program Restart or Reload would be the solution, would also be a way to run the program several times.

#include <stdio.h>
#include <math.h>
float main()
{
     char let = 'a';
     int b;
     float x0, erro, gx, x1,dif, gxlinha, absgxlinha;
     int it,val,i, exp, numero, e;
     it=0;
     int num = 0;

     printf("Valor de x0 = ");
     scanf("%f", &x0);

     printf("Valor do erro = ");
     scanf("%f", &erro);

     printf("Qual o valor do expoente maior =  ");
     scanf("%d", &exp);
     printf("\n");
     val = (exp + 1);

     printf("funcao do tipo:  \n\n    ");

     e = exp - 1;
     numero = exp;

     for(b=0;b<=e;b++)
     {
          printf("%cx^%d + ",let,numero);
          let++;
          numero--;
     }

     printf("%cx^%d",let,numero);
     printf("\n\n");

     float a[val];
     printf("Introduza os %d numeros da funcao, de acordo com os valores pedidos: \n",val); // ex.: ax^2 + bx^1 + cx^0 itroduzir pela ordem c, b, a
     printf("\n");

     for( i=0; i<val; i++ )
     {
          printf ("%c = ",let);
          let--;
          scanf("%f", &a[i] );
          gx = gx + (a[i])*(pow((x0),num));
          num++;
     }
     num = 1;

     for (i=1; i<val; i++)
     {
          gxlinha = gxlinha + ((a[i]*num)*(pow((x0),num)));
          num ++;
     }

     absgxlinha = (sqrt(pow(gxlinha,2)));

     if ( absgxlinha < 1)
     {
          printf ("\ng(x0)' < 1 logo a funcao tem pelo menos uma raiz\n");
          x1=gx;
          dif = (sqrt(pow((x1-x0),2)));
          printf("\n| Iter.| Val de x0| g(x0)=x1 |   x1-x0  |\n|      |          |          |          |\n|  %d   | %f | %f | %f |  ",it,x0,x1,dif);

          while (sqrt(pow((x1-x0),2))>=erro)
          {
               x0 = x1;
               gx = 0;
               num = 0;

               for( i=0; i<val; i++ )
               {
                    gx = gx + (a[i])*(pow((x0),num));
                    num++;
               }

               x1=gx;
               dif = (sqrt(pow((x1-x0),2)));
               it++;
               printf(">= %f\n|  %d   | %f | %f | %f |  ",erro,it,x0,x1,dif);
          }
     
          printf("<  %f \n\n",erro);
          printf("A raiz com erro inferior a %f = %f",erro,x1);
          printf("\n\n\n\n");
     }
     else
     {
          printf ("\ng(x0)' > 1 logo a funcao nao tem raizes\n");
     }
}

here are some inputs by order

0.8 , 0.000005 , 3 , 0.333333 , 0 , 0 , -0.333333

1 answer

0

Reload from a single reading

Just leave the part of the code you want to do "Reload" inside a repeat command, that is, a loop. Example:

do
{
    printf("Valor de x0 = ");
    scanf("%f", &x0);

    printf("x0: %f, digite 0 para confirmar", x0);
    scanf("%d", &confirmar);
}while(confirmar != 0);

This command will read a value for x0, will then show the content, from x0, and ask if it really was that number you wanted. After reading your confirmation (0 to confirm or another number to re-read), the condition confirmar != 0 will be evaluated and if true, confirm is non-zero, then the program goes to the beginning of the loop, that is, read again the value of x0.

You can read more than one variable and only then ask:

do
{
    printf("Valor de x0 = ");
    scanf("%f", &x0);

    printf("Valor do erro = ");
    scanf("%f", &erro);

    printf("Qual o valor do expoente maior =  ");
    scanf("%d", &exp);

    // Print dos valores

    scanf("%d", &confirmar);
}while(confirmar != 0);

Reload of the program

To "Reload" the entire program just put all the commands inside a loop.

// Exemplo de um programa que calcula a idade de uma pessoa
#include <stdio.h>

int main(void)
{
    int ano, confirmar;

    do
    {
        printf("Ano de nascimento: ");
        scanf("%d", &ano);

        printf("Idade: %d\n", (2021 - ano));

        printf("Digite 1 para saber a idade de outra pessoa: ");
        scanf("%d", &confirmar);
    }while(confirmar == 1);

    return 0;
}

Using the same logic we can do this in your program, it would look something like this:

do
{
    // Leitura das variaveis

    // Calculos

    // Print dos resultados

    // Etc...

    printf("Digite 1 para calcular a raiz de outra funcao: ");
    scanf("%d", &confirmar);
}while(confirmar == 1);

Browser other questions tagged

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