Hi Daniweb community. This is my first post. I'm new to this programming lark and I've hit a snag with an exercise I'm attempting.

EXERCISE
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

My Attempt:

// Iteration

#include <iostream>
using namespace std;

int main ()
{
    int u[]={}, N, i;
    cout << "Choose an integer N: " << endl;
    cin >> N;
    u[0]=3;
    for (i=0;i<=N;i++)
    {
        u[i+1]=(3*u[i])+4;
    }
    cout << "u(N) is given by: " << u[i] << endl;
    return 0;
}

The problem I'm having is that it is performing the iteration as though the initial value is 0. How could I set it to 3?

Thanks Deceptikon,

This works now.

// Iteration

#include <iostream>
using namespace std;

int main ()
{
    int N, i;
    cout << "Choose an integer N: " << endl;
    cin >> N;
    int u[N];
    u[0]=3;
    for (i=0;i<=N;i++)
    {
        u[i+1]=(3*u[i])+4;
    }
    cout << "u(N) is given by: " << u[N] << endl;
    return 0;
}

Tinnin the code you posted actually works? As far as I know with how c++ works line 11 should be an error. You shouldnt be able to create any array on the stack without knowing the size at compile time. What compiler are you using?

Which means if you added the switchs -Wall -pedantic to your compiler command you code would not compile (try and see)

  • -Wall - output all warnings and errors (actually not quite all a few esoteric ones still need to be explicitly enabled if you want them)
  • -pedantic - pendantically follow the C++ (or C) standard and do not use any extentions.

Have a look from lines 13 to 16, you're writing beyond the bounds of the allocated array.

Edited 4 Years Ago by nullptr

Not only are you writing beyond the size of the array (i.e., because at the last iteration, i == N and thus, i+1 == N+1, and thus, the loop-expression becomes u[N+1] = 3*u[N] + 4;, and the one iteration before that (i == N-1) is also writing beyond the bounds of the array), but you actually don't need to use an array at all. In order to compute the expression within the loop, all you need is the value computed from the previous iteration. So, you don't need to store the entire array of values from 0 to N-1. Try to rewrite your code without using an array.

Here's what I've done since.
I'm using Code::Blocks 10.05 with GNU GCC compiler. I went to setting > Compiler and debugger > Compiler Flags.
I selected -Wall and -pedantic.
The program still runs without errors. Did I do that right?

The program still runs without errors. Did I do that right?

Nope, you forgot to read the warnings. Just because the code compiles and runs doesn't mean it compiled cleanly and is correct.

And in response to mike 200 17, I see what you mean regarding the array boundary. The reason I tried to use an array for this problem is because I hadn't seen any examples where someone had tried to use one and thought I'd give it a go.

This article has been dead for over six months. Start a new discussion instead.