# Thread: Problem with a sequence

1. ## Problem with a sequence

Teh first term is 1, the second term is 2, and for all n>=3 the nth term is equal to term (n-1) plus term n/2, where n/2 is rounded down.

Program should prompt for n, and then display the nth term in the sequence. it can be assumed n is 50 or less. the program must contain a loop that keeps reading new values for n and then prints the nth term. The program show stop when n=0.

Code:
```//tj wright 11.1
#include <stdio.h>

void main (void)
{
int q,m,n,x[50];

for (m=1;m<=6;m++)

{
printf("enter n");
scanf ("%d", &n);
if (n==0) goto end;

x[1]=1;
x[2]=2;

for (q=3;q<=50;q++)
{
x[q]=x[q-1]=x[q/2];
}

printf("The number is %d\n",x[n]);

}
end:
}```

When I run the program, it asks me to enter n, then I go to scan it in, and the program exits and points me to the x[2]=2; line. I am trying to trace the program and see where it is going wrong, but I cannot tell.

Can anyone tell me where I am going wrong?

2. void main is wrong, see the FAQ.

Using goto in such a short program is very poor style. Use appropriate loop structures.

> for (q=3;q<=50;q++)
This steps off the end of the array.

3. "this steps off the end of the array"

What does that mean?

4. Obvious mistake:
Code:
`  x[q]=x[q-1]=x[q/2];`

5. I can't even get it to compile, it gives me:

error at line 23 (end label at end of compound statement
warning at line 3 (void main(void)) return type of 'main' is not 'int'

Last time i checked, "goto" was highly discouraged. Here, you can either use:
return; //supposing you don't fix the return type
return 0; //supposing you fix the return type to be int like it's supposed to be
exit(0); //works either way, but I'd generally encourage you to use "return 0;"

6. I could use break; instead of the goto, and it would work fine.

7. Originally Posted by astrodude15
"this steps off the end of the array"

What does that mean?
If you have something like:
Code:
```{
int nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int i;

for(i = 0;i < 11;++i)
printf("&#37;d\n", nums[i]);
}```
When i gets to 10 in the loop, it's stepped off the end of the array. The only valid indexes are 0 through 9 for that array.

8. A better solution would be to change your main loop from a for loop to a while loop.

You could change this:
Code:
`for (m=1;m<=6;m++)`
to this:
Code:
`while(n != 0)`
Then you wouldent even need to use a goto or break to end the program.

9. Thank you all very much for your help and sharing your knowledge!

10. Originally Posted by itsme86
Code:
`  int nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };`
Shouldn't that give an warning about an "excess elements in initializer" or somesuch? It does for me.

11. Yeah, it should. I got a little carried away with the initializer

12. Also:
Code:
```sourceFile.cpp:5: `main'
must return `int'
sourceFile.cpp: In
function `int main(...)':
sourceFile.cpp:27: label
must be followed by statement```
A goto label must precede a statement. You can't just have one at the end of a block. However, you can add a NULL statement if you wish:
Code:
```end: ;
}```
Not that I'm advocating goto. It's a bad idea to use goto.