1

In this code complex datatype is taken in c++ and while converting on c i dont know to take input and give output of complex data type. Can someone help me convert it from c++ to c. Will someone help me to take input and view complex data type in c on this program.

//c++ program original

/***************** Newton horner's method ******************/
#include<iostream.h>
#include<conio.h>
#include<complex.h>
#include<math.h>
int main()
{
    complex a[20],b[20],c[20];
    complex x;
    int n,i;
    clrscr();

    cout<<"Enter the degree of equations : ";cin>>n;
    cout<<"Enter all coefficients \n";
    for(i=0;i<=n;i++)
        cin>>a[i];
    cout<<"enter initial guess:";cin>>x;

    while(n>0)
    {
        c[0]=b[0]=a[0];
        for(i=1;i<=n;i++)
            b[i]=a[i]+b[i-1]*x;
        for(i=1;i<n;i++)
            c[i]=b[i]+c[i-1]*x;
        if(abs(c[n-1])==0.0)   // for complex "abs" instead of "fabs"
        {
            cout<<"Divide by Zero : ERROR !! ";
            break;
        }
        x-=b[n]/c[n-1];
        if(abs(b[n]/c[n-1])<0.00001)
        {
            cout<<"\nRoot is: "<<x<<"f(x) "<<b[n];
            for(i=1;i<n;i++)
                a[i]=b[i];
            n--;
        }
    }
getch();
return 0;
}

My try to convert c++ to c although it doesn't work. "and i know that %f and %d is for float and integer but i was trying on this code."

/****************** Newton horner's method ******************/
#include<stdio.h>
#include<conio.h>
#include<complex.h>
#include<math.h>
int main()
{
    float a[20],b[20],c[20];
    float x;
    int n,i;

    printf("Enter the degree of equations : ");
    scanf("%d",&n);
    printf("Enter all coefficients \n");
    for(i=0;i<=n;i++)
        scanf("%f",&a[i]);enter code here
    printf("enter initial guess:");
    scanf("%f",&x);

    while(n>0)
    {
        c[0]=b[0]=a[0];
        for(i=1;i<=n;i++)
            b[i]=a[i]+b[i-1]*x;
        for(i=1;i<n;i++)
            c[i]=b[i]+c[i-1]*x;
        if(fabs(c[n-1])==0.0)   // for complex "abs" instead of "fabs"
        {
            printf("Divide by Zero : ERROR !! ");
            break;
        }
        x-=b[n]/c[n-1];
        if(abs(b[n]/c[n-1])<0.00001)
        {
            printf("\nRoot is: %f and f(x) is %f",x,b[n]);
            for(i=1;i<n;i++)
                a[i]=b[i];
            n--;
        }
    }
getch();
return 0;
}

0 Answers0