AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
119 stars 9 forks source link

Fibonacci Error Code #62

Closed Spoiledpay closed 2 years ago

Spoiledpay commented 2 years ago

I tried this code and it works, but it generates an error from the output.

import basics

//pragma ignore_unused

func main(in argc int, in argv **ubyte) int { n1 int = 0 n2 int = 1 n3 int i int number int

 printf("Enter the number of elements:")    
 scanf('%d',&number)    

     printf("\n%d %d",n1,n2); //printing 0 and 1 
 printf('\n%d', number)
for(i = 2; i < number; i++) //loop starts from 2 because 0 and 1 are already printed    
{    
  n3 = n1 + n2   
  printf(' %d',n3)    
  n1 = n2    
  n2 = n3;   
}  

return 0

}

output:

C:\Adept\Example\Fibonacci>fibo.exe Enter the number of elements:4

0 1 4 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 -1323752223 512559680 -811192543 -298632863 -1109825406 -1408458269 1776683621 368225352 2144908973 -1781832971 363076002 -1418756969 -1055680967 1820529360 764848393

IsaacShelton commented 2 years ago

This is a bug with for loops in Adept 2.4 like as mentioned in #61 which is fixed in version 2.5.

In Adept 2.5 it outputs:

Enter the number of elements:4

0 1
4 1 2

In you don't want to update to 2.5 yet, the same effect can be achieved by doing something like:

import basics

func main(in argc int, in argv **ubyte) int {
    n1 int = 0
    n2 int = 1
    n3 int
    i int
    number int

    printf("Enter the number of elements:")    
    scanf('%d', &number)    

    printf("\n%d %d",n1,n2) //printing 0 and 1 
    printf('\n%d', number)

    i = 2
    while i <= number //loop starts from 2 because 0 and 1 are already printed    
    {
        defer i++

        n3 = n1 + n2
        printf(' %d', n3)
        n1 = n2
        n2 = n3
    }

    return 0
}

or like this:

import basics

func main(in argc int, in argv **ubyte) int {
    n1 int = 0
    n2 int = 1
    n3 int
    i int
    number int

    printf("Enter the number of elements:")    
    scanf('%d', &number)    

    printf("\n%d %d",n1,n2) //printing 0 and 1 
    printf('\n%d', number)

    repeat number - 2 // loop starts from 2 because 0 and 1 are already printed    
    {
        i = idx + 2
        n3 = n1 + n2
        printf(' %d', n3)
        n1 = n2
        n2 = n3
    }

    return 0
}

You can find more information about repeat loops here: Repeat Loops Documentation