0

I need to run fibonacci series through function in Octave.

I got the expected output, but my test case fails due to indendation in output.

 function fibo(n)
 a=0;
 b=1;
 x(:,1)=[1];
 for i=2:n
   c=a+b;
   x(:,i)=[c];
   a=b;
   b=c;
 endfor
 g=sprintf("%d   ",x);
 fprintf("Fibonacci sequence:   %s\n",g)
 endfunction    

 a = input("");
 fibo(a)

if Input = 10, Output: (Test case failed)

enter image description here

When using

g=sprintf("\t%d",x);

the following output is getting displayed:

enter image description here

Can anyone help me with passing the test case by resolving indentation!

Raja
  • 71
  • 3
  • 11
  • Maybe you are just missing some spaces? – Filip Feb 22 '20 at 01:36
  • Yes Filip, I cannot insert spaces to just pass this test case. If I did so, rest 10 test cases getting failed. – Raja Feb 22 '20 at 01:43
  • You'll have to give complete specs if that's the case: we can't guess what the other 10 test cases require or why this one won't be fixed by putting in the appropriate spaces. – L. Scott Johnson Feb 22 '20 at 02:25
  • Scott, Input will be 1 to 100. Test case gets random input numbers from 1 to 100. – Raja Feb 22 '20 at 04:13
  • Have you tried using tab `\t`? Like this: `g=sprintf("\t%d",x); fprintf("Fibonacci sequence:%s\n",g)` – R. Schifini Feb 22 '20 at 04:21
  • Schifini, using tab causes more spaces. Test cases getting failed – Raja Feb 22 '20 at 04:35
  • Please count the number of spaces between `:` and 1, between `1` and `1`, between `8` and `13` and between `13` and `21`. I can't do this from the pictures. I guess a format. Probably a format like `%5d` was used, padding leading zeros: `sprintf('%5d ',1,100,1,10000)` – Daniel Feb 22 '20 at 07:04
  • @Raja, your program matches the specs you've provided. If it fails the test, that merely means there are more specs you haven't provided (or the test is flawed). The output you show has the correct numbers, so clearly the spec deviation lies in the whitespace. Please provide specs for the whitespace or ask your instructor to fix the test. – L. Scott Johnson Feb 22 '20 at 12:04

1 Answers1

3

Since the image of the expected output isn't highlight-able, I cannot be 100% sure, but it looks like each number has a fixed width of 4 characters.

Hence, the way I see the output is:

Fibonacci sequence: ---1 ---1 ---2 ---3 ---5 ---8 --13 --21 --34 --55

where the - here indicates padded spaces.

To make this change, your print lines should change to something like this:

g=sprintf(" %4d", x);
fprintf("Fibonacci sequence: %s\n", g)

Aside: the function should pre-allocate the memory for x for better memory management. In MATLAB, this would be something like:

a=0;
b=1;
x = zeros(1, n); % This reserves memory for n numbers
x(:,1)=[1];
... % your code below

Furthermore, since x is designed to be a 1-dimensional array, it would probably be easier to just use

x(i) = c

instead of something like

x(:,i) = [c]

Michael
  • 535
  • 2
  • 6
  • Thanks Michael. Above scenario works for test case with n<10. For n>10, test case fails. I will contact instructor to fix this issue! – Raja Feb 23 '20 at 00:17