C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 26th, 2014, 06:49 PM
klika klika is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2014
Posts: 1 klika User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 34 sec
Reputation Power: 0
C++ square help

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

}

Reply With Quote
  #2  
Old January 27th, 2014, 01:21 PM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 7
Please explain the problem in more detail.

I guess you mean you get these numbers: -1#IND000000.
This is because you do something illegal. You use sqrt on a negative value.

add this
Code:
	for(int i=1; i<n; i++)
	{
		x[i]=sqrt(complex<float>(y[i]*i));
		cout<<"x"<<i<<": "<<x[i]<<endl;
	}


The problem with your code was the x values were complex numbers, but you use sqrt on a non (y) complex number, resulting in an illegal input.
By writing complex<float>(y[i]*i); you cast the type of float to a complex float.

also your main function returns a value. You should write return 0; in the end.

Last edited by ediz : January 27th, 2014 at 01:34 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ square help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap