0

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

#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;
    }

}
3
Contributors
2
Replies
24
Views
3 Years
Discussion Span
Last Post by ddanbe
0

What is it that you are trying to do? If you are trying to square each element in the vector/array then it would be y[i]*y[I] but I doubt that's your problem but I don't exactly know what it is you want to do so can you be more specific?

0

it isn't working

Probably obvious for you, not for us. Please explain EXACTLY what is not working. (error messages, program behavior etc.)
To ovecome the overhead of the pow((-1),i) expression on line 32, I usually use something like: int sign = -1; before the while loop.
And

A[j] = A[j] + sign*2*(elem[j-i]*elem[j+i]);
i = i+1;
sign = -sign;

in the while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.