loop doing wrong things.

hello ive made a code that set a value to the value pointed. but when i want to output that whole pointer array it doesn't dislay them all
i have this output :
25

25
5
   //the numbers above this don't matter.
1          
1
2
3
4
5
6
6
6
6
6
7
   //the numbers under this should be the numbers above this. not the 25, 25 and 5.
1
1
2
3
4
5
6
6
6


here is my code :
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int x,a,z,i;
	cin>>x;cout<<endl;
	a=4*sqrt(float(x))+5;//length of array --> the next odd perfect square - x +1
	int * b=new int [a] ;//pointer to my values
	cout<<a<<endl;
	z=0;
	while(a>9) {
		a=a-8;z=z+1; }
	z=2*z+1; //gets the amount of numbers for my second and third  loop;
	cout<<z<<endl<<endl;
	for (i=0;i<2;i++) {
		*(b+i)=1;cout<<"1"<<endl;} //set value of first 2 numbers
	for (i=0;i<z;i++) {
		*(b+i+2)=i+2;cout<<i+2<<endl;}//set value of next z numbers
	for (i=0;i<z;i++) {
		*(b+z+i+2)=z+1;cout<<z+1<<endl;}//set value of next z numbers
	for(i=0;i<1;i++) {
		*(b+z+z+i+2)=*(b+z+z+i+1)+1;cout<<*(b+z+z+i+1)+1<<endl;} // set value of next number
	
	
	
	cout<<endl;
	for (i=-2;i<a-2;i++) { // i think the problem is somewhere here.
		cout<<*(b+i+2)<<endl; }

	delete b;	
	system("pause");
    return 0;
	}

can someone tell me why it isn't displaying them all.

edit: when you input x it must be odd and a perfect square. but when i input 9 it works fine.
Last edited on
i noticed that there are always only 9 numbers displayed in the last loop.
thats why its work with 9 since that only have 9 numbers. maybe this will help someone to discover whats going wrong.
What does variable a represent?

After this loop a is always 9 for x=9, x=25, x=49 etc.

1
2
	while(a>9) {
		a=a-8;z=z+1; }


Could you please try to give your variables more descriptive names than "a"?
a is the amount of numbers that i want to have at the end.
there should be 6 loops but only made four yet

here is a link to a graphic of what i want. i have denoted what z and a are in my loop.
http://img684.imageshack.us/img684/453/captureexn.png
I think you need to preserve a's value.

If so don't change a's value in this loop. Use another variable set to a's value before the loop or calculate z differently, possibly z=(a-9)/8.

1
2
	while(a>9) {
		a=a-8;z=z+1; }
Topic archived. No new replies allowed.