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?

int u[]={}

You need to give your array a size. I'm surprised there wasn't an error from this definition.

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?

I'm using MINGW. The code works fine. No errors.

That's a compiler extension for gcc that allows non-constant array sizes, it's not a standard feature.

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.

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.

I see now. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.