Tuesday, February 13, 2024

16. WAP to reverse the order of the elements in the stack using additional stack.

 16. WAP to reverse the order of the elements in the stack using additional stack. 

PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class stack_rev{

                int size,*arr,*arr1,top,top1;

                public:

                                stack_rev(){

                                                size=0;

                                                arr=NULL;

                                                arr1=NULL;

                                                top=-1;

                                                top1=-1;

                                }

                                void getdata(){

                                                cout<<"ENTER THE SIZE OF THE STACK-> "<<endl;

                                                cin>>this->size;

                                                arr=new int[this->size];

                                                arr1=new int[this->size];

                                }

                                void push(){

                                                if(top==(size-1))

                                                                cout<<"THE STACK IS OVER-FLOW...."<<endl;

                                                else{

                                                                int x=0;

                                                                cout<<"ENTER THE ELEMENT ->"<<endl;

                                                                cin>>x;

                                                                top++;

                                                                arr[top]=x;

                                                }

                                }

                                void revstack(){

                                                if(top1==(size-1))

                                                                cout<<"THE STACK IS OVER-FLOW...."<<endl;

                                                else{

                                                                while(top>=0){

                                                                                top1++;

                                                                                arr1[top1]=arr[top];

                                                                                top--;

                                                                }

                                                }

                                               

                                }

                                void display1(){

                                                cout<<"THE ORIGINAL STACK IS ->"<<endl;

                                                int x=top+1;

                                                while(--x>=0)

                                                                cout<<arr[x]<<endl;

                                }

                                void display2(){

                                                cout<<"THE REVERSE STACK IS ->"<<endl;

                                                int x=top1+1;

                                                while(--x>=0)

                                                                cout<<arr1[x]<<endl;

                                }                             

};

int main(){

                stack_rev ob;

                int opt=0;

                do{

                                cout<<"\nPRESS 0 TO EXIT ...."<<endl;

                                cout<<"PRESS 1 TO SET STACK SIZE...."<<endl;

                                cout<<"PRESS 2 TO PUSH DATA...."<<endl;

                                cout<<"PRESS 3 TO DISPLAY THE STACK...."<<endl;

                                cout<<"PRESS 4 TO REV THE STACK...."<<endl;

                                cout<<"PRESS 5 TO DISPLAY THE REV STACK....."<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                ob.getdata();

                                                                break;

                                                case 2:

                                                                ob.push();

                                                                break;

                                                case 3:

                                                                ob.display1();

                                                                break;

                                                case 4:

                                                                ob.revstack();

                                                                cout<<"THE REVERSE SUCCESSFULL.........."<<endl;

                                                                break;

                                                case 5:

                                                                ob.display2();

                                                                break;

                                                default:

                                                                cout<<"ERROR OPTION CHOICE ........."<<endl;

                                                                break;

                                }

                }while(opt!=0);

                return 0;

}


15. WAP to convert the Sparse Matrix into non-zero form and vice-versa.

 15. WAP to convert the Sparse Matrix into non-zero form and vice-versa. 


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class sparse_matrix{

            int A[50][50],B[50][3],row,col,k;

            public:

                        void datains(int row,int col){

                                    this->row=row;

                                    this->col=col;

                                    cout<<"ENTER THE DATA ->"<<endl;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++)

                                                            cin>>A[i][j];

                                    }

                                    display();

                                    k=check();

                                    if(k){

                                                cout<<"THIS IS A PURSE MATRIX ...."<<endl;

                                                cout<<"THE TOTAL ELEMENTS ARE -> "<<(row*col)<<" THE TOTAL NON-ZERO ELEMENTS ARE ->"<<(row*col)-k<<endl;

                                    }

                                    else

                                                cout<<"THIS IS NOT A PURSE MATRIX ...."<<endl;                                     

                        }

                        int check(){

                                    int count=0;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(A[i][j]==0)

                                                                        count++;

                                                }

                                    }

                                    if(count>=(row*col)/2)

                                                return count;

                                    else

                                                return 0;

                        }

                        void purse(){

                                    int p=0;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(A[i][j]!=0){

                                                                        B[p][0]=i;

                                                                        B[p][1]=j;

                                                                        B[p][2]=A[i][j];

                                                                        p++;

                                                            }

                                                }

                                    }

                        }

                        void display(){

                                    cout<<"THE MATRIX IS->"<<endl;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            cout<<A[i][j]<<"  ";

                                                }

                                                cout<<endl;

                                    }                      

                        }

                        void display1(){

                                    cout<<"THE NEW MATRIX IS->"<<endl;

                                    for(int i=0;i<((row*col)-k);i++){

                                                for(int j=0;j<col;j++){

                                                            cout<<B[i][j]<<"  ";

                                                }

                                                cout<<endl;

                                    }                      

                        }

};

int main(){

            int row=0,col=0;

            sparse_matrix ob;

            cout<<"ENTER THE ROW VALUE -> "<<endl;

            cin>>row;

            cout<<"ENTER THE COL VALUE -> "<<endl;

            cin>>col;

            ob.datains(row,col);

            ob.purse();

            ob.display1();

            return 0;

}


13. WAP to calculate GCD of 2 number (i) with recursion (ii) without recursion

13. WAP to calculate GCD of 2 number (i) with recursion (ii) without recursion


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class gcd{

            public:

                        int a,b,k;

                        gcd(){

                                    a=0;

                                    b=0;

                                    k=0;

                        }

                        void calculate(){

                                    if(a==0||b==0)

                                                cout<<"THERE IS A ERROR ......"<<endl;

                                    else if(a>b){

                                                int key=a;

                                                a=b;

                                                b=key;

                                    }

                                    while(a!=0){

                                                int c=b/a;

                                                int d=b%a;

                                                if(d==0)

                                                            k=a;                                        

                                                b=a;

                                                a=d;

                                    }

                        }

                        int calculate_rec(int a,int b){

                                    if(a==0 && b==0)

                                                cout<<"THERE IS A ERROR ......"<<endl;

                                    else if(a>b){

                                                int key=a;

                                                a=b;

                                                b=key;

                                    }

                                    int c=b/a;

                                    int d=b%a;

                                    if(d==0)

                                                return a;                                            

                                    b=a;

                                    a=d;

                                    calculate_rec(a,b);                          

                        }

                        void display(){

                                    if(a==0 && b==0)

                                                cout<<"...........ERROR.........."<<endl;

                                    cout<<"THE GCD IS -> "<<k<<endl;

                        }

};

int main(){

            gcd ob;

            int opt=0;

            do{

                        cout<<"\nPRESS 0 TO EXIT....."<<endl;

                        cout<<"PRESS 1 TO CALCULATE GCD.........."<<endl;

                        cout<<"PRESS 2 TO CALCULATE GCD USING RECURTION......."<<endl;

                        cout<<"PRESS 3 TO DISPLAY..."<<endl;

                        cin>>opt;

                        switch(opt){

                                    case 0:

                                                break;

                                    case 1:

                                                cout<<"ENTER THE 1st NUMBER ->"<<endl;

                                                cin>>ob.a;

                                                cout<<"ENTER THE 2nd NUMBER ->"<<endl;

                                                cin>>ob.b;

                                                ob.calculate();

                                                break;

                                    case 2:

                                                cout<<"ENTER THE 1st NUMBER ->"<<endl;

                                                cin>>ob.a;

                                                cout<<"ENTER THE 2nd NUMBER ->"<<endl;

                                                cin>>ob.b;

                                                cout<<"THE GCD IS -> "<<ob.calculate_rec(ob.a,ob.b)<<endl;

                                                break;                                    

                                    case 3:

                                                ob.display();

                                                break;

                                    default:

                                                cout<<"........ERROR..........."<<endl;

                        }

                       

            }while(opt!=0);

            return 0;

}


12. (ii) WAP to display Fibonacci series (i)using recursion, (ii) using iteration

 12. (ii) WAP to display Fibonacci series (i)using recursion, (ii) using iteration


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class fibo{

                public:

                                void fibo1(int num){

                                                int a=1,b=1,c=0;

                                                for(int i=1;i<=num;i++){

                                                                if(i==1)

                                                                                cout<<a<<" ";

                                                                else if(i==2)

                                                                                cout<<b<<" ";

                                                                else{

                                                                                c=a+b;

                                                                                a=b;

                                                                                b=c;

                                                                                cout<<c<<" ";                                   

                                                                }

                                                }

                                }

                                int fib(int num){

                                                if(num==1||num==2)

                                                                return 1;

                                                return(fib(num-1)+fib(num-2));

                                }                             

                                void fibo2(int num){

                                                for(int i=1;i<=num;i++){

                                                                cout<<fib(i)<<" ";

                                                }

 

                                }

};

int main(){

                fibo ob;

                int num=0,opt=0;

                do{

                                cout<<"\nPRESS 0 TO EXIT ..."<<endl;

                                cout<<"PRESS 1 TO CALCULATE FIBONACCI SERIES ...."<<endl;

                                cout<<"PRESS 2 TO CALCULATE FIBONACCI SERIES USING RECERSION..........."<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                cout<<"ENTER THE POSITION OF SEREIES....."<<endl;

                                                                cin>>num;

                                                                ob.fibo1(num);

                                                                break;

                                                case 2:

                                                                cout<<"ENTER THE POSITION OF SEREIES....."<<endl;

                                                                cin>>num;

                                                                ob.fibo2(num);

                                                                break;                                                  

                                                default:

                                                                cout<<"...........ERROR............"<<endl;

                                }

                }while(opt!=0);

                return 0;

}


Friday, January 19, 2024

Find the roots of equation(x3-4x-9)Newton Raphson method.

 #include<stdio.h>

#include<math.h>

double f(double x)

{

return x*x*x-4*x-9;

}

double fd(double x)

{

return 3*x*x-4;

}

void main()

{

int i=1;

double a,b,tol;

printf("Enter value of y0:");

scanf("%lf",&a);

printf("Enter tolerance:");

scanf("%lf",&tol);

while(1)

{

       b=a;

       a=a-f(a)/fd(a);

       printf(" y%d ---> %f\n",i++,a);

       if(fabs(a-b)<tol)

           break;

}

printf("\n Solution=%f",a);

}

Output:

Enter value of y0:2

Enter tolerance:0.00001

y1 ---> 3.125000

y2 ---> 2.768530

y3 ---> 2.708196

y4 ---> 2.706529

y5 ---> 2.706528


Solution=2.706528

Enter value of y0:3

Enter tolerance:0.00001

y1 ---> 2.739130

y2 ---> 2.706998

y3 ---> 2.706528

y4 ---> 2.706528


Solution=2.706528

Enter value of y0:5

Enter tolerance:0.00001

y1 ---> 3.647887

y2 ---> 2.953279

y3 ---> 2.730187

y4 ---> 2.706777

y5 ---> 2.706528

y6 ---> 2.706528


Solution=2.706528

Simpson 3/8 Rule C Program

 #include<stdio.h>

#include<math.h>

float f(float x)

{

    return(x);

}


void main()

{

    int i,n;

    float x0,xn,h,y[20],so,se,ans,x[20];

    printf("\n Enter values of x0,xn,h: ");

    scanf("%f%f%f",&x0,&xn,&h);

    n=(xn-x0)/h;

    if(n%2==1)

    {

        n=n+1;

    }

    h=(xn-x0)/n;

    printf("\n Refined value of n and h are:%d %f\n",n,h);

    printf("\n Y values: \n");

    for(i=0; i<=n; i++)

    {

        x[i]=x0+i*h;

        y[i]=f(x[i]);

        printf("\n %f\n",y[i]);

    }

    so=0;

    se=0;

    for(i=1; i<n; i++)

    {

        if(i%3==0)

        {

            so=so+y[i];

        }

        else

        {

            se=se+y[i];

        }

    }

    printf("  i        x           y\n");

    for (i=0;i<n;i++)

    {

        printf("  %d    %lf    %lf\n",i,x[i],y[i]);

    }

    ans=h/3*(y[0]+y[n]+4*so+2*se);

    printf("\n Final integration is %f",ans);


}

trapezoidal rule numerical in c

 #include<stdio.h>

float f(float x)

{

    return(x*x);

}

void main()

{

    int i,n;

    float x0,xn,h,y[20],s=0,ans,x[20];

    printf("\n Enter values of x0,xn,h: ");

    scanf("%f%f%f",&x0,&xn,&h);

    n=(xn-x0)/h;

    if(n%2==1)

    {

        n=n+1;

    }

    h=(xn-x0)/n;

    printf("\n Refined value of n and h are:%d %f\n",n,h);

    printf("\n Y values: \n");

    for(i=0; i<=n; i++)

    {

        x[i]=x0+i*h;

        y[i]=f(x[i]);

        printf("\n %f\n",y[i]);

    }


    for(i=1; i<n; i++)

    {

            s=s+y[i];

    }


    printf("  i        x           y\n");


    for (i=0;i<n;i++)

    {

        printf("  %d    %lf    %lf\n",i,x[i],y[i]);

    }

    ans=h/2*(y[0]+y[n]+2*s);

    printf("\n Final integration is %f",ans);

}