I Don't Know What to Write for the Title, HELP!!

So i'm doing this:

"Write a program that asks the user to type an integer N and compute u(N) defined with :
u(0)=3
u(n+1)=3*u(n)+4"

from: http://en.wikibooks.org/wiki/C++_Programming/Exercises/Iterations#EXERCISE_7

I know there is solutions for this, but i'm trying to make mine. Its exercise.

Theeeeenn....this is my attempt:

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
  #include <iostream>
using namespace std;

int main()
{
    int a[0]=3;

    for(int n=0; n<9; n++)
    {
        cout << "Please insert a number to calculate a[" << n <<"]: ";
        cin >> a[n]; cout << endl;

        if(n==0)
        {
            cout << a[0] << endl;
        }

        else
        {
            a[n] = 3*a[n-1]+4;
            cout << a[n] << endl;
        }
    }

    return 0;
}


but, unfortunately, its always error at line 6. Did i declare it the wrong way?
Last edited on
At line 6 you are declaring an array with 0 elements and assigning 3 to it.That part is wrong.
The general syntax for declaring an array is-
1
2
int a[5] = {2,1,3,5,4};
int b[5] = { 0 }; //initialise all to 0 

Please refer to the c++ tutorial at this site or your book.
And your implementation looks a little quirky .here's a hint .create a function "int u(int n)" and if the valve of n is equal to 0 return 0 else return 3*u(n-1)+4
Last edited on
You should use a function instead of the array that is used by you incorrectly.
Topic archived. No new replies allowed.