Square problem

The part

for(int i=1; i<n; i++)
{
x[i]=sqrt(y[i]*i);
cout<<"x"<<i<<": "<<x[i]<<endl;
}



isn't working wll, for some elements the result is non, i thought that i because the numbers are complex, but not, with include complex i get the same resut.. can anyone help me

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<math.h>
#include <complex>
using namespace std;

int main()
{
    int n,elem[20];
    float y[20],A[20];
    complex<float>  x[20];
    
    cout<<"Number of elements?"<<endl;
    cin>>n;
    cout<<"Insert elements:"<<endl;
    for(int i=0; i<n; i++)
    {
        cin>>elem[i];
        
    }
    
    
    A[0]=pow(elem[0],2);
    
    for(int j=1; j<n; j++)
    {
        A[j]=pow(elem[j],2);
    
            int i=1;
            while((i+j)<=n-1&&(i<=j))
            {
                
                A[j]=A[j]+pow((-1),i)*2*(elem[j-i]*elem[j+i]);
                i=i+1;
            
            }
    

            cout<<"A"<<j<<":"<<A[j]<<endl;
    
    
    }
    
    for(int i=1; i<n; i++)
    {
        y[i]=A[i]/-A[i-1];
        cout<<"y"<<i<<": "<<y[i]<<endl;
    }
    
    for(int i=1; i<n; i++)
    {
        x[i]=sqrt(y[i]*i);
        cout<<"x"<<i<<": "<<x[i]<<endl;
    }

}
What does this do?
Last edited on
I'm not sure what this does, but array indices go from 0 to n-1, so
for(int i=1; i<n; i++)
should start from i=0 instead of i=1.
Topic archived. No new replies allowed.