save Fibonacci sequence in C

Asked

Viewed 148 times

0

I need to do a program that keeps the first 100 numbers of the Fibonacci sequence on file. I was able to make the sequence right, but at the time of the program save the file appears only the number "695895453".

How do I save all the numbers?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

 int main() {
    setlocale (LC_ALL,"portuguese");
    FILE * teste;

    int a, b, aux, i;

    a = 1;
    b = 0;

    for(i = 0; i < 100; i++)
    {
        aux = a + b;
        a = b;
        b = aux;

        printf(" %d ,", aux);
    }


    if((teste = fopen("teste.txt","w")) == NULL)
    {
        printf("Erro de abertura! \n");
    }

    else
    {
        for(i = 0; i < 64; i++)
        {
            aux = a + b;
            a = b;
            b = aux;

            fprintf(teste,"%d" ,aux);
            fclose(teste);
        }


        return 0;
    }
}
  • You first print the first 64 members of the Fibonacci sequence and then record the 64 members following sequence. If you want to save the first 64, reset the variables a and b.

  • It should actually be a = 0; and b = 1; and these are the first two elements of the sequence and therefore should be recorded first and then recorded the next 62 elements.

2 answers

0


You are not specifying the creation of a new line. Includes the value \n in instruction fprintf to create a new line.

(...)
fprintf(teste,"%d\n" ,aux);
(...)

Note, at another point: You should only close the file after finishing the writing process. The instruction fclose(teste) shall be placed outside the cycle for.

With these changes in your code, on my machine (not Windows) works without pimples.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main() {
    setlocale (LC_ALL,"portuguese");
    FILE * teste;

    long a, b, aux, i;

    a = 1;
    b = 0;

    if((teste = fopen("teste.txt","w")) == NULL) {
        printf("Erro de abertura! \n");
        exit(1);
    }

    for(i = 0; i < 64; i++) {
        aux = a + b;
        a = b;
        b = aux;

        fprintf(teste,"%ld\n" ,aux);
        printf(" %ld ,", aux);
    }

    fclose(teste);
    exit(0);
}
  • Didn’t work, keeps printing only the number "695895453" in the file.

  • Windows or Linux?

  • Windows. I put fclose(test) out of the loop but now it’s printing negative numbers and not the full sequence of Fibonacci. Is this junk stored in the program?

  • For windows the string is \r\n.

  • I changed and it still didn’t work.

  • On the one hand you are not restarting the variavies a and b. So the file is registering not the first 64 numbers in the sequence but the second. The "trash" comes from the size of the number. The sequence grows very quickly and in a few iterations you surpass the maxInt. Try switching to "long" and printf and fprints %d for %ld.

  • It still didn’t work.

  • Now yes! Thank you very much!

Show 3 more comments

0

Your program is not good. Have a book about C? Thought about it? Read some table sore the sequence?

  • Had you thought about it and looked for a table and would you see for example in The first 100 numbers in the Fibonacci sequence that the last number you need is 10610209857723 which has 14 digits
  • And so you wouldn’t be surprised to see negative values using a int whose maximum is well known: INT_MAX = +2147483647 that has only 10 digits. And if you think about it you will also see that the sequence starts in 1 then all the numbers are positive and there’s no point in using int that has signal
  • avoid this (common) type of construction
 if((teste = fopen("teste.txt","w")) == NULL)
    {
        printf("Erro de abertura! \n");
    }

    else
    {
        for(i = 0; i < 64; i++)
        {
            aux = a + b;
            a = b;
            b = aux;

            fprintf(teste,"%d" ,aux);
            fclose(teste);
        }
        return 0;
    }
  • Note that the return is within the clause of if() and not outside, and if the fopen() fail the program does not go through return.
  • understand that if you cannot open the file you have no reason to continue with the program, since the goal is to write the file. Do the simple and return.
  • when generating the numbers record in the file at the same time or you will lose the values you did not save. Of course you can restart a and b and use another loop but would be unintelligent
  • declare control variables INSIDE the loop. It took many years for the committee to fix this in the language, but it has been 30 years. It is very important to yourself to reduce the scope --- Scope, life --- variables. especially ones with simple names like a,b and aux.

An example

Copying from your code

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

 int main()
 {
    setlocale (LC_ALL,"portuguese");
    FILE * teste = fopen("teste.txt","w");
    if( teste == NULL)
    {   printf("Erro de abertura! \n");
        return 0;
    }

    unsigned long long a = 1;
    unsigned long long  b = 0;

    for(int i = 0; i < 64; i++)
    {
        unsigned long long aux = a + b;
        a = b;
        b = aux;
        printf("%4i%15llu\n", 1+i,aux);
        fprintf(teste, "%15llu\n", aux);
    };  // for()
    fclose(teste);
    return 0;
};  // main()

Show on screen

   1              1
   2              1
   3              2
   4              3
   5              5
   ...
  62  4052739537881
  63  6557470319842
  64 10610209857723

And write to disk one number per line

              1
              1
              2
              3
              5
...
  4052739537881
  6557470319842
 10610209857723

Browser other questions tagged

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