How to make Fibonacci sequence through matrix?

Asked

Viewed 117 times

-2

I’m developing a C algorithm that displays a Fibonacci sequence on the screen, but first I built a spreadsheet in Excel to understand how this sequence works and realized that it is a 3 column array, in which the value of a column, when passed to the bottom row returns to the previous column. As an example in cell C4 the value is 3, and this same value 3 goes in the future to cell B5. Would it be possible to create this sequence through matrix? How could I do it? Because I’ve tried everything including adding together the elements of different columns and so display them in sequence and it didn’t work.Tabela da sequência Fibonacci

1 answer

0


I’m not sure I quite understood what you wanted, but follow an example.

#include <stdio.h>
#define TAM 5
#define COLUNAS 3
#define COL1 0
#define COL2 1
#define COL3 2
void main(){
   int matriz[TAM][COLUNAS];
   int col1, col2,inc;
   col1=0;
   inc=0;
   while(inc<TAM){
       if(inc==0){
           col2=1;
           col1 = col2;
       }
       else{
           col1 = col2;
           col2=matriz[inc-1][COL3];
       }
       matriz[inc][COL1]=col1;
       matriz[inc][COL2]=col2;
       matriz[inc][COL3]= col1 + col2;
       inc+=1;
   }
   printf("Registrado %d elementos\n", inc);
   puts("Imprimindo a Matriz sequencia de Fibonacci\n\n");
   printf("ELEM 1\tELEM 2\tRESULT\n");
   for (inc = 0 ; inc <TAM ; inc++)
    printf("%6d\t%6d\t%6d\n",  matriz[inc][COL1], matriz[inc][COL2], matriz[inc][COL3]  );  
}
  • Exactly was more or less this idea that I wanted, vlw by help. I will use as a basis to change some things in it, already broke a branch.

  • Do not use greetings or greetings, see []

Browser other questions tagged

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