locked
Gimme C/C++ Programs Definitions - Part-II RRS feed

  • Question

  • Ok guys this thread is a continuation to the thread "Gimme C/C++ Programs Definitions.."

    As the pages of that thread increased, it was becoming hard to navigate, i guess it was a good decision to start a new thread about the same topic.
    Wednesday, May 23, 2007 4:33 PM

Answers

  • oh nice of you to take the initiative in case of forums actual starters, absence..anyways lets get rolling...
    Thursday, May 24, 2007 1:36 PM
  • where's the problem?
    Saturday, May 26, 2007 1:23 PM
  • ok here's the problem.
    though i have posted the same question on the other forum.
    WAP to print 1 to 100 without using any looping technique, goto or recursion or indirect recursion in C
    Saturday, May 26, 2007 1:24 PM
  • @Sunil, cant figure out the answer to this. I think you should post the answer.
    Saturday, May 26, 2007 5:41 PM
  • truly speaking, i'm also looking for it's answer.
    though i know a little bit logic.
    here, either we have to make use of a stack
    or make use of functions returning pointers.
    buddy, if you'll get the solution, do post it here
    Sunday, May 27, 2007 12:37 PM
  • Well i m not posting the answer, but i will tell you the logic.. though it sounds wierd but it might just work....

    ok here it goes...
    take a string and initialize it with the numbers till 100,
    like char a[]="1  2  3  4  5  6...... 100";

    now a is a pointer to this string, take as an input from the user till which number he wants to print the nummbers.. lets say in n;

    now give this cmd:
    cout.write(a, n*3);

    what this will do is print the string a only till the length n*3,
    ie is n =1; only 1 will get printed, and if n = 2, only 1   2   will get printed....

    Sunday, May 27, 2007 1:41 PM
  • Now i m going to start programming for my assignment of 2nd sem from tommorrow, so if there is any problems, you all are most welcome to post it here, i will program them and give it here and also have them for my assginment file, so 2 birds with one arrow....

    After 1 week i will post some of my work
    Sunday, May 27, 2007 1:45 PM
  • itzy bitzy solution.
    i would like to point out some deficiences
    1)what if user inputs a number >100.
    2) you are taking much memory.
    30 i told you to write a program in C. if you want to do it, there's  much better way
    Monday, May 28, 2007 7:00 AM
  • itzy bitzy solution.
    i would like to point out some deficiences
    1)what if user inputs a number >100.
    2) you are taking much memory.
    30 i told you to write a program in C. if you want to do it, there's  much better way
    Monday, May 28, 2007 7:01 AM
  • Well Sunil, then post the answer... I used this method because of your restrictions... and there is something like write cmd in C also, but i dont remmber is rite now..

    if you have something else in mind then please post it, or give us what you ave in mind...
    Monday, May 28, 2007 4:41 PM
  • truly speaking, i'm also looking for it's answer.
    though i know a little bit logic.
    here, either we have to make use of a stack
    or make use of functions returning pointers.
    buddy, if you'll get the solution, do post it here
    Tuesday, May 29, 2007 1:51 AM
  • @Sunil - Do you know whether its possible or not ? or you are just trying to find a novel way to do it ?
    Tuesday, May 29, 2007 4:38 PM
  • yaar, this question was asked in the NSIT tech event.
    surely, answer is possible.
    all we have to do is explore it.
    answer surely should be using memory stack or functions returning pointers
    Wednesday, May 30, 2007 1:28 AM
  •  Harshil_Patel_03b5f2 wrote:
    Ok guys this thread is a continuation to the thread "Gimme C/C++ Programs Definitions.."

    As the pages of that thread increased, it was becoming hard to navigate, i guess it was a good decision to start a new thread about the same topic.

     

    Dear Harshil,

     

    Thank you Harshil for starting a new thread. I really do not mind about you starting a new thread. Actually i had exams, and so i was not able to be much online. All i watned is the C/C++ program definitions, whether its in my thread or yours, it does not matter. I am here  to learn. You can continue with the posting of the rest of the programs. i have seen many programs in my thread, i wish you continue them here.

     

     

    Regards.

    Thursday, May 31, 2007 9:59 AM
  • Thanks Mahesh. I thought you would oppose my action of starting a new thread. You seem to be cool Smile

    Ill start continuing posting the programs.
    Thursday, May 31, 2007 6:25 PM

  • Code Snippet

    /*
    [36] Write a program to print the pattern :
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
    int i,j,n,sp;
    clrscr();
    printf("Enter a number : ");
    scanf("%d",&n);
    sp=0;
    for(i=n ; i>=1 ; i--, printf("\n")) {
    printf("%*c",sp+=3,' ');
    for (j=1 ; j<=i ; j++)
    {
    printf("%3d",j);
    }
    }
    getch();
    }
    Thursday, May 31, 2007 6:27 PM

  • Code Snippet


    /*
    [37] Write a program to print the pattern :
                A B C D E
                A B C D
                A B C
                A B
                A
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for (ch='A', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:28 PM
  • Code Snippet

    /*
    [35] Write a program to print the pattern :
                5 4 3 2 1
                5 4 3 2
                5 4 3
                5 4
                5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=n ; i>=1 ; i--, printf("\n")) {
    //        printf("%*c",sp--*3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",n-j+1);
            }
        }
        getch();
    }


    Thursday, May 31, 2007 6:28 PM

  • Code Snippet

    /*
    [34] Write a program to print the pattern :
                        a
                      b b
                    c c c
                  d d d d
                e e e e e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",i+'a'-1);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [40] Write a program to print the pattern :
                        *
                      *   *
                    *   *   *
                  *   *   *   *   
                *   *   *   *   *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%4c",'*');
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [39] Write a program to print the pattern :
                        a
                      a   b
                    a   b   c
                  a   b   c   d
                a   b   c   d   e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",j+'a'-1);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [38] Write a program to print the pattern :
                a b c d e
                  a b c d
                    a b c
                      a b
                        a
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch = 'a';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for(j=0 ; j<n-i ; j++)    {
                printf("  ");
            }
            for (ch='a', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM
  • Alright, then get ready for some hot new programs, I just made them today itself.. they are of Conm, you will like them....
    Thursday, May 31, 2007 6:51 PM
  • Code Snippet


    // This program displays the use of Bisection method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon;
        getch();
        clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(x1-x2) > epsilon && n <500)
        {
            n++;
            x3 = (x1 + x2) / 2;
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(Fx(x1)*Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006
    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.50000  5.62500  0.50000
    2  2.00000  -1.00000 2.50000  5.62500  2.25000  1.89063  0.25000
    3  2.00000  -1.00000 2.25000  1.89063  2.12500  0.34570  0.12500
    4  2.00000  -1.00000 2.12500  0.34570  2.06250  -0.35132 0.06250
    5  2.06250  -0.35132 2.12500  0.34570  2.09375  -0.00894 0.03125
    6  2.09375  -0.00894 2.12500  0.34570  2.10938  0.16684  0.01563
    7  2.09375  -0.00894 2.10938  0.16684  2.10156  0.07856  0.00781
    8  2.09375  -0.00894 2.10156  0.07856  2.09766  0.03471  0.00391
    9  2.09375  -0.00894 2.09766  0.03471  2.09570  0.01286  0.00195
    10 2.09375  -0.00894 2.09570  0.01286  2.09473  0.00195  0.00098
    11 2.09375  -0.00894 2.09473  0.00195  2.09424  -0.00350 0.00049
    12 2.09424  -0.00350 2.09473  0.00195  2.09448  -0.00077 0.00024
    13 2.09448  -0.00077 2.09473  0.00195  2.09460  0.00059  0.00012
    14 2.09448  -0.00077 2.09460  0.00059  2.09454  -0.00009 0.00006
    15 2.09454  -0.00009 2.09460  0.00059  2.09457  0.00025  0.00003
    16 2.09454  -0.00009 2.09457  0.00025  2.09456  0.00008  0.00002
    17 2.09454  -0.00009 2.09456  0.00008  2.09455  -0.00000 0.00001
    18 2.09455  -0.00000 2.09456  0.00008  2.09455  0.00004  0.00000


    The root of the function x^3 - 2x - 5 is 2.09455

      */


    Thursday, May 31, 2007 6:51 PM
  • Code Snippet


    // This program displays the use of Bisection method to find the root of
    // The equation, given by the user at runtime...

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void clrscr()
    {
        system("cls");
    }



    void main()
    {
        equation F;
        double x1 = 0, x2 = 1, x3;
        double epsilon;
        int degree;
        cout<<endl;

    //    clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<endl;

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(x1-x2) > epsilon && n <500)
        {
            n++;
            x3 = (x1 + x2) / 2;
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<F.Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<F.Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<F.Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(F.Fx(x1)*F.Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5

    Please give the degree of tolerance : 5
     Epsilon : 5e-006
    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.50000  5.62500  0.50000
    2  2.00000  -1.00000 2.50000  5.62500  2.25000  1.89063  0.25000
    3  2.00000  -1.00000 2.25000  1.89063  2.12500  0.34570  0.12500
    4  2.00000  -1.00000 2.12500  0.34570  2.06250  -0.35132 0.06250
    5  2.06250  -0.35132 2.12500  0.34570  2.09375  -0.00894 0.03125
    6  2.09375  -0.00894 2.12500  0.34570  2.10938  0.16684  0.01563
    7  2.09375  -0.00894 2.10938  0.16684  2.10156  0.07856  0.00781
    8  2.09375  -0.00894 2.10156  0.07856  2.09766  0.03471  0.00391
    9  2.09375  -0.00894 2.09766  0.03471  2.09570  0.01286  0.00195
    10 2.09375  -0.00894 2.09570  0.01286  2.09473  0.00195  0.00098
    11 2.09375  -0.00894 2.09473  0.00195  2.09424  -0.00350 0.00049
    12 2.09424  -0.00350 2.09473  0.00195  2.09448  -0.00077 0.00024
    13 2.09448  -0.00077 2.09473  0.00195  2.09460  0.00059  0.00012
    14 2.09448  -0.00077 2.09460  0.00059  2.09454  -0.00009 0.00006
    15 2.09454  -0.00009 2.09460  0.00059  2.09457  0.00025  0.00003
    16 2.09454  -0.00009 2.09457  0.00025  2.09456  0.00008  0.00002
    17 2.09454  -0.00009 2.09456  0.00008  2.09455  -0.00000 0.00001
    18 2.09455  -0.00000 2.09456  0.00008  2.09455  0.00004  0.00000


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:52 PM
  • Code Snippet


    // This program displays the use of False position method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        x3 = x2;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(Fx(x3)) > epsilon && n <500)
        {
            n++;
            x3 = x1 - Fx(x1)*((x2 - x1) / (Fx(x2) - Fx(x1)));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(Fx(x1)*Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.05882  -0.39080 0.50000
    2  2.05882  -0.39080 3.00000  16.00000 2.08126  -0.14720 0.47059
    3  2.08126  -0.14720 3.00000  16.00000 2.08964  -0.05468 0.45937
    4  2.08964  -0.05468 3.00000  16.00000 2.09274  -0.02020 0.45518
    5  2.09274  -0.02020 3.00000  16.00000 2.09388  -0.00745 0.45363
    6  2.09388  -0.00745 3.00000  16.00000 2.09431  -0.00275 0.45306
    7  2.09431  -0.00275 3.00000  16.00000 2.09446  -0.00101 0.45285
    8  2.09446  -0.00101 3.00000  16.00000 2.09452  -0.00037 0.45277
    9  2.09452  -0.00037 3.00000  16.00000 2.09454  -0.00014 0.45274
    10 2.09454  -0.00014 3.00000  16.00000 2.09455  -0.00005 0.45273
    11 2.09455  -0.00005 3.00000  16.00000 2.09455  -0.00002 0.45273
    12 2.09455  -0.00002 3.00000  16.00000 2.09455  -0.00001 0.45273
    13 2.09455  -0.00001 3.00000  16.00000 2.09455  -0.00000 0.45272


    The root of the function x^3 - 2x - 5 is 2.09455
      */


    Thursday, May 31, 2007 6:53 PM
  • Code Snippet


    // This program displays the use of False position method to find the root of
    // The equation, user defined at the run time..

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void main()
    {
        equation F;
        double x1, x2, x3;
        double f1, f2, f3;
        double epsilon;
        int degree;


        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        x3 = x2;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        f3 = F.Fx(x3);
        while(fabs(f3) > epsilon && n <500)
        {
            n++;
            f1 = F.Fx(x1);
            f2 = F.Fx(x2);
            f3 = F.Fx(x3);
            x3 = x1 - f1*((x2 - x1) / (f2 - f1));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<f1
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<f2
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<f3
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(f1*f2 > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5
    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.05882  16.00000 0.50000
    2  2.00000  -1.00000 2.05882  -0.39080 2.09656  -0.39080 0.02941
    3  2.09656  0.02243  2.05882  -0.39080 2.09451  0.02243  0.01887
    4  2.09656  0.02243  2.09451  -0.00046 2.09455  -0.00046 0.00102
    5  2.09656  0.02243  2.09455  -0.00000 2.09455  -0.00000 0.00100


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:54 PM
  • Code Snippet


    // This program displays the use of Secant method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        x3=x2;
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout<<"-1 "<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)<<endl;
        cout<<"0  "<<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)<<endl;

        while(fabs(Fx(x3)) > epsilon && n <500)
        {
            n++;
            x3 = (x1*Fx(x2) - x2*Fx(x1)) / (Fx(x2) - Fx(x1));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)<<endl;
            x1 = x2;
            x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  xn       f(xn)
    -1 2.00000  -1.00000
    0  3.00000  16.00000
    1  2.05882  -0.39080
    2  2.08126  -0.14720
    3  2.09482  0.00304
    4  2.09455  -0.00002
    5  2.09455  -0.00000


    The root of the function x^3 - 2x - 5 is 2.09455
      */


    Thursday, May 31, 2007 6:54 PM
  • Code Snippet


    // This program displays the use of Secant method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void main()
    {
        equation F;
        double x1, x2, x3;
        double f1, f2, f3;
        double epsilon;
        int degree;


        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        x3=x2;
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout<<"-1 "<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<F.Fx(x1)<<endl;
        cout<<"0  "<<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<F.Fx(x2)<<endl;

        f3 = F.Fx(x3);
        while(fabs(f3) > epsilon && n <500)
        {
            n++;
            f1 = F.Fx(x1);
            f2 = F.Fx(x2);
            f3 = F.Fx(x3);
            x3 = (x1*f2 - x2*f1) / (f2 - f1);
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<f3<<endl;
            x1 = x2;
            x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5
    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  xn       f(xn)
    -1 2.00000  -1.00000
    0  3.00000  16.00000
    1  2.05882  16.00000
    2  2.08126  -0.39080
    3  2.09482  -0.14720
    4  2.09455  0.00304
    5  2.09455  -0.00002
    6  2.09455  -0.00000


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:55 PM

  • // This program displays the use of Newton-Raphson method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    double F1x(double x)
    {
        return(3*pow(x,2) - 2);
    }


    void main()
    {
        double x;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Asking the initial approximate value from the user...
        cout<<"Please give the initial approximate value : ";
        cin>>x;


    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<"f1(xn)   "<<"f(xn) / f1(xn)"<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        while(fabs(Fx(x)) > epsilon && n <500)
        {
            double x1;
            n++;
            x1 = x - Fx(x) / F1x(x);
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x<<setw(9)<<setprecision(5)<<Fx(x)
                <<setw(9)<<setprecision(5)<<F1x(x)<<setw(9)<<setprecision(5)<<Fx(x) / F1x(x)<<endl;
            x = x1;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Please give the initial approximate value : 2
    N  xn       f(xn)    f1(xn)   f(xn) / f1(xn)
    1  2.00000  -1.00000 10.00000 -0.10000
    2  2.10000  0.06100  11.23000 0.00543
    3  2.09457  0.00019  11.16165 0.00002


    The root of the function x^3 - 2x - 5 is 2.09455
      */
    Thursday, May 31, 2007 6:55 PM
  • Good progs Varun.I am first posting low level C programs Smile

    And then i will post harder ones. I have few OR and stats programs Smile ill post em too.
    Thursday, May 31, 2007 7:35 PM
  • Code Snippet

    // This program will find the roots of the equation x^3 -9x + 1 = 0
    // Using Successive method of Approximation.

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    double f(double x)
    {
        return (x*(pow(x,2) -9) + 1);
    }

    double g(double x)
    {
        return ((pow(x,3) + 1 ) / 9);
    }


    void main()
    {
        double x0, x1;
        double epsilon;
        int precision, itteration = 0;

        printline('=');
        cout<<"Enter the decimal places upto where you want precision : ";
        cin>>precision;
        epsilon = pow(10, -precision);
        cout<<"Epsilon : "<<epsilon<<endl;

        cout<<"Please give the 1st approximate value of the root : ";
        cin>>x1;
        if(f(x1) == 0)
        {
            cout<<"The root of the equation is "<<x1;
            exit(1);
        }
        printline('=');
        
        printline('-');
        cout<<setw(3)<<"N"<<setw(precision + 5)<<"x"<<setw(precision + 5)<<"g(x)"<<endl;
        printline('-');

        cout.precision(precision);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        do
        {
            x0 = x1;
            itteration++;
            x1 = g(x0);
            cout<<setw(6)<<itteration<<setw(precision + 5)<<x0<<setw(precision + 5)<<x1<<endl;
        }
        while(fabs(x0-x1)>epsilon && itteration < 500);

        printline('=');

        if(itteration == 500)
        {
            cout<<"Sorry the method fails, as it crosses the limited itterations ";
        }
        else
        {
            cout<<"The root of the equation x^3 - 9x + 1 = 0 is "<<x1<<endl;
            cout<<"The approximated g(x) where g(x) = (x^3 + 1) / 9"<<endl;
        }
        printline('=');

    }

    /*..................................Output...................................
    ================================================================================

    Enter the decimal places upto where you want precision : 8
    Epsilon : 1e-008
    Please give the 1st approximate value of the root : .5
    ================================================================================

    --------------------------------------------------------------------------------

      N            x         g(x)
    --------------------------------------------------------------------------------

    1     0.50000000   0.12500000
    2     0.12500000   0.11132813
    3     0.11132813   0.11126442
    4     0.11126442   0.11126416
    5     0.11126416   0.11126416
    ================================================================================

    The root of the equation x^3 - 9x + 1 = 0 is 0.11126416
    The approximated g(x) where g(x) = (x^3 + 1) / 9
    ================================================================================

    */


    Saturday, June 2, 2007 4:27 AM
  • Code Snippet

    /*  This program will calculate the root of the equation
        x^2 + y^2 = 5
        x^2 - y^2 = 1
        Using Newton Raphson method for 2 variables.
    */

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    double F(double x, double y)
    {
        return (pow(x,2) + pow(y,2) - 5);
    }
    double G(double x, double y)
    {
        return (pow(x,2) - pow(y,2) - 1);
    }
    double Fx(double x, double y)
    {
        return (2*x);
    }
    double Fy(double x, double y)
    {
        return (2*y);
    }
    double Gx(double x, double y)
    {
        return (2*x);
    }
    double Gy(double x, double y)
    {
        return (-2*y);
    }


    void main()
    {

        double f0, f1, f2, g0, g1, g2;
        double x0, x1, y0, y1;
        double D, Dx, Dy ;
        double epsilon;
        int precision, counter=0;

        printline('=');
        cout<<"Enter the Number of decimal digits of precision : ";
        cin>>precision;
        epsilon = pow(10, -precision);
        cout<<"Enter the 1st approx value of x : ";
        cin>>x1;
        cout<<"Enter the 1st approx value of y : ";
        cin>>y1;

        cout.precision(precision);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        printline('-');
        cout<<setw(3)<<"N"<<setw(precision + 4)<<"x"<<setw(precision + 4)<<"y"
            <<setw(precision + 4)<<"f0"<<setw(precision + 4)<<"f1"
            <<setw(precision + 4)<<"f2"<<setw(precision + 4)<<"g0"
            <<setw(precision + 4)<<"g1"<<setw(precision + 4)<<"g2"
            <<endl;
        printline('-');

        do
        {
            counter++;
            x0 = x1;
            y0 = y1;

            f0 = F(x0, y0);
            f1 = Fx(x0, y0);
            f2 = Fy(x0, y0);
            g0 = G(x0, y0);
            g1 = Gx(x0, y0);
            g2 = Gy(x0, y0);

            D  = (f1 * g2 ) - ( f2 * g1 );
            Dx = (f2 * g0 ) - ( f0 * g2 );
            Dy =((f1 * g0 ) - ( f0 * g1 ))*-1;

            x1 = x0 + Dx / D;
            y1 = y0 + Dy / D;

        cout<<setw(3)<<counter<<setw(precision + 4)<<x0<<setw(precision + 4)<<y0
            <<setw(precision + 4)<<f0<<setw(precision + 4)<<f1
            <<setw(precision + 4)<<f2<<setw(precision + 4)<<g0
            <<setw(precision + 4)<<g1<<setw(precision + 4)<<g2
            <<endl;

        }while( fabs(x0-x1) > epsilon && fabs(y0 - y1) > epsilon && counter < 500);

        printline('=');

        if(counter == 500)
            cout<<"Sorry the method fails ";
        else
        {
            cout<<"The sollution of the following functions :"<<endl;
            cout<<"F(x,y)        =    x^2 + y^2 - 5"<<endl;
            cout<<"G(x,y)        =    x^2 - y^2 - 1"<<endl;
            cout<<"Fx(x,y)        =    2x"<<endl;
            cout<<"Fy(x,y)        =    2y"<<endl;
            cout<<"Gx(x,y)        =    2x"<<endl;
            cout<<"Gy(x,y)        =  -2y"<<endl;
            printline('-');
            cout<<"is "<<" x = "<<x1<<" y = "<<y1<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................

    ================================================================================
    Enter the Number of decimal digits of precision : 5
    Enter the 1st approx value of x : 1
    Enter the 1st approx value of y : 1
    --------------------------------------------------------------------------------

    N  x        y        f0       f1       f2       g0       g1       g2
    --------------------------------------------------------------------------------

    1  1.00000  1.00000  -3.00000 2.00000  2.00000  -1.00000 2.00000  -2.00000
    2  2.00000  1.50000  1.25000  4.00000  3.00000  0.75000  4.00000  -3.00000
    3  1.75000  1.41667  0.06944  3.50000  2.83333  0.05556  3.50000  -2.83333
    4  1.73214  1.41422  0.00032  3.46429  2.82843  0.00031  3.46429  -2.82843
    ================================================================================

    The sollution of the following functions :
    F(x,y)          =       x^2 + y^2 - 5
    G(x,y)          =       x^2 - y^2 - 1
    Fx(x,y)         =       2x
    Fy(x,y)         =       2y
    Gx(x,y)         =       2x
    Gy(x,y)         =      -2y
    --------------------------------------------------------------------------------

    is  x = 1.73205 y = 1.41421
    ================================================================================

    */


    Saturday, June 2, 2007 4:27 AM
  • Code Snippet

    // This program will Calculate the root of a equation
    // Using the Birge-Vieta Method..

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }


    void main()
    {

        double *f, *f0,*g0, *f1, *g1;
        double x0, x1, fx, f1x;
        int counter=0, order, precision, i;
        double epsilon;

        printline('=');
        cout<<"Enter the order of the polynomial : ";
        cin>>order;

        f = new double[order + 1];
        f0 = new double[order + 1];
        f1 = new double[order + 1];
        g0 = new double[order + 1];
        g1 = new double[order + 1];

        cout<<"Please give the coefficients of the polynomial : "<<endl;
        for(i=order; i>=0; i--)
        {
            cout<<"Coefficient of x^"<<i<<" : ";
            cin>>f[i];
        }

        cout<<"Please give the nuumber of decimal places of accuracy : ";
        cin>>precision;
        epsilon = pow(10, -precision);

        cout<<"Enter the Initial approximate value of the root : ";
        cin>>x1;

        printline('=');

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);

        do
        {
            counter++;
            x0 = x1;
            printline('*');
            cout<<setw(23)<<"Itteration "<<counter<<endl;
            printline('*');
        
            cout<<"Calculation of fx and f'x Using Synthetic devision"<<endl;
            printline('~');
            cout<<setw(20)<<" "<<"x0 = "<<x0<<endl;
            
            f0[order] = f1[order] = 0;
            g0[order] = g1[order] = f[order];
            for(i=order - 1; i>=0; i--)
            {
                f0[i] = g0[i+1] * x0;
                g0[i] = f[i] + f0[i];

                f1[i] = g1[i+1] * x0;
                g1[i] = f1[i] + g0[i];
            }

            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<f[i];
            cout<<"\n\n";
            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<f0[i];
            cout<<"\n";
            printline('-');

            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<g0[i];
            cout<<"\n\n";        
            for(i=order; i>0; i--)
                cout<<setw(precision + 5)<<f1[i];
            cout<<"\n";
            printline('-');
            for(i=order; i>0; i--)
                cout<<setw(precision + 5)<<g1[i];
            cout<<"\n\n";

            printline('-');
            fx = g0[0];
            f1x = g1[1];

            cout<<"fx = "<<fx<<"  f1x = "<<f1x<<endl;
            x1 = x0 - fx / f1x;
            cout<<" x1 = "<<x1<<endl;
            printline('=');

        }while(fabs(x1- x0) > epsilon && counter < 10);

            if(counter == 10)
                cout<<"Sorry the method fails";
            else
            {
                cout<<"The root of the equation :- "<<endl;
                for(int i = order; i>=0; i--)
                {
                    if(f[i])
                        cout<<f[i]<<"x^"<<i<<" ";
                }
                cout<<" is "<<x1<<endl;
            }
            printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Enter the order of the polynomial : 3
    Please give the coefficients of the polynomial :
    Coefficient of x^3 : 1
    Coefficient of x^2 : 0
    Coefficient of x^1 : -2
    Coefficient of x^0 : -5
    Please give the nuumber of decimal places of accuracy : 5
    Enter the Initial approximate value of the root : 2
    ================================================================================

    ********************************************************************************

    Itteration             1
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.00000
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.00000   4.00000   4.00000
    --------------------------------------------------------------------------------

    1.00000   2.00000   2.00000   -1.00000

    0.00000   2.00000   8.00000
    --------------------------------------------------------------------------------

    1.00000   4.00000   10.00000

    --------------------------------------------------------------------------------

    fx = -1.00000  f1x = 10.00000
     x1 = 2.10000
    ================================================================================

    ********************************************************************************

    Itteration             2
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.10000
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.10000   4.41000   5.06100
    --------------------------------------------------------------------------------

    1.00000   2.10000   2.41000   0.06100

    0.00000   2.10000   8.82000
    --------------------------------------------------------------------------------

    1.00000   4.20000   11.23000

    --------------------------------------------------------------------------------

    fx = 0.06100  f1x = 11.23000
     x1 = 2.09457
    ================================================================================

    ********************************************************************************

    Itteration             3
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.09457
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.09457   4.38722   5.00019
    --------------------------------------------------------------------------------

    1.00000   2.09457   2.38722   0.00019

    0.00000   2.09457   8.77443
    --------------------------------------------------------------------------------

    1.00000   4.18914   11.16165

    --------------------------------------------------------------------------------

    fx = 0.00019  f1x = 11.16165
     x1 = 2.09455
    ================================================================================

    ********************************************************************************

    Itteration             4
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.09455
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.09455   4.38715   5.00000
    --------------------------------------------------------------------------------

    1.00000   2.09455   2.38715   0.00000

    0.00000   2.09455   8.77429
    --------------------------------------------------------------------------------

    1.00000   4.18910   11.16144

    --------------------------------------------------------------------------------

    fx = 0.00000  f1x = 11.16144
     x1 = 2.09455
    ================================================================================

    The root of the equation :-
    1.00000x^3 -2.00000x^1 -5.00000x^0  is 2.09455
    ================================================================================


    */


    Saturday, June 2, 2007 4:28 AM
  • @Varun - Awesome work man. Shall be really very very useful for others. Cause everyone either in BCA or MCA or may be also BE will have to come across these kind of problems.

    I had these problems to solve in my BCA, sad we dont got it in MCA. I have done all the programs you had listed during my BCA. But one think i liked of yours, that accepting the equation at the  runtime. When i was in BCA, i was feeling it really tough to accept the equations in runtine in the form of a string  so i had to fix the equation before compilation Stick out tongue

    Your runtime code was nice, and good logic you used  Also that "Birge-Vieta" is new for me.
    Saturday, June 2, 2007 5:11 AM
  • Birge Vieta is a combination of Newton Raphson and Synthetic division,
    In Birge-Vieta we calculate the root using the N-R method, but we calculate f(x) and f'(x) using the synthetic division...
    By use of synthetic division, lots and lots of operations are saved, that is the reason it is widely used in the computers, If you observe the method for a smalled degree polynomial, you wont find much difference, but take a higher degree polynomial, you will find how useful it is and how fast it is...

    And for the equations, I used the C++ technique of Class and created a class of equation, so now whenever i need to use a runtime equation, i use this class and all the operations of reading and displaying the equation and value of f(x) are defined in the class, this makes it very useful...
    Saturday, June 2, 2007 6:54 AM
  • Thanks for explaining how that Virge Vieta works Smile I now understand it.

    I have seen that class you have used for the runtime equation. Its really nice, and decrease the coding. At the time i did the coding for the problems we wasn't taught C++, so the class thing was out of the question, but there was structures to be used, but at that time my C logic wasn't that good. But i must say, really a good idea to use the class . I am impressed.
    Saturday, June 2, 2007 7:32 AM
  • Ok, for the next 2 codes that i m putting down, i had nearly exhausted the whole half day today making it out.. but its a beautiful code and will work out very well, its for the system of polynomial equations, I have never found any such code before and so i think you will love it...

    It is a general program and you can give the system of equations of any degree, it will work out very fine, giving out the solutions....
    Saturday, June 2, 2007 6:03 PM
  • Code Snippet

    // This program will Solve the series of equations using Gauss-Jacobi method


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    class equation
    {
    public :
        int *x;
        int degree;

        void get(int order)
        {
            degree = order;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x"<<i<<" : ";
                cin>>x[i];
            }
        }

        void display()
        {
            for(int i = degree; i>0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x"<<i<<" ";
            }
            cout<<" = "<<-x[0];
        }

        void operator =(equation &f)
        {
            for(int i=degree; i>=0; i--)
                x[i] = f.x[i];
        }
    };

    void main()
    {
        equation *f;
        double *r0, *r1;
        int order, i,j,k, precision, counter =0;
        printline('=');
        cout<<"Please give the number of equations & variables in the system : ";
        cin>>order;

        f = new equation[order+1];
        r0 = new double[order+1];
        r1 = new double[order+1];

        cout<<"Please give the Coefficients of the Equations : "<<endl;
        for(i=0; i<order; i++)
        {
            cout<<"Equation No : "<<i+1<<endl;
            f[i].get(order);
        }

    //    Checking the S.D.D. form of the equations...

        
            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += fabs(f[order - j].x[k]);
                }
                if(fabs(f[order - j].x[j]) <= tmp)
                {
                    cout<<"Sorry the given equations are not in S.D.D form";
                    exit(1);
                }
            }

        printline('-');
        cout<<"Enter the number of decimal places of accuracy : ";
        cin>>precision;

        double epsilon = pow(10, -precision);

        printline('-');

        cout<<"N";
        for(i=order;i>0; i--)
        {
            cout<<setw(precision + 5)<<"x"<<i;
        }
        cout<<endl;

    //    Taking the initial values of the roots as 0.
        for(i=order;i>0;i--)
            r1[i] = 0;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);
    //    Calculating the Value of x1, x2.... using the Gauss-Jacobi formula...

        do
        {

            counter++;
            for(i=order;i>0;i--)
                r0[i] = r1[i];

            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += f[order - j].x[k] * r0[k];
                }
                r1[j] = (-f[order - j].x[0] - tmp) / f[order - j].x[j] ;
            }

            cout<<setw(4)<<counter;
            for(i=order;i>0; i--)
            {
                cout<<setw(precision + 5)<<r1[i];
            }
            cout<<endl;
        }while(counter <100 && fabs(r1[1] - r0[1]) > epsilon);

        printline('~');
        cout<<"The solution for the given system of equations :\n";
        for(i=0; i<order; i++)
        {
            f[i].display();
            cout<<endl;
        }
        cout<<"---------------------------------------\n";
        for(i=order; i>0; i--)
        {
            cout<<" x"<<i<<" = "<<r1[i]<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of equations & variables in the system : 3
    Please give the Coefficients of the Equations :
    Equation No : 1
    Please give the co-efficiet of x3 : 8
    Please give the co-efficiet of x2 : -3
    Please give the co-efficiet of x1 : 2
    Please give the co-efficiet of x0 : -20
    Equation No : 2
    Please give the co-efficiet of x3 : 4
    Please give the co-efficiet of x2 : 11
    Please give the co-efficiet of x1 : -1
    Please give the co-efficiet of x0 : -33
    Equation No : 3
    Please give the co-efficiet of x3 : 1
    Please give the co-efficiet of x2 : 1
    Please give the co-efficiet of x1 : 4
    Please give the co-efficiet of x0 : -9
    --------------------------------------------------------------------------------

    Enter the number of decimal places of accuracy : 3
    --------------------------------------------------------------------------------

    N       x3       x2       x1
    1   2.500   3.000   2.250
    2   3.063   2.295   0.875
    3   3.142   1.966   0.911
    4   3.010   1.940   0.973
    5   2.984   1.994   1.013
    6   2.995   2.007   1.005
    7   3.001   2.002   1.000
    8   3.001   2.000   0.999
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The solution for the given system of equations :
    8x3 -3x2 2x1  = 20
    4x3 11x2 -1x1  = 33
    1x3 1x2 4x1  = 9
    ---------------------------------------
     x3 = 3.001
     x2 = 2.000
     x1 = 0.999
    ================================================================================

    */


    Saturday, June 2, 2007 6:04 PM
  • Code Snippet

    // This program will Solve the series of equations using Gauss-Seidle method


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    class equation
    {
    public :
        int *x;
        int degree;

        void get(int order)
        {
            degree = order;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x"<<i<<" : ";
                cin>>x[i];
            }
        }

        void display()
        {
            for(int i = degree; i>0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x"<<i<<" ";
            }
            cout<<" = "<<-x[0];
        }

        void operator =(equation &f)
        {
            for(int i=degree; i>=0; i--)
                x[i] = f.x[i];
        }
    };

    void main()
    {
        equation *f;
        double *r0, *r1;
        int order, i,j,k, precision, counter =0;
        printline('=');
        cout<<"Please give the number of equations & variables in the system : ";
        cin>>order;

        f = new equation[order+1];
        r0 = new double[order+1];
        r1 = new double[order+1];

        cout<<"Please give the Coefficients of the Equations : "<<endl;
        for(i=0; i<order; i++)
        {
            cout<<"Equation No : "<<i+1<<endl;
            f[i].get(order);
        }

    //    Checking the S.D.D. form of the equations...

        
            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += fabs(f[order - j].x[k]);
                }
                if(fabs(f[order - j].x[j]) <= tmp)
                {
                    cout<<"Sorry the given equations are not in S.D.D form";
                    exit(1);
                }
            }

        printline('-');
        cout<<"Enter the number of decimal places of accuracy : ";
        cin>>precision;

        double epsilon = pow(10, -precision);

        printline('-');

        cout<<"N";
        for(i=order;i>0; i--)
        {
            cout<<setw(precision + 5)<<"x"<<i;
        }
        cout<<endl;

    //    Taking the initial values of the roots as 0.
        for(i=order;i>0;i--)
            r1[i] = 0;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);
    //    Calculating the Value of x1, x2.... using the Gauss-Jacobi formula...

        do
        {

            counter++;
            for(i=order;i>0;i--)
                r0[i] = r1[i];

            for(j=order; j>0; j--)
            {
                double tmp = 0;
            
                for(k=order; k>j; k--)
                {
                    tmp += f[order - j].x[k] * r1[k];
                }
                for(k = j-1; k>0; k--)
                {
                    tmp += f[order - j].x[k] * r0[k];
                }
                r1[j] = (-f[order - j].x[0] - tmp) / f[order - j].x[j] ;
            }

            cout<<setw(4)<<counter;
            for(i=order;i>0; i--)
            {
                cout<<setw(precision + 5)<<r1[i];
            }
            cout<<endl;
        }while(counter <100 && fabs(r1[1] - r0[1]) > epsilon);

        printline('~');
        cout<<"The solution for the given system of equations :\n";
        for(i=0; i<order; i++)
        {
            f[i].display();
            cout<<endl;
        }
        cout<<"---------------------------------------\n";
        for(i=order; i>0; i--)
        {
            cout<<" x"<<i<<" = "<<r1[i]<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of equations & variables in the system : 3
    Please give the Coefficients of the Equations :
    Equation No : 1
    Please give the co-efficiet of x3 : 8
    Please give the co-efficiet of x2 : -3
    Please give the co-efficiet of x1 : 2
    Please give the co-efficiet of x0 : -20
    Equation No : 2
    Please give the co-efficiet of x3 : 4
    Please give the co-efficiet of x2 : 11
    Please give the co-efficiet of x1 : -1
    Please give the co-efficiet of x0 : -33
    Equation No : 3
    Please give the co-efficiet of x3 : 1
    Please give the co-efficiet of x2 : 1
    Please give the co-efficiet of x1 : 4
    Please give the co-efficiet of x0 : -9
    --------------------------------------------------------------------------------

    Enter the number of decimal places of accuracy : 3
    --------------------------------------------------------------------------------

    N       x3       x2       x1
    1   2.500   2.091   1.102
    2   3.009   2.006   0.996
    3   3.003   1.998   1.000
    4   3.000   2.000   1.000
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The solution for the given system of equations :
    8x3 -3x2 2x1  = 20
    4x3 11x2 -1x1  = 33
    1x3 1x2 4x1  = 9
    ---------------------------------------
     x3 = 3.000
     x2 = 2.000
     x1 = 1.000
    ================================================================================

    */


    Saturday, June 2, 2007 6:04 PM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Newtons Backward Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    long int fact(int a)
    {
        long int tmp = 1;
        for(int i =a; i > 0; i--)
            tmp *= i;
        return tmp;
    }

    void main()
    {

        double *x, *y, xi, yi, h, u, **dy;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double[n];

        dy = new double*[n-1];
        for(i=0; i<n-1; i++)
        {
            dy[i] = new double[n-i-1];
        }

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[i];
        }

        printline('=');
        cout<<setw(15)<<"Newton's Backward Interpolation"<<endl;
        printline('=');

        cout<<setw(15)<<"Backward difference table "<<endl;
        printline('-');

    //    Calculating the values of the forward table....

        for(i=0; i<n-1; i++)
        {
            dy[0][i] = y[i+1] - y[i];
        }

        for(i=1; i<n-1; i++)
        {
            for(j=0; j<n-i-1; j++)
            {
                dy[i][j] = dy[i-1][j+1] - dy[i-1][j];
            }
        }
        

        cout<<"x"<<setw(8)<<"y";
        for(i=0; i<n-1; i++)
            cout<<setw(9)<<"\\/"<<i+1;
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(8)<<x[i]<<setw(8)<<y[i];
            for(j=0; j<i; j++)
            {
                    cout<<setw(10)<<dy[j][i-j-1];
            }
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        h = x[1] - x[0];
        u = (xi - x[n-1])/h;

        yi = y[n-1];
        for(i=0; i<n-1; i++)
        {
            double tmp = 1;
            for(j=0; j<=i; j++)
            {
                tmp *= (u+j);
            }
            yi += tmp*dy[i][n-i-2] / fact(i+1);
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');
    }

    /*..................................Output...................................

    ================================================================================
    ================================================================================

    Please give the number of inputs value of x and y : 5
    Please give the input values :
    Value of x1 = 0
    Value of y1 = .3989
    Value of x2 = .5
    Value of y2 = .3521
    Value of x3 = 1
    Value of y3 = .2420
    Value of x4 = 1.5
    Value of y4 = .1245
    Value of x5 = 2
    Value of y5 = .0540
    ================================================================================

    Newton's Backward Interpolation
    ================================================================================

    Backward difference table
    --------------------------------------------------------------------------------

    x       y       \/1       \/2       \/3       \/4
    0.0000  0.3989
    0.5000  0.3521  -0.0468
    1.0000  0.2420  -0.1101   -0.0633
    1.5000  0.1245  -0.1175   -0.0074   0.0559
    2.0000  0.0540  -0.0705   0.0470    0.0544    -0.0015
    --------------------------------------------------------------------------------

    Enter the value of x : 1.8
    The value of y for x = 1.8000 is 0.0731
    ================================================================================

    */


    Sunday, June 3, 2007 11:52 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Newtons Divided Difference Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    long int fact(int a)
    {
        long int tmp = 1;
        for(int i =a; i > 0; i--)
            tmp *= i;
        return tmp;
    }

    void main()
    {

        double *x, **y, xi, yi;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double*[n];

        for(i=0; i<n; i++)
        {
            y[i] = new double[n-i];
        }

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[0][i];
        }

        printline('=');
        cout<<setw(15)<<"Newton's Divided Difference Interpolation"<<endl;
        printline('=');

        cout<<setw(15)<<"Divided Difference table "<<endl;
        printline('-');

    //    Calculating the values of the Divided Difference table....

        for(i=1; i<n; i++)
        {
            for(j=0; j<n-i; j++)
            {
                y[i][j] = (y[i-1][j+1] - y[i-1][j]) / (x[j+i] - x[j]);
            }
        }

        cout<<"x"<<setw(10)<<"y";
        for(i=0; i<n-1; i++)
            cout<<setw(11)<<"/|\\"<<i+1;
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(10)<<x[i]<<setw(10)<<y[0][i];
            for(j=1; j<n-i; j++)
            {
                    cout<<setw(12)<<y[j][i];
            }
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        yi = y[0][0];
        for(i=1; i<n; i++)
        {
            double tmp = 1;
            for(j=0; j<i; j++)
            {
                tmp *= (xi-x[j]);
            }
            yi += tmp*y[i][0];
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');

    }

    /*..................................Output...................................

    ================================================================================

    Please give the number of inputs value of x and y : 4
    Please give the input values :
    Value of x1 = 2
    Value of y1 = 10
    Value of x2 = 4
    Value of y2 = 26
    Value of x3 = 7
    Value of y3 = 65
    Value of x4 = 9
    Value of y4 = 101
    ================================================================================

    Newton's Divided Difference Interpolation
    ================================================================================

    Divided Difference table
    --------------------------------------------------------------------------------

    x         y        /|\1        /|\2        /|\3
    2.0000    10.0000   8.0000      1.0000      0.0000
    4.0000    26.0000   13.0000     1.0000
    7.0000    65.0000   18.0000
    9.0000    101.0000
    --------------------------------------------------------------------------------

    Enter the value of x : 5
    The value of y for x = 5.0000 is 37.0000
    ================================================================================

    */


    Sunday, June 3, 2007 11:52 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Langrange's  Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }


    void main()
    {

        double *x, *y, xi, yi;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double[n];

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[i];
        }

        printline('=');
        cout<<setw(15)<<"Langrange's Interpolation"<<endl;
        printline('=');

    //    Calculating the values of the Divided Difference table....


        cout<<"x"<<setw(10)<<"y";
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(10)<<x[i]<<setw(10)<<y[i];
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        yi = 0;

        for(i=0; i<n; i++)
        {
            double tmp = 1;
            for(j=0; j<n; j++)
            {
                if(i!=j)
                    tmp *= (xi - x[j])/(x[i] - x[j]);
            }
            yi += tmp*y[i];
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');

    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of inputs value of x and y : 4
    Please give the input values :
    Value of x1 = 2
    Value of y1 = 10
    Value of x2 = 4
    Value of y2 = 26
    Value of x3 = 7
    Value of y3 = 65
    Value of x4 = 9
    Value of y4 = 101
    ================================================================================

    Langrange's Interpolation
    ================================================================================

    x         y
    2.0000    10.0000
    4.0000    26.0000
    7.0000    65.0000
    9.0000    101.0000
    --------------------------------------------------------------------------------

    Enter the value of x : 5
    The value of y for x = 5.0000 is 37.0000
    ================================================================================

    */


    Sunday, June 3, 2007 11:53 AM
  • Nice Programs Varun. Keep em coming Smile Especially those general equations, where you can enter as many number of equations. haha thats nice.

    Now time to start some low level C again Smile
    Sunday, June 3, 2007 12:02 PM

  • Code Snippet

    /*
    [42] Write a program to print the pattern :
                *             *
                * *         * *
                * * *     * * *
                * * * * * * * *
                * * * * * * * *

    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n*2-2;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            if(i!=n) {
                for (j=1 ; j<=i ; j++)
                {
                    printf("* ");
                }
                for(j=1 ; j<=2*(n-i)-2 ; j++) {
                    printf("  ");
                }
                for (j=1 ; j<=i ; j++)
                {
                    printf("* ");
                }
            }
            else {
                for(j=1 ; j<=n*2-2 ; j++) {
                    printf("* ");

                }
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [43] Write a program to print the pattern :
                      *     For n=5
                    *   *
                  *   *   *
                *   *   *   *
              *   *   *   *   *
                *   *   *   *
                  *   *   *
                    *   *
                      *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("*   ");
            }
        }
        for(sp+=2,i=n-1 ; i>=1 ; i--, printf("\n")) {
            printf("%*c",sp++*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("*   ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [44] Write a program to print the pattern :
            Enter a number : 5
            * * * * *
            *       *
            *       *
            *       *
            *       *
            *       *
            * * * * *
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,j,n=10;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n+2 ; i++,printf("\n")) {
            for(j=1 ; j<=n ; j++) {
                if(i==1 || j==1 || i==n+2 || j==n )
                    printf("* ");
                else
                    printf("  ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [45] Write a program to print the pattern :
             A
             A B
             A B C
             A B C D
             A B C D E
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",j+'A'-1);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [46] Write a program to print the pattern :
             Z
             Z Y
             Z Y X
             Z Y X W
             Z Y X W V
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,ch='Z';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (ch='Z',j=1 ; j<=i ; j++)
            {
                printf("%2c",ch--);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [47] Write a program to print the pattern :
                         A
                       A   B
                     A   B   C
                   A   B   C   D
                 A   B   C   D   E
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40,ch='A';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (ch='A',j=1 ; j<=i ; j++)
            {
                printf("%4c",ch++);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [48] Write a program to print the pattern :
                           Z
                         Z   Y
                       Z   Y   X
                     Z   Y   X   W
                   Z   Y   X   W   V
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40,ch='Z';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (ch='Z',j=1 ; j<=i ; j++)
            {
                printf("%4c",ch--);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [49] Write a program to print the pattern :
              1
              1  4
              1  4  9
              1  4  9 16
              1  4  9 16 25
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",j*j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [50] Write a program to print the pattern :
                         1
                      4  1
                   9  4  1
               16  9  4  1
            25 16  9  4  1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",(i-j+1)*(i-j+1));
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [51] Write a program to print the pattern :
            Enter a number : 5
                             0
                          1  0  1
                       2  1  0  1  2
                    3  2  1  0  1  2  3
                 4  3  2  1  0  1  2  3  4
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=0 ; i<n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=-i ; j<=i ; j++)    {
                printf("%3d",(j<0)?j*-1:j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [52] Write a program to print the pattern :
        Enter a number : 5
                         0
                     -1  0  1
                  -2 -1  0  1  2
               -3 -2 -1  0  1  2  3
            -4 -3 -2 -1  0  1  2  3  4
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=0 ; i<n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=-i ; j<=i ; j++)    {
                printf("%3d",j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [53] Write a program to print the pattern :
        A diamond pattern is formed with a given letter of the
        alphabet. Write a program to generate and display such
        by accepting any character and total number of lines (say N).  
        Assume  N is an odd number.
            For example, if the accepted character is 'H'
            and N = 7,  then your program should display the
            following output :

                      H
                    H   H
                  H   H   H
                H   H   H   H
                  H   H   H
                    H   H
                      H
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp,ch;
        clrscr();
        printf("Enter an odd number : ");
        scanf("%d",&n);
        sp=n;
        printf("Enter a character : ");
        scanf(" %c",&ch);
        for(i=1 ; i<=n/2+1 ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("%c   ",ch);
            }
        }
        for(sp+=2,i=n/2 ; i>=1 ; i--, printf("\n")) {
            printf("%*c",sp++*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("%c   ",ch);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [54] Write a program to print the pattern :
        A pattern is constructed by stacking up 3 basic triangles
        formed with stars. The figure below shows each basic triangle
        having 3 layers of stars :
                Enter an odd number : 5
                                          *
                                        * * *
                                      * * * * *
                                    * * * * * * *
                                  * * * * * * * * *
                                  *               *
                                * * *           * * *
                              * * * * *       * * * * *
                            * * * * * * *   * * * * * * *
                          * * * * * * * * * * * * * * * * *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp,ch;
        clrscr();
        printf("Enter an odd number : ");
        scanf("%d",&n);
        sp=40;
        for(i=1 ; i<=n*2 ; i+=2, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("* ");
            }
        }
        for(sp+=2,i=1 ; i<=n*2 ; i+=2, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("* ");
            }
            for(j=1 ; j<=2*n-3-i+1 ;j++) {
                printf("  ");
            }
            for (j=1 ; j<=i ; j++)    {
                if(i==n*2-1 && j==1);
                else printf("* ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [55]    You are given two 4-digit positive integers. Write a program to
        calculate and print out the sum of the products of each pair of
        digits occupying the same position in the two numbers. For example,
        if first number is 3445 and second number is 4534, then output will
        be 64 (3*4+4*5+4*3+5*4=64).
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int no1,no2,sum=0;
        clrscr();
        do {
            printf("Enter no1 [Only 4 Digited]  : ");
            scanf("%d",&no1);
            printf("\n");
        }while(no1<1000 || no1>9999);
        do {
            printf("Enter no2 [Only 4 Digited] : ");
            scanf("%d",&no2);
            printf("\n");
        } while(no2<1000 || no2>9999);
        while(no1!=0) {
            sum+=((no1%10)*(no2%10));
            no1/=10;
            no2/=10;
        }
        printf("ANS = %d",sum);
        getch();


    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [56]    To print ASCII value of all characters.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int i=0,flag=0;
        clrscr();
        for(i=0 ; i<=255 ; i++)
        {       if(i>=186 && flag==0)
            {
                printf("\nPress any key to continue...");
                getch();
                printf("\r");
                flag=1;
            }
            printf("%4d = %c  ",i,i);

        }
        getch();
    }
    Sunday, June 3, 2007 12:10 PM

  • Code Snippet

    /*
    [57]    To print the series :  2,  10,  30,  68,  130 and so on
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,n,cnt=1;
        clrscr();
        printf("Enter a number  : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++) {
            printf("%4d",(i*i*i)+i);
        }
        getch();

    }
    Sunday, June 3, 2007 12:10 PM

  • Code Snippet

    /*
    [58]    To print the series :  1,  3,  6,  10,  15,  21, and so on    
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,n,cnt=1;
        clrscr();
        printf("Enter a number  : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++) {
            printf("%3d",cnt);
            cnt+=i+1;
        }
        getch();

    }
    Sunday, June 3, 2007 12:11 PM

  • Code Snippet

    /*
    [59]    Write a program to print circular matrix.
        For example,
          if N= 1,    N=2        N=3        N=4
     
        1    1  2        1  2  3         1  2  3  4
             4  3        8  9  4        12 13 14  5
                         7  6  5        11 16 15  6
                                        10  9  8  7
    */                                
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int x,y,i,j,n=5,r=0,c=0,cnt=1;
        int arr[10][10]={0};
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        x=n;
        for(i=1 ; i<=n*n ; i++)
        {
            for(j=1 ; j<=x ; j++)
                arr[r][c++]=cnt++;
            for(r++,c--,j=1 ; j<=x-1 ; j++)
                arr[r++][c]=cnt++;
            for(r--,c--,j=1 ; j<=x-1 ; j++)
                arr[r][c--]=cnt++;
            for(c++,r--,j=1 ; j<=x-2 ; j++)
                arr[r--][c]=cnt++;
            r++;c++;x-=2;
        }
        for(i=0 ; i<n ; i++)
        {
            for(j=0 ; j<n ; j++)
            {
                printf("%3d",arr[i][j]);
            }
            printf("\n");
        }
        getch();

    }
    Sunday, June 3, 2007 12:11 PM

  • Code Snippet

    /*
    [60]    Generate the following "pyramid" of digits.
                        1
                      2 3 2
                    3 4 5 4 3
                  4 5 6 7 6 5 4
                5 6 7 8 9 8 7 6 5
              6 7 8 9 0 1 0 9 8 7 6
            7 8 9 0 1 2 3 2 1 0 9 8 7
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>

    void main() {
        int i,j,n,sp=40,cnt=0;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);

        for(i=1 ; i<=n ; i++) {
            printf("%*c",sp-=2,' ');
            for(j=i,cnt=i ; j>=1 ; j--,cnt++) {
                printf("%2d",abs(cnt%10));
            }
            for(j=1,cnt-=2 ; j<i ; j++,cnt--) {
                printf("%2d",abs(cnt%10));
            }

            printf("\n");
        }
        getch();
    }
    Sunday, June 3, 2007 12:12 PM

All replies

  • oh nice of you to take the initiative in case of forums actual starters, absence..anyways lets get rolling...
    Thursday, May 24, 2007 1:36 PM
  • where's the problem?
    Saturday, May 26, 2007 1:23 PM
  • ok here's the problem.
    though i have posted the same question on the other forum.
    WAP to print 1 to 100 without using any looping technique, goto or recursion or indirect recursion in C
    Saturday, May 26, 2007 1:24 PM
  • @Sunil, cant figure out the answer to this. I think you should post the answer.
    Saturday, May 26, 2007 5:41 PM
  • truly speaking, i'm also looking for it's answer.
    though i know a little bit logic.
    here, either we have to make use of a stack
    or make use of functions returning pointers.
    buddy, if you'll get the solution, do post it here
    Sunday, May 27, 2007 12:37 PM
  • Well i m not posting the answer, but i will tell you the logic.. though it sounds wierd but it might just work....

    ok here it goes...
    take a string and initialize it with the numbers till 100,
    like char a[]="1  2  3  4  5  6...... 100";

    now a is a pointer to this string, take as an input from the user till which number he wants to print the nummbers.. lets say in n;

    now give this cmd:
    cout.write(a, n*3);

    what this will do is print the string a only till the length n*3,
    ie is n =1; only 1 will get printed, and if n = 2, only 1   2   will get printed....

    Sunday, May 27, 2007 1:41 PM
  • Now i m going to start programming for my assignment of 2nd sem from tommorrow, so if there is any problems, you all are most welcome to post it here, i will program them and give it here and also have them for my assginment file, so 2 birds with one arrow....

    After 1 week i will post some of my work
    Sunday, May 27, 2007 1:45 PM
  • itzy bitzy solution.
    i would like to point out some deficiences
    1)what if user inputs a number >100.
    2) you are taking much memory.
    30 i told you to write a program in C. if you want to do it, there's  much better way
    Monday, May 28, 2007 7:00 AM
  • itzy bitzy solution.
    i would like to point out some deficiences
    1)what if user inputs a number >100.
    2) you are taking much memory.
    30 i told you to write a program in C. if you want to do it, there's  much better way
    Monday, May 28, 2007 7:01 AM
  • Well Sunil, then post the answer... I used this method because of your restrictions... and there is something like write cmd in C also, but i dont remmber is rite now..

    if you have something else in mind then please post it, or give us what you ave in mind...
    Monday, May 28, 2007 4:41 PM
  • truly speaking, i'm also looking for it's answer.
    though i know a little bit logic.
    here, either we have to make use of a stack
    or make use of functions returning pointers.
    buddy, if you'll get the solution, do post it here
    Tuesday, May 29, 2007 1:51 AM
  • @Sunil - Do you know whether its possible or not ? or you are just trying to find a novel way to do it ?
    Tuesday, May 29, 2007 4:38 PM
  • yaar, this question was asked in the NSIT tech event.
    surely, answer is possible.
    all we have to do is explore it.
    answer surely should be using memory stack or functions returning pointers
    Wednesday, May 30, 2007 1:28 AM
  •  Harshil_Patel_03b5f2 wrote:
    Ok guys this thread is a continuation to the thread "Gimme C/C++ Programs Definitions.."

    As the pages of that thread increased, it was becoming hard to navigate, i guess it was a good decision to start a new thread about the same topic.

     

    Dear Harshil,

     

    Thank you Harshil for starting a new thread. I really do not mind about you starting a new thread. Actually i had exams, and so i was not able to be much online. All i watned is the C/C++ program definitions, whether its in my thread or yours, it does not matter. I am here  to learn. You can continue with the posting of the rest of the programs. i have seen many programs in my thread, i wish you continue them here.

     

     

    Regards.

    Thursday, May 31, 2007 9:59 AM
  • Thanks Mahesh. I thought you would oppose my action of starting a new thread. You seem to be cool Smile

    Ill start continuing posting the programs.
    Thursday, May 31, 2007 6:25 PM

  • Code Snippet

    /*
    [36] Write a program to print the pattern :
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
    int i,j,n,sp;
    clrscr();
    printf("Enter a number : ");
    scanf("%d",&n);
    sp=0;
    for(i=n ; i>=1 ; i--, printf("\n")) {
    printf("%*c",sp+=3,' ');
    for (j=1 ; j<=i ; j++)
    {
    printf("%3d",j);
    }
    }
    getch();
    }
    Thursday, May 31, 2007 6:27 PM

  • Code Snippet


    /*
    [37] Write a program to print the pattern :
                A B C D E
                A B C D
                A B C
                A B
                A
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for (ch='A', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:28 PM
  • Code Snippet

    /*
    [35] Write a program to print the pattern :
                5 4 3 2 1
                5 4 3 2
                5 4 3
                5 4
                5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=n ; i>=1 ; i--, printf("\n")) {
    //        printf("%*c",sp--*3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",n-j+1);
            }
        }
        getch();
    }


    Thursday, May 31, 2007 6:28 PM

  • Code Snippet

    /*
    [34] Write a program to print the pattern :
                        a
                      b b
                    c c c
                  d d d d
                e e e e e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",i+'a'-1);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [40] Write a program to print the pattern :
                        *
                      *   *
                    *   *   *
                  *   *   *   *   
                *   *   *   *   *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%4c",'*');
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [39] Write a program to print the pattern :
                        a
                      a   b
                    a   b   c
                  a   b   c   d
                a   b   c   d   e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",j+'a'-1);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM

  • Code Snippet

    /*
    [38] Write a program to print the pattern :
                a b c d e
                  a b c d
                    a b c
                      a b
                        a
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch = 'a';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for(j=0 ; j<n-i ; j++)    {
                printf("  ");
            }
            for (ch='a', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Thursday, May 31, 2007 6:29 PM
  • Alright, then get ready for some hot new programs, I just made them today itself.. they are of Conm, you will like them....
    Thursday, May 31, 2007 6:51 PM
  • Code Snippet


    // This program displays the use of Bisection method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon;
        getch();
        clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(x1-x2) > epsilon && n <500)
        {
            n++;
            x3 = (x1 + x2) / 2;
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(Fx(x1)*Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006
    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.50000  5.62500  0.50000
    2  2.00000  -1.00000 2.50000  5.62500  2.25000  1.89063  0.25000
    3  2.00000  -1.00000 2.25000  1.89063  2.12500  0.34570  0.12500
    4  2.00000  -1.00000 2.12500  0.34570  2.06250  -0.35132 0.06250
    5  2.06250  -0.35132 2.12500  0.34570  2.09375  -0.00894 0.03125
    6  2.09375  -0.00894 2.12500  0.34570  2.10938  0.16684  0.01563
    7  2.09375  -0.00894 2.10938  0.16684  2.10156  0.07856  0.00781
    8  2.09375  -0.00894 2.10156  0.07856  2.09766  0.03471  0.00391
    9  2.09375  -0.00894 2.09766  0.03471  2.09570  0.01286  0.00195
    10 2.09375  -0.00894 2.09570  0.01286  2.09473  0.00195  0.00098
    11 2.09375  -0.00894 2.09473  0.00195  2.09424  -0.00350 0.00049
    12 2.09424  -0.00350 2.09473  0.00195  2.09448  -0.00077 0.00024
    13 2.09448  -0.00077 2.09473  0.00195  2.09460  0.00059  0.00012
    14 2.09448  -0.00077 2.09460  0.00059  2.09454  -0.00009 0.00006
    15 2.09454  -0.00009 2.09460  0.00059  2.09457  0.00025  0.00003
    16 2.09454  -0.00009 2.09457  0.00025  2.09456  0.00008  0.00002
    17 2.09454  -0.00009 2.09456  0.00008  2.09455  -0.00000 0.00001
    18 2.09455  -0.00000 2.09456  0.00008  2.09455  0.00004  0.00000


    The root of the function x^3 - 2x - 5 is 2.09455

      */


    Thursday, May 31, 2007 6:51 PM
  • Code Snippet


    // This program displays the use of Bisection method to find the root of
    // The equation, given by the user at runtime...

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void clrscr()
    {
        system("cls");
    }



    void main()
    {
        equation F;
        double x1 = 0, x2 = 1, x3;
        double epsilon;
        int degree;
        cout<<endl;

    //    clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<endl;

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(x1-x2) > epsilon && n <500)
        {
            n++;
            x3 = (x1 + x2) / 2;
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<F.Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<F.Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<F.Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(F.Fx(x1)*F.Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5

    Please give the degree of tolerance : 5
     Epsilon : 5e-006
    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.50000  5.62500  0.50000
    2  2.00000  -1.00000 2.50000  5.62500  2.25000  1.89063  0.25000
    3  2.00000  -1.00000 2.25000  1.89063  2.12500  0.34570  0.12500
    4  2.00000  -1.00000 2.12500  0.34570  2.06250  -0.35132 0.06250
    5  2.06250  -0.35132 2.12500  0.34570  2.09375  -0.00894 0.03125
    6  2.09375  -0.00894 2.12500  0.34570  2.10938  0.16684  0.01563
    7  2.09375  -0.00894 2.10938  0.16684  2.10156  0.07856  0.00781
    8  2.09375  -0.00894 2.10156  0.07856  2.09766  0.03471  0.00391
    9  2.09375  -0.00894 2.09766  0.03471  2.09570  0.01286  0.00195
    10 2.09375  -0.00894 2.09570  0.01286  2.09473  0.00195  0.00098
    11 2.09375  -0.00894 2.09473  0.00195  2.09424  -0.00350 0.00049
    12 2.09424  -0.00350 2.09473  0.00195  2.09448  -0.00077 0.00024
    13 2.09448  -0.00077 2.09473  0.00195  2.09460  0.00059  0.00012
    14 2.09448  -0.00077 2.09460  0.00059  2.09454  -0.00009 0.00006
    15 2.09454  -0.00009 2.09460  0.00059  2.09457  0.00025  0.00003
    16 2.09454  -0.00009 2.09457  0.00025  2.09456  0.00008  0.00002
    17 2.09454  -0.00009 2.09456  0.00008  2.09455  -0.00000 0.00001
    18 2.09455  -0.00000 2.09456  0.00008  2.09455  0.00004  0.00000


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:52 PM
  • Code Snippet


    // This program displays the use of False position method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        x3 = x2;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        while(fabs(Fx(x3)) > epsilon && n <500)
        {
            n++;
            x3 = x1 - Fx(x1)*((x2 - x1) / (Fx(x2) - Fx(x1)));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(Fx(x1)*Fx(x3) > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.05882  -0.39080 0.50000
    2  2.05882  -0.39080 3.00000  16.00000 2.08126  -0.14720 0.47059
    3  2.08126  -0.14720 3.00000  16.00000 2.08964  -0.05468 0.45937
    4  2.08964  -0.05468 3.00000  16.00000 2.09274  -0.02020 0.45518
    5  2.09274  -0.02020 3.00000  16.00000 2.09388  -0.00745 0.45363
    6  2.09388  -0.00745 3.00000  16.00000 2.09431  -0.00275 0.45306
    7  2.09431  -0.00275 3.00000  16.00000 2.09446  -0.00101 0.45285
    8  2.09446  -0.00101 3.00000  16.00000 2.09452  -0.00037 0.45277
    9  2.09452  -0.00037 3.00000  16.00000 2.09454  -0.00014 0.45274
    10 2.09454  -0.00014 3.00000  16.00000 2.09455  -0.00005 0.45273
    11 2.09455  -0.00005 3.00000  16.00000 2.09455  -0.00002 0.45273
    12 2.09455  -0.00002 3.00000  16.00000 2.09455  -0.00001 0.45273
    13 2.09455  -0.00001 3.00000  16.00000 2.09455  -0.00000 0.45272


    The root of the function x^3 - 2x - 5 is 2.09455
      */


    Thursday, May 31, 2007 6:53 PM
  • Code Snippet


    // This program displays the use of False position method to find the root of
    // The equation, user defined at the run time..

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void main()
    {
        equation F;
        double x1, x2, x3;
        double f1, f2, f3;
        double epsilon;
        int degree;


        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        x3 = x2;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"x1       "<<"f(x1)    "<<"x2       "<<"f(x2)    "<<"x3       "<<"f(x3)    "<<"Max error"<<endl;
    //    cout.precision(5);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        f3 = F.Fx(x3);
        while(fabs(f3) > epsilon && n <500)
        {
            n++;
            f1 = F.Fx(x1);
            f2 = F.Fx(x2);
            f3 = F.Fx(x3);
            x3 = x1 - f1*((x2 - x1) / (f2 - f1));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<f1
                <<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<f2
                <<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<f3
                <<setw(9)<<setprecision(5)<<fabs((x1-x2) / 2)<<endl;

            if(f1*f2 > 0)
                x1 = x3;
            else
                x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5
    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  x1       f(x1)    x2       f(x2)    x3       f(x3)    Max error
    1  2.00000  -1.00000 3.00000  16.00000 2.05882  16.00000 0.50000
    2  2.00000  -1.00000 2.05882  -0.39080 2.09656  -0.39080 0.02941
    3  2.09656  0.02243  2.05882  -0.39080 2.09451  0.02243  0.01887
    4  2.09656  0.02243  2.09451  -0.00046 2.09455  -0.00046 0.00102
    5  2.09656  0.02243  2.09455  -0.00000 2.09455  -0.00000 0.00100


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:54 PM
  • Code Snippet


    // This program displays the use of Secant method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    void Initial(double &a, double &b)
    {
        while(!(Fx(a)*Fx(b) < 0))
        {
            if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
            {
                b = a;
                a--;
            }
            else
            {
                a = b;
                b++;
            }
        }
    }

    void main()
    {
        double x1, x2, x3;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        Initial(x1, x2);
        x3=x2;
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout<<"-1 "<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<Fx(x1)<<endl;
        cout<<"0  "<<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<Fx(x2)<<endl;

        while(fabs(Fx(x3)) > epsilon && n <500)
        {
            n++;
            x3 = (x1*Fx(x2) - x2*Fx(x1)) / (Fx(x2) - Fx(x1));
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<Fx(x3)<<endl;
            x1 = x2;
            x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  xn       f(xn)
    -1 2.00000  -1.00000
    0  3.00000  16.00000
    1  2.05882  -0.39080
    2  2.08126  -0.14720
    3  2.09482  0.00304
    4  2.09455  -0.00002
    5  2.09455  -0.00000


    The root of the function x^3 - 2x - 5 is 2.09455
      */


    Thursday, May 31, 2007 6:54 PM
  • Code Snippet


    // This program displays the use of Secant method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    class equation
    {
        int *x;
        int degree;
    public :
        equation()
        {
            cout<<"Please give the degree of the equation : ";
            cin>>degree;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x^"<<i<<" : ";
                cin>>x[i];
            }
        }
        double Fx(double xn)
        {
            double tmp = 0;
            for(int i=degree; i>=0; i--)
            {
                tmp += x[i]*pow(xn, i);
            }
            return tmp;
        }
        void display()
        {
            for(int i = degree; i>=0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x^"<<i<<" ";
            }
        }
        void Initial(double &a, double &b)
        {
            while(!(Fx(a)*Fx(b) < 0))
            {
                if(Fx(a) == 0)
                {
                    a--;
                }
                else if(Fx(b) == 0)
                {
                    b++;
                }
                else if(Fx(a)*Fx(b) <= Fx(b)*Fx(b+1))
                {
                    b = a;
                    a--;
                }
                else
                {
                    a = b;
                    b++;
                }
            }
        }

    };

    void main()
    {
        equation F;
        double x1, x2, x3;
        double f1, f2, f3;
        double epsilon;
        int degree;


        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Finding the initial approximate values under which the Root lies...
        x1 = 0;
        x2 = 1;
        F.Initial(x1, x2);
        x3=x2;
        cout<<"Initial approximation : "<<x1<<" " <<x2<<endl;
        getch();

    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout<<"-1 "<<setw(9)<<setprecision(5)<<x1<<setw(9)<<setprecision(5)<<F.Fx(x1)<<endl;
        cout<<"0  "<<setw(9)<<setprecision(5)<<x2<<setw(9)<<setprecision(5)<<F.Fx(x2)<<endl;

        f3 = F.Fx(x3);
        while(fabs(f3) > epsilon && n <500)
        {
            n++;
            f1 = F.Fx(x1);
            f2 = F.Fx(x2);
            f3 = F.Fx(x3);
            x3 = (x1*f2 - x2*f1) / (f2 - f1);
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x3<<setw(9)<<setprecision(5)<<f3<<endl;
            x1 = x2;
            x2 = x3;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function ";
            F.display();
            cout<<" is "<<x3<<endl;
        }
    }


    /*        Output

    Please give the degree of the equation : 3
    Please give the co-efficiet of x^3 : 1
    Please give the co-efficiet of x^2 : 0
    Please give the co-efficiet of x^1 : -2
    Please give the co-efficiet of x^0 : -5
    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Initial approximation : 2 3
    N  xn       f(xn)
    -1 2.00000  -1.00000
    0  3.00000  16.00000
    1  2.05882  16.00000
    2  2.08126  -0.39080
    3  2.09482  -0.14720
    4  2.09455  0.00304
    5  2.09455  -0.00002
    6  2.09455  -0.00000


    The root of the function 1x^3 -2x^1 -5x^0  is 2.09455
      */


    Thursday, May 31, 2007 6:55 PM

  • // This program displays the use of Newton-Raphson method to find the root of
    // The equation, x^3 - 2x - 5 = 0

    #include<iostream>
    #include<conio.h>
    #include<math.h>
    #include<iomanip>

    using namespace std;

    void clrscr()
    {
        system("cls");
    }

    double Fx(double x)
    {
        return(x*(pow(x,2) - 2) - 5);
    }

    double F1x(double x)
    {
        return(3*pow(x,2) - 2);
    }


    void main()
    {
        double x;
        double epsilon;
        int degree;

        clrscr();
        cout<<"Please give the degree of tolerance : ";
        cin>>degree;
        epsilon = pow(10, -1*degree) / 2;
        cout<<" Epsilon : "<<epsilon<<"\n\n";
        getch();
    //    clrscr();

    //    Asking the initial approximate value from the user...
        cout<<"Please give the initial approximate value : ";
        cin>>x;


    //    Finding the root...
        int n = 0;
        cout<<"N  "<<"xn       "<<"f(xn)    "<<"f1(xn)   "<<"f(xn) / f1(xn)"<<endl;
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        while(fabs(Fx(x)) > epsilon && n <500)
        {
            double x1;
            n++;
            x1 = x - Fx(x) / F1x(x);
            cout<<setw(3)<<n<<setw(9)<<setprecision(5)<<x<<setw(9)<<setprecision(5)<<Fx(x)
                <<setw(9)<<setprecision(5)<<F1x(x)<<setw(9)<<setprecision(5)<<Fx(x) / F1x(x)<<endl;
            x = x1;
        }

        if(n==500)
        {
            cout<<"Sorry the method fails ";
            exit(1);
        }
        else
        {
            cout<<"\n\nThe root of the function x^3 - 2x - 5 is "<<x<<endl;
        }
    }


    /*        Output

    Please give the degree of tolerance : 5
     Epsilon : 5e-006

    Please give the initial approximate value : 2
    N  xn       f(xn)    f1(xn)   f(xn) / f1(xn)
    1  2.00000  -1.00000 10.00000 -0.10000
    2  2.10000  0.06100  11.23000 0.00543
    3  2.09457  0.00019  11.16165 0.00002


    The root of the function x^3 - 2x - 5 is 2.09455
      */
    Thursday, May 31, 2007 6:55 PM
  • Good progs Varun.I am first posting low level C programs Smile

    And then i will post harder ones. I have few OR and stats programs Smile ill post em too.
    Thursday, May 31, 2007 7:35 PM
  • Code Snippet

    // This program will find the roots of the equation x^3 -9x + 1 = 0
    // Using Successive method of Approximation.

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    double f(double x)
    {
        return (x*(pow(x,2) -9) + 1);
    }

    double g(double x)
    {
        return ((pow(x,3) + 1 ) / 9);
    }


    void main()
    {
        double x0, x1;
        double epsilon;
        int precision, itteration = 0;

        printline('=');
        cout<<"Enter the decimal places upto where you want precision : ";
        cin>>precision;
        epsilon = pow(10, -precision);
        cout<<"Epsilon : "<<epsilon<<endl;

        cout<<"Please give the 1st approximate value of the root : ";
        cin>>x1;
        if(f(x1) == 0)
        {
            cout<<"The root of the equation is "<<x1;
            exit(1);
        }
        printline('=');
        
        printline('-');
        cout<<setw(3)<<"N"<<setw(precision + 5)<<"x"<<setw(precision + 5)<<"g(x)"<<endl;
        printline('-');

        cout.precision(precision);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        do
        {
            x0 = x1;
            itteration++;
            x1 = g(x0);
            cout<<setw(6)<<itteration<<setw(precision + 5)<<x0<<setw(precision + 5)<<x1<<endl;
        }
        while(fabs(x0-x1)>epsilon && itteration < 500);

        printline('=');

        if(itteration == 500)
        {
            cout<<"Sorry the method fails, as it crosses the limited itterations ";
        }
        else
        {
            cout<<"The root of the equation x^3 - 9x + 1 = 0 is "<<x1<<endl;
            cout<<"The approximated g(x) where g(x) = (x^3 + 1) / 9"<<endl;
        }
        printline('=');

    }

    /*..................................Output...................................
    ================================================================================

    Enter the decimal places upto where you want precision : 8
    Epsilon : 1e-008
    Please give the 1st approximate value of the root : .5
    ================================================================================

    --------------------------------------------------------------------------------

      N            x         g(x)
    --------------------------------------------------------------------------------

    1     0.50000000   0.12500000
    2     0.12500000   0.11132813
    3     0.11132813   0.11126442
    4     0.11126442   0.11126416
    5     0.11126416   0.11126416
    ================================================================================

    The root of the equation x^3 - 9x + 1 = 0 is 0.11126416
    The approximated g(x) where g(x) = (x^3 + 1) / 9
    ================================================================================

    */


    Saturday, June 2, 2007 4:27 AM
  • Code Snippet

    /*  This program will calculate the root of the equation
        x^2 + y^2 = 5
        x^2 - y^2 = 1
        Using Newton Raphson method for 2 variables.
    */

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    double F(double x, double y)
    {
        return (pow(x,2) + pow(y,2) - 5);
    }
    double G(double x, double y)
    {
        return (pow(x,2) - pow(y,2) - 1);
    }
    double Fx(double x, double y)
    {
        return (2*x);
    }
    double Fy(double x, double y)
    {
        return (2*y);
    }
    double Gx(double x, double y)
    {
        return (2*x);
    }
    double Gy(double x, double y)
    {
        return (-2*y);
    }


    void main()
    {

        double f0, f1, f2, g0, g1, g2;
        double x0, x1, y0, y1;
        double D, Dx, Dy ;
        double epsilon;
        int precision, counter=0;

        printline('=');
        cout<<"Enter the Number of decimal digits of precision : ";
        cin>>precision;
        epsilon = pow(10, -precision);
        cout<<"Enter the 1st approx value of x : ";
        cin>>x1;
        cout<<"Enter the 1st approx value of y : ";
        cin>>y1;

        cout.precision(precision);
        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);

        printline('-');
        cout<<setw(3)<<"N"<<setw(precision + 4)<<"x"<<setw(precision + 4)<<"y"
            <<setw(precision + 4)<<"f0"<<setw(precision + 4)<<"f1"
            <<setw(precision + 4)<<"f2"<<setw(precision + 4)<<"g0"
            <<setw(precision + 4)<<"g1"<<setw(precision + 4)<<"g2"
            <<endl;
        printline('-');

        do
        {
            counter++;
            x0 = x1;
            y0 = y1;

            f0 = F(x0, y0);
            f1 = Fx(x0, y0);
            f2 = Fy(x0, y0);
            g0 = G(x0, y0);
            g1 = Gx(x0, y0);
            g2 = Gy(x0, y0);

            D  = (f1 * g2 ) - ( f2 * g1 );
            Dx = (f2 * g0 ) - ( f0 * g2 );
            Dy =((f1 * g0 ) - ( f0 * g1 ))*-1;

            x1 = x0 + Dx / D;
            y1 = y0 + Dy / D;

        cout<<setw(3)<<counter<<setw(precision + 4)<<x0<<setw(precision + 4)<<y0
            <<setw(precision + 4)<<f0<<setw(precision + 4)<<f1
            <<setw(precision + 4)<<f2<<setw(precision + 4)<<g0
            <<setw(precision + 4)<<g1<<setw(precision + 4)<<g2
            <<endl;

        }while( fabs(x0-x1) > epsilon && fabs(y0 - y1) > epsilon && counter < 500);

        printline('=');

        if(counter == 500)
            cout<<"Sorry the method fails ";
        else
        {
            cout<<"The sollution of the following functions :"<<endl;
            cout<<"F(x,y)        =    x^2 + y^2 - 5"<<endl;
            cout<<"G(x,y)        =    x^2 - y^2 - 1"<<endl;
            cout<<"Fx(x,y)        =    2x"<<endl;
            cout<<"Fy(x,y)        =    2y"<<endl;
            cout<<"Gx(x,y)        =    2x"<<endl;
            cout<<"Gy(x,y)        =  -2y"<<endl;
            printline('-');
            cout<<"is "<<" x = "<<x1<<" y = "<<y1<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................

    ================================================================================
    Enter the Number of decimal digits of precision : 5
    Enter the 1st approx value of x : 1
    Enter the 1st approx value of y : 1
    --------------------------------------------------------------------------------

    N  x        y        f0       f1       f2       g0       g1       g2
    --------------------------------------------------------------------------------

    1  1.00000  1.00000  -3.00000 2.00000  2.00000  -1.00000 2.00000  -2.00000
    2  2.00000  1.50000  1.25000  4.00000  3.00000  0.75000  4.00000  -3.00000
    3  1.75000  1.41667  0.06944  3.50000  2.83333  0.05556  3.50000  -2.83333
    4  1.73214  1.41422  0.00032  3.46429  2.82843  0.00031  3.46429  -2.82843
    ================================================================================

    The sollution of the following functions :
    F(x,y)          =       x^2 + y^2 - 5
    G(x,y)          =       x^2 - y^2 - 1
    Fx(x,y)         =       2x
    Fy(x,y)         =       2y
    Gx(x,y)         =       2x
    Gy(x,y)         =      -2y
    --------------------------------------------------------------------------------

    is  x = 1.73205 y = 1.41421
    ================================================================================

    */


    Saturday, June 2, 2007 4:27 AM
  • Code Snippet

    // This program will Calculate the root of a equation
    // Using the Birge-Vieta Method..

    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }


    void main()
    {

        double *f, *f0,*g0, *f1, *g1;
        double x0, x1, fx, f1x;
        int counter=0, order, precision, i;
        double epsilon;

        printline('=');
        cout<<"Enter the order of the polynomial : ";
        cin>>order;

        f = new double[order + 1];
        f0 = new double[order + 1];
        f1 = new double[order + 1];
        g0 = new double[order + 1];
        g1 = new double[order + 1];

        cout<<"Please give the coefficients of the polynomial : "<<endl;
        for(i=order; i>=0; i--)
        {
            cout<<"Coefficient of x^"<<i<<" : ";
            cin>>f[i];
        }

        cout<<"Please give the nuumber of decimal places of accuracy : ";
        cin>>precision;
        epsilon = pow(10, -precision);

        cout<<"Enter the Initial approximate value of the root : ";
        cin>>x1;

        printline('=');

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);

        do
        {
            counter++;
            x0 = x1;
            printline('*');
            cout<<setw(23)<<"Itteration "<<counter<<endl;
            printline('*');
        
            cout<<"Calculation of fx and f'x Using Synthetic devision"<<endl;
            printline('~');
            cout<<setw(20)<<" "<<"x0 = "<<x0<<endl;
            
            f0[order] = f1[order] = 0;
            g0[order] = g1[order] = f[order];
            for(i=order - 1; i>=0; i--)
            {
                f0[i] = g0[i+1] * x0;
                g0[i] = f[i] + f0[i];

                f1[i] = g1[i+1] * x0;
                g1[i] = f1[i] + g0[i];
            }

            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<f[i];
            cout<<"\n\n";
            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<f0[i];
            cout<<"\n";
            printline('-');

            for(i=order; i>=0; i--)
                cout<<setw(precision + 5)<<g0[i];
            cout<<"\n\n";        
            for(i=order; i>0; i--)
                cout<<setw(precision + 5)<<f1[i];
            cout<<"\n";
            printline('-');
            for(i=order; i>0; i--)
                cout<<setw(precision + 5)<<g1[i];
            cout<<"\n\n";

            printline('-');
            fx = g0[0];
            f1x = g1[1];

            cout<<"fx = "<<fx<<"  f1x = "<<f1x<<endl;
            x1 = x0 - fx / f1x;
            cout<<" x1 = "<<x1<<endl;
            printline('=');

        }while(fabs(x1- x0) > epsilon && counter < 10);

            if(counter == 10)
                cout<<"Sorry the method fails";
            else
            {
                cout<<"The root of the equation :- "<<endl;
                for(int i = order; i>=0; i--)
                {
                    if(f[i])
                        cout<<f[i]<<"x^"<<i<<" ";
                }
                cout<<" is "<<x1<<endl;
            }
            printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Enter the order of the polynomial : 3
    Please give the coefficients of the polynomial :
    Coefficient of x^3 : 1
    Coefficient of x^2 : 0
    Coefficient of x^1 : -2
    Coefficient of x^0 : -5
    Please give the nuumber of decimal places of accuracy : 5
    Enter the Initial approximate value of the root : 2
    ================================================================================

    ********************************************************************************

    Itteration             1
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.00000
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.00000   4.00000   4.00000
    --------------------------------------------------------------------------------

    1.00000   2.00000   2.00000   -1.00000

    0.00000   2.00000   8.00000
    --------------------------------------------------------------------------------

    1.00000   4.00000   10.00000

    --------------------------------------------------------------------------------

    fx = -1.00000  f1x = 10.00000
     x1 = 2.10000
    ================================================================================

    ********************************************************************************

    Itteration             2
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.10000
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.10000   4.41000   5.06100
    --------------------------------------------------------------------------------

    1.00000   2.10000   2.41000   0.06100

    0.00000   2.10000   8.82000
    --------------------------------------------------------------------------------

    1.00000   4.20000   11.23000

    --------------------------------------------------------------------------------

    fx = 0.06100  f1x = 11.23000
     x1 = 2.09457
    ================================================================================

    ********************************************************************************

    Itteration             3
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.09457
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.09457   4.38722   5.00019
    --------------------------------------------------------------------------------

    1.00000   2.09457   2.38722   0.00019

    0.00000   2.09457   8.77443
    --------------------------------------------------------------------------------

    1.00000   4.18914   11.16165

    --------------------------------------------------------------------------------

    fx = 0.00019  f1x = 11.16165
     x1 = 2.09455
    ================================================================================

    ********************************************************************************

    Itteration             4
    ********************************************************************************

    Calculation of fx and f'x Using Synthetic devision
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        x0 = 2.09455
    1.00000   0.00000   -2.00000  -5.00000

    0.00000   2.09455   4.38715   5.00000
    --------------------------------------------------------------------------------

    1.00000   2.09455   2.38715   0.00000

    0.00000   2.09455   8.77429
    --------------------------------------------------------------------------------

    1.00000   4.18910   11.16144

    --------------------------------------------------------------------------------

    fx = 0.00000  f1x = 11.16144
     x1 = 2.09455
    ================================================================================

    The root of the equation :-
    1.00000x^3 -2.00000x^1 -5.00000x^0  is 2.09455
    ================================================================================


    */


    Saturday, June 2, 2007 4:28 AM
  • @Varun - Awesome work man. Shall be really very very useful for others. Cause everyone either in BCA or MCA or may be also BE will have to come across these kind of problems.

    I had these problems to solve in my BCA, sad we dont got it in MCA. I have done all the programs you had listed during my BCA. But one think i liked of yours, that accepting the equation at the  runtime. When i was in BCA, i was feeling it really tough to accept the equations in runtine in the form of a string  so i had to fix the equation before compilation Stick out tongue

    Your runtime code was nice, and good logic you used  Also that "Birge-Vieta" is new for me.
    Saturday, June 2, 2007 5:11 AM
  • Birge Vieta is a combination of Newton Raphson and Synthetic division,
    In Birge-Vieta we calculate the root using the N-R method, but we calculate f(x) and f'(x) using the synthetic division...
    By use of synthetic division, lots and lots of operations are saved, that is the reason it is widely used in the computers, If you observe the method for a smalled degree polynomial, you wont find much difference, but take a higher degree polynomial, you will find how useful it is and how fast it is...

    And for the equations, I used the C++ technique of Class and created a class of equation, so now whenever i need to use a runtime equation, i use this class and all the operations of reading and displaying the equation and value of f(x) are defined in the class, this makes it very useful...
    Saturday, June 2, 2007 6:54 AM
  • Thanks for explaining how that Virge Vieta works Smile I now understand it.

    I have seen that class you have used for the runtime equation. Its really nice, and decrease the coding. At the time i did the coding for the problems we wasn't taught C++, so the class thing was out of the question, but there was structures to be used, but at that time my C logic wasn't that good. But i must say, really a good idea to use the class . I am impressed.
    Saturday, June 2, 2007 7:32 AM
  • Ok, for the next 2 codes that i m putting down, i had nearly exhausted the whole half day today making it out.. but its a beautiful code and will work out very well, its for the system of polynomial equations, I have never found any such code before and so i think you will love it...

    It is a general program and you can give the system of equations of any degree, it will work out very fine, giving out the solutions....
    Saturday, June 2, 2007 6:03 PM
  • Code Snippet

    // This program will Solve the series of equations using Gauss-Jacobi method


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    class equation
    {
    public :
        int *x;
        int degree;

        void get(int order)
        {
            degree = order;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x"<<i<<" : ";
                cin>>x[i];
            }
        }

        void display()
        {
            for(int i = degree; i>0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x"<<i<<" ";
            }
            cout<<" = "<<-x[0];
        }

        void operator =(equation &f)
        {
            for(int i=degree; i>=0; i--)
                x[i] = f.x[i];
        }
    };

    void main()
    {
        equation *f;
        double *r0, *r1;
        int order, i,j,k, precision, counter =0;
        printline('=');
        cout<<"Please give the number of equations & variables in the system : ";
        cin>>order;

        f = new equation[order+1];
        r0 = new double[order+1];
        r1 = new double[order+1];

        cout<<"Please give the Coefficients of the Equations : "<<endl;
        for(i=0; i<order; i++)
        {
            cout<<"Equation No : "<<i+1<<endl;
            f[i].get(order);
        }

    //    Checking the S.D.D. form of the equations...

        
            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += fabs(f[order - j].x[k]);
                }
                if(fabs(f[order - j].x[j]) <= tmp)
                {
                    cout<<"Sorry the given equations are not in S.D.D form";
                    exit(1);
                }
            }

        printline('-');
        cout<<"Enter the number of decimal places of accuracy : ";
        cin>>precision;

        double epsilon = pow(10, -precision);

        printline('-');

        cout<<"N";
        for(i=order;i>0; i--)
        {
            cout<<setw(precision + 5)<<"x"<<i;
        }
        cout<<endl;

    //    Taking the initial values of the roots as 0.
        for(i=order;i>0;i--)
            r1[i] = 0;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);
    //    Calculating the Value of x1, x2.... using the Gauss-Jacobi formula...

        do
        {

            counter++;
            for(i=order;i>0;i--)
                r0[i] = r1[i];

            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += f[order - j].x[k] * r0[k];
                }
                r1[j] = (-f[order - j].x[0] - tmp) / f[order - j].x[j] ;
            }

            cout<<setw(4)<<counter;
            for(i=order;i>0; i--)
            {
                cout<<setw(precision + 5)<<r1[i];
            }
            cout<<endl;
        }while(counter <100 && fabs(r1[1] - r0[1]) > epsilon);

        printline('~');
        cout<<"The solution for the given system of equations :\n";
        for(i=0; i<order; i++)
        {
            f[i].display();
            cout<<endl;
        }
        cout<<"---------------------------------------\n";
        for(i=order; i>0; i--)
        {
            cout<<" x"<<i<<" = "<<r1[i]<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of equations & variables in the system : 3
    Please give the Coefficients of the Equations :
    Equation No : 1
    Please give the co-efficiet of x3 : 8
    Please give the co-efficiet of x2 : -3
    Please give the co-efficiet of x1 : 2
    Please give the co-efficiet of x0 : -20
    Equation No : 2
    Please give the co-efficiet of x3 : 4
    Please give the co-efficiet of x2 : 11
    Please give the co-efficiet of x1 : -1
    Please give the co-efficiet of x0 : -33
    Equation No : 3
    Please give the co-efficiet of x3 : 1
    Please give the co-efficiet of x2 : 1
    Please give the co-efficiet of x1 : 4
    Please give the co-efficiet of x0 : -9
    --------------------------------------------------------------------------------

    Enter the number of decimal places of accuracy : 3
    --------------------------------------------------------------------------------

    N       x3       x2       x1
    1   2.500   3.000   2.250
    2   3.063   2.295   0.875
    3   3.142   1.966   0.911
    4   3.010   1.940   0.973
    5   2.984   1.994   1.013
    6   2.995   2.007   1.005
    7   3.001   2.002   1.000
    8   3.001   2.000   0.999
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The solution for the given system of equations :
    8x3 -3x2 2x1  = 20
    4x3 11x2 -1x1  = 33
    1x3 1x2 4x1  = 9
    ---------------------------------------
     x3 = 3.001
     x2 = 2.000
     x1 = 0.999
    ================================================================================

    */


    Saturday, June 2, 2007 6:04 PM
  • Code Snippet

    // This program will Solve the series of equations using Gauss-Seidle method


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    class equation
    {
    public :
        int *x;
        int degree;

        void get(int order)
        {
            degree = order;
            x = new int[degree + 1];
            for(int i=degree; i>=0; i--)
            {
                cout<<"Please give the co-efficiet of x"<<i<<" : ";
                cin>>x[i];
            }
        }

        void display()
        {
            for(int i = degree; i>0; i--)
            {
                if(x[i])
                    cout<<x[i]<<"x"<<i<<" ";
            }
            cout<<" = "<<-x[0];
        }

        void operator =(equation &f)
        {
            for(int i=degree; i>=0; i--)
                x[i] = f.x[i];
        }
    };

    void main()
    {
        equation *f;
        double *r0, *r1;
        int order, i,j,k, precision, counter =0;
        printline('=');
        cout<<"Please give the number of equations & variables in the system : ";
        cin>>order;

        f = new equation[order+1];
        r0 = new double[order+1];
        r1 = new double[order+1];

        cout<<"Please give the Coefficients of the Equations : "<<endl;
        for(i=0; i<order; i++)
        {
            cout<<"Equation No : "<<i+1<<endl;
            f[i].get(order);
        }

    //    Checking the S.D.D. form of the equations...

        
            for(j=order; j>0; j--)
            {
                double tmp = 0;
                for(k=order; k>0; k--)
                {
                    if(j!=k)
                        tmp += fabs(f[order - j].x[k]);
                }
                if(fabs(f[order - j].x[j]) <= tmp)
                {
                    cout<<"Sorry the given equations are not in S.D.D form";
                    exit(1);
                }
            }

        printline('-');
        cout<<"Enter the number of decimal places of accuracy : ";
        cin>>precision;

        double epsilon = pow(10, -precision);

        printline('-');

        cout<<"N";
        for(i=order;i>0; i--)
        {
            cout<<setw(precision + 5)<<"x"<<i;
        }
        cout<<endl;

    //    Taking the initial values of the roots as 0.
        for(i=order;i>0;i--)
            r1[i] = 0;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(precision);
    //    Calculating the Value of x1, x2.... using the Gauss-Jacobi formula...

        do
        {

            counter++;
            for(i=order;i>0;i--)
                r0[i] = r1[i];

            for(j=order; j>0; j--)
            {
                double tmp = 0;
            
                for(k=order; k>j; k--)
                {
                    tmp += f[order - j].x[k] * r1[k];
                }
                for(k = j-1; k>0; k--)
                {
                    tmp += f[order - j].x[k] * r0[k];
                }
                r1[j] = (-f[order - j].x[0] - tmp) / f[order - j].x[j] ;
            }

            cout<<setw(4)<<counter;
            for(i=order;i>0; i--)
            {
                cout<<setw(precision + 5)<<r1[i];
            }
            cout<<endl;
        }while(counter <100 && fabs(r1[1] - r0[1]) > epsilon);

        printline('~');
        cout<<"The solution for the given system of equations :\n";
        for(i=0; i<order; i++)
        {
            f[i].display();
            cout<<endl;
        }
        cout<<"---------------------------------------\n";
        for(i=order; i>0; i--)
        {
            cout<<" x"<<i<<" = "<<r1[i]<<endl;
        }
        printline('=');
    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of equations & variables in the system : 3
    Please give the Coefficients of the Equations :
    Equation No : 1
    Please give the co-efficiet of x3 : 8
    Please give the co-efficiet of x2 : -3
    Please give the co-efficiet of x1 : 2
    Please give the co-efficiet of x0 : -20
    Equation No : 2
    Please give the co-efficiet of x3 : 4
    Please give the co-efficiet of x2 : 11
    Please give the co-efficiet of x1 : -1
    Please give the co-efficiet of x0 : -33
    Equation No : 3
    Please give the co-efficiet of x3 : 1
    Please give the co-efficiet of x2 : 1
    Please give the co-efficiet of x1 : 4
    Please give the co-efficiet of x0 : -9
    --------------------------------------------------------------------------------

    Enter the number of decimal places of accuracy : 3
    --------------------------------------------------------------------------------

    N       x3       x2       x1
    1   2.500   2.091   1.102
    2   3.009   2.006   0.996
    3   3.003   1.998   1.000
    4   3.000   2.000   1.000
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The solution for the given system of equations :
    8x3 -3x2 2x1  = 20
    4x3 11x2 -1x1  = 33
    1x3 1x2 4x1  = 9
    ---------------------------------------
     x3 = 3.000
     x2 = 2.000
     x1 = 1.000
    ================================================================================

    */


    Saturday, June 2, 2007 6:04 PM
  • Alright, time for some more Conm Programs, now for the next section,
    Interpolation...
    Sunday, June 3, 2007 11:50 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Newtons Forward Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    long int fact(int a)
    {
        long int tmp = 1;
        for(int i =a; i > 0; i--)
            tmp *= i;
        return tmp;
    }

    void main()
    {

        double *x, *y, xi, yi, h, u, **dy;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double[n];

        dy = new double*[n-1];
        for(i=0; i<n-1; i++)
        {
            dy[i] = new double[n-i-1];
        }

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[i];
        }

        printline('=');
        cout<<setw(15)<<"Newton's Forward Interpolation"<<endl;
        printline('=');

        cout<<setw(15)<<"Forward difference table "<<endl;
        printline('-');

    //    Calculating the values of the forward table....

        for(i=0; i<n-1; i++)
        {
            dy[0][i] = y[i+1] - y[i];
        }

        for(i=1; i<n-1; i++)
        {
            for(j=0; j<n-i-1; j++)
            {
                dy[i][j] = dy[i-1][j+1] - dy[i-1][j];
            }
        }
        

        cout<<"x"<<setw(8)<<"y";
        for(i=0; i<n-1; i++)
            cout<<setw(9)<<"/\\"<<i+1;
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(8)<<x[i]<<setw(8)<<y[i];
            for(j=0; j<n-i-1; j++)
            {
                    cout<<setw(10)<<dy[j][i];
            }
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        h = x[1] - x[0];
        u = (xi - x[0])/h;

        yi = y[0];
        for(i=0; i<n-1; i++)
        {
            double tmp = 1;
            for(j=0; j<=i; j++)
            {
                tmp *= (u-j);
            }
            yi += tmp*dy[i][0] / fact(i+1);
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');
    }

    /*..................................Output...................................

    ================================================================================

    Please give the number of inputs value of x and y : 5
    Please give the input values :
    Value of x1 = .2
    Value of y1 = 1.6596
    Value of x2 = .22
    Value of y2 = 1.6698
    Value of x3 = .24
    Value of y3 = 1.6804
    Value of x4 = .26
    Value of y4 = 1.6912
    Value of x5 = .28
    Value of y5 = 1.7024
    ================================================================================

    Newton's Forward Interpolation
    ================================================================================

    Forward difference table
    --------------------------------------------------------------------------------

    x       y       /\1       /\2       /\3       /\4
    0.2000  1.6596  0.0102    0.0004    -0.0002   0.0004
    0.2200  1.6698  0.0106    0.0002    0.0002
    0.2400  1.6804  0.0108    0.0004
    0.2600  1.6912  0.0112
    0.2800  1.7024
    --------------------------------------------------------------------------------

    Enter the value of x : .21
    The value of y for x = 0.2100 is 1.6646
    ================================================================================

    */


    Sunday, June 3, 2007 11:51 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Newtons Backward Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    long int fact(int a)
    {
        long int tmp = 1;
        for(int i =a; i > 0; i--)
            tmp *= i;
        return tmp;
    }

    void main()
    {

        double *x, *y, xi, yi, h, u, **dy;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double[n];

        dy = new double*[n-1];
        for(i=0; i<n-1; i++)
        {
            dy[i] = new double[n-i-1];
        }

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[i];
        }

        printline('=');
        cout<<setw(15)<<"Newton's Backward Interpolation"<<endl;
        printline('=');

        cout<<setw(15)<<"Backward difference table "<<endl;
        printline('-');

    //    Calculating the values of the forward table....

        for(i=0; i<n-1; i++)
        {
            dy[0][i] = y[i+1] - y[i];
        }

        for(i=1; i<n-1; i++)
        {
            for(j=0; j<n-i-1; j++)
            {
                dy[i][j] = dy[i-1][j+1] - dy[i-1][j];
            }
        }
        

        cout<<"x"<<setw(8)<<"y";
        for(i=0; i<n-1; i++)
            cout<<setw(9)<<"\\/"<<i+1;
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(8)<<x[i]<<setw(8)<<y[i];
            for(j=0; j<i; j++)
            {
                    cout<<setw(10)<<dy[j][i-j-1];
            }
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        h = x[1] - x[0];
        u = (xi - x[n-1])/h;

        yi = y[n-1];
        for(i=0; i<n-1; i++)
        {
            double tmp = 1;
            for(j=0; j<=i; j++)
            {
                tmp *= (u+j);
            }
            yi += tmp*dy[i][n-i-2] / fact(i+1);
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');
    }

    /*..................................Output...................................

    ================================================================================
    ================================================================================

    Please give the number of inputs value of x and y : 5
    Please give the input values :
    Value of x1 = 0
    Value of y1 = .3989
    Value of x2 = .5
    Value of y2 = .3521
    Value of x3 = 1
    Value of y3 = .2420
    Value of x4 = 1.5
    Value of y4 = .1245
    Value of x5 = 2
    Value of y5 = .0540
    ================================================================================

    Newton's Backward Interpolation
    ================================================================================

    Backward difference table
    --------------------------------------------------------------------------------

    x       y       \/1       \/2       \/3       \/4
    0.0000  0.3989
    0.5000  0.3521  -0.0468
    1.0000  0.2420  -0.1101   -0.0633
    1.5000  0.1245  -0.1175   -0.0074   0.0559
    2.0000  0.0540  -0.0705   0.0470    0.0544    -0.0015
    --------------------------------------------------------------------------------

    Enter the value of x : 1.8
    The value of y for x = 1.8000 is 0.0731
    ================================================================================

    */


    Sunday, June 3, 2007 11:52 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Newtons Divided Difference Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }

    long int fact(int a)
    {
        long int tmp = 1;
        for(int i =a; i > 0; i--)
            tmp *= i;
        return tmp;
    }

    void main()
    {

        double *x, **y, xi, yi;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double*[n];

        for(i=0; i<n; i++)
        {
            y[i] = new double[n-i];
        }

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[0][i];
        }

        printline('=');
        cout<<setw(15)<<"Newton's Divided Difference Interpolation"<<endl;
        printline('=');

        cout<<setw(15)<<"Divided Difference table "<<endl;
        printline('-');

    //    Calculating the values of the Divided Difference table....

        for(i=1; i<n; i++)
        {
            for(j=0; j<n-i; j++)
            {
                y[i][j] = (y[i-1][j+1] - y[i-1][j]) / (x[j+i] - x[j]);
            }
        }

        cout<<"x"<<setw(10)<<"y";
        for(i=0; i<n-1; i++)
            cout<<setw(11)<<"/|\\"<<i+1;
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(10)<<x[i]<<setw(10)<<y[0][i];
            for(j=1; j<n-i; j++)
            {
                    cout<<setw(12)<<y[j][i];
            }
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        yi = y[0][0];
        for(i=1; i<n; i++)
        {
            double tmp = 1;
            for(j=0; j<i; j++)
            {
                tmp *= (xi-x[j]);
            }
            yi += tmp*y[i][0];
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');

    }

    /*..................................Output...................................

    ================================================================================

    Please give the number of inputs value of x and y : 4
    Please give the input values :
    Value of x1 = 2
    Value of y1 = 10
    Value of x2 = 4
    Value of y2 = 26
    Value of x3 = 7
    Value of y3 = 65
    Value of x4 = 9
    Value of y4 = 101
    ================================================================================

    Newton's Divided Difference Interpolation
    ================================================================================

    Divided Difference table
    --------------------------------------------------------------------------------

    x         y        /|\1        /|\2        /|\3
    2.0000    10.0000   8.0000      1.0000      0.0000
    4.0000    26.0000   13.0000     1.0000
    7.0000    65.0000   18.0000
    9.0000    101.0000
    --------------------------------------------------------------------------------

    Enter the value of x : 5
    The value of y for x = 5.0000 is 37.0000
    ================================================================================

    */


    Sunday, June 3, 2007 11:52 AM
  • Code Snippet


    // This program will find the interpolating value y on a given y
    // Using Langrange's  Interpolation Method...


    #include<iostream>
    #include<math.h>
    #include<conio.h>
    #include<iomanip>

    using namespace std;

    void printline(char printCharacter)
    {
        for(int i = 0 ; i < 80 ; i++)
        {
            cout<<printCharacter;
        }
        cout<<endl;
    }


    void main()
    {

        double *x, *y, xi, yi;
        int n, i, j;

        printline('=');
        cout<<"Please give the number of inputs value of x and y : ";
        cin>>n;

        x = new double[n];
        y = new double[n];

        cout<<"Please give the input values : \n";
        for(i=0; i<n; i++)
        {
            cout<<"Value of x"<<i+1<<" = ";
            cin>>x[i];
            cout<<"Value of y"<<i+1<<" = ";
            cin>>y[i];
        }

        printline('=');
        cout<<setw(15)<<"Langrange's Interpolation"<<endl;
        printline('=');

    //    Calculating the values of the Divided Difference table....


        cout<<"x"<<setw(10)<<"y";
        cout<<endl;

        cout.setf(ios::left, ios::adjustfield);
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(4);

        for(i=0; i<n; i++)
        {
            cout<<setw(10)<<x[i]<<setw(10)<<y[i];
            cout<<endl;
        }

    //    Calculating the interpolating value of y..
        printline('-');
        cout<<"Enter the value of x : ";
        cin>>xi;

        yi = 0;

        for(i=0; i<n; i++)
        {
            double tmp = 1;
            for(j=0; j<n; j++)
            {
                if(i!=j)
                    tmp *= (xi - x[j])/(x[i] - x[j]);
            }
            yi += tmp*y[i];
        }

        cout<<"The value of y for x = "<<xi<<" is "<<yi<<endl;
        printline('=');

    }

    /*..................................Output...................................
    ================================================================================

    Please give the number of inputs value of x and y : 4
    Please give the input values :
    Value of x1 = 2
    Value of y1 = 10
    Value of x2 = 4
    Value of y2 = 26
    Value of x3 = 7
    Value of y3 = 65
    Value of x4 = 9
    Value of y4 = 101
    ================================================================================

    Langrange's Interpolation
    ================================================================================

    x         y
    2.0000    10.0000
    4.0000    26.0000
    7.0000    65.0000
    9.0000    101.0000
    --------------------------------------------------------------------------------

    Enter the value of x : 5
    The value of y for x = 5.0000 is 37.0000
    ================================================================================

    */


    Sunday, June 3, 2007 11:53 AM
  • Nice Programs Varun. Keep em coming Smile Especially those general equations, where you can enter as many number of equations. haha thats nice.

    Now time to start some low level C again Smile
    Sunday, June 3, 2007 12:02 PM

  • Code Snippet

    /*
    [41] Write a program to print the Pascals Triangle :
                           1
                         1   1
                       1   2   1
                     1   3   3   1
                   1   4   6   4   1
                 1   5  10  10   5   1

    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp,a=0,b=1,c=0,p=1;
        int arr[15][15]={0};
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n+1;
        if(n>0) {
            printf("%*c",sp*2,' ');
            printf("%4d\n",p);
        }
        sp--;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (p=1,j=0 ; j<=i ; j++)    {
                if(j==0 || j==i)
                    p=1;
                else
                    p=p*(i-j+1)/j;
                printf("%4d",p);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:05 PM

  • Code Snippet

    /*
    [42] Write a program to print the pattern :
                *             *
                * *         * *
                * * *     * * *
                * * * * * * * *
                * * * * * * * *

    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n*2-2;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            if(i!=n) {
                for (j=1 ; j<=i ; j++)
                {
                    printf("* ");
                }
                for(j=1 ; j<=2*(n-i)-2 ; j++) {
                    printf("  ");
                }
                for (j=1 ; j<=i ; j++)
                {
                    printf("* ");
                }
            }
            else {
                for(j=1 ; j<=n*2-2 ; j++) {
                    printf("* ");

                }
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [43] Write a program to print the pattern :
                      *     For n=5
                    *   *
                  *   *   *
                *   *   *   *
              *   *   *   *   *
                *   *   *   *
                  *   *   *
                    *   *
                      *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("*   ");
            }
        }
        for(sp+=2,i=n-1 ; i>=1 ; i--, printf("\n")) {
            printf("%*c",sp++*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("*   ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [44] Write a program to print the pattern :
            Enter a number : 5
            * * * * *
            *       *
            *       *
            *       *
            *       *
            *       *
            * * * * *
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,j,n=10;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n+2 ; i++,printf("\n")) {
            for(j=1 ; j<=n ; j++) {
                if(i==1 || j==1 || i==n+2 || j==n )
                    printf("* ");
                else
                    printf("  ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:06 PM

  • Code Snippet

    /*
    [45] Write a program to print the pattern :
             A
             A B
             A B C
             A B C D
             A B C D E
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",j+'A'-1);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [46] Write a program to print the pattern :
             Z
             Z Y
             Z Y X
             Z Y X W
             Z Y X W V
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,ch='Z';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (ch='Z',j=1 ; j<=i ; j++)
            {
                printf("%2c",ch--);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [47] Write a program to print the pattern :
                         A
                       A   B
                     A   B   C
                   A   B   C   D
                 A   B   C   D   E
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40,ch='A';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (ch='A',j=1 ; j<=i ; j++)
            {
                printf("%4c",ch++);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [48] Write a program to print the pattern :
                           Z
                         Z   Y
                       Z   Y   X
                     Z   Y   X   W
                   Z   Y   X   W   V
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40,ch='Z';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (ch='Z',j=1 ; j<=i ; j++)
            {
                printf("%4c",ch--);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:07 PM

  • Code Snippet

    /*
    [49] Write a program to print the pattern :
              1
              1  4
              1  4  9
              1  4  9 16
              1  4  9 16 25
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",j*j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [50] Write a program to print the pattern :
                         1
                      4  1
                   9  4  1
               16  9  4  1
            25 16  9  4  1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",(i-j+1)*(i-j+1));
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [51] Write a program to print the pattern :
            Enter a number : 5
                             0
                          1  0  1
                       2  1  0  1  2
                    3  2  1  0  1  2  3
                 4  3  2  1  0  1  2  3  4
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=0 ; i<n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=-i ; j<=i ; j++)    {
                printf("%3d",(j<0)?j*-1:j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [52] Write a program to print the pattern :
        Enter a number : 5
                         0
                     -1  0  1
                  -2 -1  0  1  2
               -3 -2 -1  0  1  2  3
            -4 -3 -2 -1  0  1  2  3  4
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=0 ; i<n ; i++, printf("\n")) {
            printf("%*c",sp--*3,' ');
            for (j=-i ; j<=i ; j++)    {
                printf("%3d",j);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:08 PM

  • Code Snippet

    /*
    [53] Write a program to print the pattern :
        A diamond pattern is formed with a given letter of the
        alphabet. Write a program to generate and display such
        by accepting any character and total number of lines (say N).  
        Assume  N is an odd number.
            For example, if the accepted character is 'H'
            and N = 7,  then your program should display the
            following output :

                      H
                    H   H
                  H   H   H
                H   H   H   H
                  H   H   H
                    H   H
                      H
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp,ch;
        clrscr();
        printf("Enter an odd number : ");
        scanf("%d",&n);
        sp=n;
        printf("Enter a character : ");
        scanf(" %c",&ch);
        for(i=1 ; i<=n/2+1 ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("%c   ",ch);
            }
        }
        for(sp+=2,i=n/2 ; i>=1 ; i--, printf("\n")) {
            printf("%*c",sp++*2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("%c   ",ch);
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [54] Write a program to print the pattern :
        A pattern is constructed by stacking up 3 basic triangles
        formed with stars. The figure below shows each basic triangle
        having 3 layers of stars :
                Enter an odd number : 5
                                          *
                                        * * *
                                      * * * * *
                                    * * * * * * *
                                  * * * * * * * * *
                                  *               *
                                * * *           * * *
                              * * * * *       * * * * *
                            * * * * * * *   * * * * * * *
                          * * * * * * * * * * * * * * * * *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp,ch;
        clrscr();
        printf("Enter an odd number : ");
        scanf("%d",&n);
        sp=40;
        for(i=1 ; i<=n*2 ; i+=2, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("* ");
            }
        }
        for(sp+=2,i=1 ; i<=n*2 ; i+=2, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)    {
                printf("* ");
            }
            for(j=1 ; j<=2*n-3-i+1 ;j++) {
                printf("  ");
            }
            for (j=1 ; j<=i ; j++)    {
                if(i==n*2-1 && j==1);
                else printf("* ");
            }
        }
        getch();
    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [55]    You are given two 4-digit positive integers. Write a program to
        calculate and print out the sum of the products of each pair of
        digits occupying the same position in the two numbers. For example,
        if first number is 3445 and second number is 4534, then output will
        be 64 (3*4+4*5+4*3+5*4=64).
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int no1,no2,sum=0;
        clrscr();
        do {
            printf("Enter no1 [Only 4 Digited]  : ");
            scanf("%d",&no1);
            printf("\n");
        }while(no1<1000 || no1>9999);
        do {
            printf("Enter no2 [Only 4 Digited] : ");
            scanf("%d",&no2);
            printf("\n");
        } while(no2<1000 || no2>9999);
        while(no1!=0) {
            sum+=((no1%10)*(no2%10));
            no1/=10;
            no2/=10;
        }
        printf("ANS = %d",sum);
        getch();


    }
    Sunday, June 3, 2007 12:09 PM

  • Code Snippet

    /*
    [56]    To print ASCII value of all characters.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int i=0,flag=0;
        clrscr();
        for(i=0 ; i<=255 ; i++)
        {       if(i>=186 && flag==0)
            {
                printf("\nPress any key to continue...");
                getch();
                printf("\r");
                flag=1;
            }
            printf("%4d = %c  ",i,i);

        }
        getch();
    }
    Sunday, June 3, 2007 12:10 PM

  • Code Snippet

    /*
    [57]    To print the series :  2,  10,  30,  68,  130 and so on
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,n,cnt=1;
        clrscr();
        printf("Enter a number  : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++) {
            printf("%4d",(i*i*i)+i);
        }
        getch();

    }
    Sunday, June 3, 2007 12:10 PM

  • Code Snippet

    /*
    [58]    To print the series :  1,  3,  6,  10,  15,  21, and so on    
    */
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i,n,cnt=1;
        clrscr();
        printf("Enter a number  : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++) {
            printf("%3d",cnt);
            cnt+=i+1;
        }
        getch();

    }
    Sunday, June 3, 2007 12:11 PM

  • Code Snippet

    /*
    [59]    Write a program to print circular matrix.
        For example,
          if N= 1,    N=2        N=3        N=4
     
        1    1  2        1  2  3         1  2  3  4
             4  3        8  9  4        12 13 14  5
                         7  6  5        11 16 15  6
                                        10  9  8  7
    */                                
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int x,y,i,j,n=5,r=0,c=0,cnt=1;
        int arr[10][10]={0};
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        x=n;
        for(i=1 ; i<=n*n ; i++)
        {
            for(j=1 ; j<=x ; j++)
                arr[r][c++]=cnt++;
            for(r++,c--,j=1 ; j<=x-1 ; j++)
                arr[r++][c]=cnt++;
            for(r--,c--,j=1 ; j<=x-1 ; j++)
                arr[r][c--]=cnt++;
            for(c++,r--,j=1 ; j<=x-2 ; j++)
                arr[r--][c]=cnt++;
            r++;c++;x-=2;
        }
        for(i=0 ; i<n ; i++)
        {
            for(j=0 ; j<n ; j++)
            {
                printf("%3d",arr[i][j]);
            }
            printf("\n");
        }
        getch();

    }
    Sunday, June 3, 2007 12:11 PM

  • Code Snippet

    /*
    [60]    Generate the following "pyramid" of digits.
                        1
                      2 3 2
                    3 4 5 4 3
                  4 5 6 7 6 5 4
                5 6 7 8 9 8 7 6 5
              6 7 8 9 0 1 0 9 8 7 6
            7 8 9 0 1 2 3 2 1 0 9 8 7
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>

    void main() {
        int i,j,n,sp=40,cnt=0;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);

        for(i=1 ; i<=n ; i++) {
            printf("%*c",sp-=2,' ');
            for(j=i,cnt=i ; j>=1 ; j--,cnt++) {
                printf("%2d",abs(cnt%10));
            }
            for(j=1,cnt-=2 ; j<i ; j++,cnt--) {
                printf("%2d",abs(cnt%10));
            }

            printf("\n");
        }
        getch();
    }
    Sunday, June 3, 2007 12:12 PM