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
#include <iostream>
#include <cmath>
usingnamespace 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=newint [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.
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.