Print Array in reverse order

This is for a school homework assignment on Fibonacci numbers. Anyways we have to store the numbers that the user asked for in an array and then display those numbers in reverse order(greatest to smallest). I can do everything except 1 minor detail which is this. The output is supposed to look like this

4181 2584 1597 987 610 377
233 144 89 55 34 21
13 8 5 3 2 1
1 0

But mine looks like this

4181 2584
1597 987 610 377 233 144
89 55 34 21 13 8
5 3 2 1 1 0


Basically it looks like its flipped, but I need to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


#include    <iostream>

#include    <iomanip>

using namespace std;



void Fibonacci( int );





int main( )

{

    cout << "how many numbers in the Fibonacci Series? ";



    int n;                          // n numbers in the Fibonacci Series

    cin >> n;



    Fibonacci( n );



    return 0;

}



// the rest of your program...



void Fibonacci(int n)
{
	int fib[20];
	fib[0]=0;
	fib[1]=1;


	for(int i=2;i<20;i++)
	{
	fib[i]=fib[i-1]+fib[i-2];
	}


	for(int j=n-1;j>=0;j--)
	{
	cout << fib[j] << " ";
		if(j%6==0)
		cout << endl;
	}
}
The problem is that you're starting at the end by doing j = n-1. So, j % 6 == 0 not for the first six, but for the first arbitrary amount of numbers.

Personally I'd just make the for loop go forward: j = 0; j < n; ++j (or use i again), then instead of using fib[j] use fib[n-1-j].

Oh, and since the endline is after the output, make it j % 6 == 5 ;)

Good luck!
Last edited on
Topic archived. No new replies allowed.