This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C programming Fibonacci Question

This is my homework question. I'm still pretty new to programming with C using Keil/uVision. I'm not looking for the direct answers and would like to learn but I have no idea what I am doing wrong or right really. I know a basic background of c++. This is what I have so far for the first part. Any help would be greatly appreciated.

Write a C-program to:
•Automatically generate the Fibonacci sequence numbers up to 233 from an input given by the user on P1 (lower 4 bits).
•Display each number up to the input index value on P2 (e.g. if P1 inputs 00000011, first three numbers of the series will be displayed on P2 with a delay in between) and allow a 250 ms delay for the user to observe the displayed value. Assume a delay sub-routine is available for your use.

------My Code Below------

#include <reg51.h>

void main()
{

sbit T1 = P1;
int fib = 0;
int prev = 0;
T1 = 1; for (int i = 0; i < T1; i++) { while (i == 1) { fib = 1; i++; } i--; fib = fib + prev; prev = fib; }

}

Parents
  • main()
    {
    
    sbit T1 = P1;
    int fib = 0;
    int prev = 0;
    T1 = 1;
    for (int i = 0; i < T1; i++)
    {
      while (i == 1)
      {
        fib = 1;
        i++;
      }
      i--;
      fib = fib + prev;
      prev = fib;
      }
    }
    

    I have made your code readable. your onelining almost made me not respond
    what are you doing with the while?
    it equates

    if (i == 1)
    i = 2;
    fib = 1;
    }
    

Reply
  • main()
    {
    
    sbit T1 = P1;
    int fib = 0;
    int prev = 0;
    T1 = 1;
    for (int i = 0; i < T1; i++)
    {
      while (i == 1)
      {
        fib = 1;
        i++;
      }
      i--;
      fib = fib + prev;
      prev = fib;
      }
    }
    

    I have made your code readable. your onelining almost made me not respond
    what are you doing with the while?
    it equates

    if (i == 1)
    i = 2;
    fib = 1;
    }
    

Children
  • Sorry about that. That was just a copy error on my part from uvision.

    I guess I’m more asking about the part where it has me taking the user input and putting it into p1 and using that for how many sequences it goes through.

  • Sorry about that. That was just a copy error on my part from uvision.

    NEVER copy always use cut-and-paste

    I guess I’m more asking about the part where it has me taking the user input and putting it into p1 and using that for how many sequences it goes through.

    I guess "putting it into p1" should be getting it from P1
    x = P1;

    how many sequences? just keep going till you have a value greater than the max specified