Sunday, 8 October 2023

SPARSE MATRIX

#include<iostream>

using namespace std;


int main()

{

int a[10][10],row,col,count=0,i,j,k=1;

int s[10][3];

cout<<"total number of row";

cout<<"total number of column";

cin>>row;

cin>>col;

cout<<"enter value";

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

{

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

{

cin>>a[i][j];

}

}

cout<<"matrix element\n";

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

{

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

{

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

}

cout<<"\n";

}

//count total number of nonzero elements

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

{

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

{

if(a[i][j]!=0)

{

count++;

}

}

}

cout<<count;

s[0][0]=row;

s[0][1]=col;

s[0][2]=count;

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

{

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

{

if(a[i][j]!=0)

{

s[k][0]=i;

s[k][1]=j;

s[k][2]=a[i][j];

k++;

}

}

}

cout<<"\n output array \n";

for(i=0;i<count+1;i++)

{

for(j=0;j<3;j++)

{

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

}

cout<<"\n";

}

}




ARRAY INSERTION AND DELETION

 #include<iostream>


using namespace std;




class array


{


int a[30], n;


public:


void input()


{


cout<<"Enter the length: ";


cin>>n;


cout<<"Enter a value for the array "<<endl;


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


cin>>a[i];


}


void insert_begin()


{


int value;


cout<<"Enter a value for insertion at the front "<<endl;


cin>>value;


n=n+1;


for(int i=n-1; i>=1; i--)


{


a[i]=a[i-1];


}


a[0]=value;


}


void insert_end()


{


int value;


cout<<"Enter a value for insertion at the end "<<endl;


cin>>value;


n=n+1;


a[n-1]=value;


}


void insert_position()


{


int pos;


cout<<"Enter the position "<<endl;


cin>>pos;


int value;


cout<<"Enter the value "<<endl;


cin>>value;


for(int i=n-1; i>=pos; i--)


{


a[i]=a[i-1];


}


a[pos-1]=value;


}


void display()


{


cout<<"The value of array "<<endl;


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


cout<<a[i]<<" ";


cout<<endl;


}


void delete_begin()


{


int value;


value=a[0];


cout<<"The deleted value is "<<value<<endl;


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


{


a[i-1]=a[i];


}


n=n-1;


}


void delete_end()


{


int value;


value=a[n-1];


cout<<"The deleted value is "<<value<<endl;


n=n-1;


}


void delete_position()


{


int value, pos;


cout<<"Enter position "<<endl;


cin>>pos;


value=a[pos-1];


cout<<"The deleted value is "<<value<<endl;


for(int i=pos; i<=n-1; i++)


{


a[i-1]=a[i];


}


n=n-1;


}



};




int main()


{


array ob;


ob.input();


while(1)


{


cout<<"Enter 1 for displaying array "<<endl;


cout<<"Enter 2 for inserting element at the front "<<endl;


cout<<"Enter 3 for inserting element at the end "<<endl;


cout<<"Enter 4 for inserting element at the given position "<<endl;


cout<<"Enter 5 for deleting element from the front "<<endl;


cout<<"Enter 6 for deleting element from the end "<<endl;


cout<<"Enter 7 for deleting element from a given position "<<endl;


cout<<"Enter 8 for exit "<<endl;



int choice;


cin>>choice;


switch(choice)


{


case 1:


ob.display();


break;



case 2:


cout<<"\nValue before insertion\n";


ob.display();


ob.insert_begin();


cout<<"\nValue after insertion\n";


ob.display();


break;



case 3:


cout<<"\nValue before insertion\n";


ob.display();


ob.insert_end();


cout<<"\nValue after insertion\n";


ob.display();


break;



case 4:


cout<<"\nValue before insertion\n";


ob.display();


ob.insert_position();


cout<<"\nValue after insertion\n";


ob.display();


break;



case 5:


cout<<"\nValue before deletion\n";


ob.display();


ob.delete_begin();


cout<<"\nValue after deletion\n";


ob.display();


break;



case 6:


cout<<"\nValue before deletion\n";


ob.display();


ob.delete_end();


cout<<"\nValue after deletion\n";


ob.display();


break;



case 7:


cout<<"\nValue before deletion\n";


ob.display();


ob.delete_position();


cout<<"\nValue after deletion\n";


ob.display();


break;



case 8:


cout<<"Exit from program";


break;



default:


cout<<"Enter a valid choice\n";


}


if(choice==8)


break;


}


return 0;


}

 





LINKED LIST PUSH-POP OPERATION

#include<stdio.h>

#include<stdlib.h>


struct node {

    int data;

    struct node* link;

};


struct node* top = NULL;


void push() {

    struct node* temp = (struct node*) malloc(sizeof(struct node));

    printf("Enter a value: ");

    scanf("%d", &temp->data);

    temp->link = top;

    top = temp;

}


void pop() {

    if (top == NULL) {

        printf("Stack underflow\n");

    } else {

        struct node* temp = top;

        top = top->link;

        printf("Popped value = %d\n", temp->data);

        free(temp);

    }

}


void display() {

    if (top == NULL) {

        printf("Empty stack\n");

    } else {

        struct node* temp = top;

        while (temp != NULL) {

            printf("%d ", temp->data);

            temp = temp->link;

        }

        printf("\n");

    }

}


int main() {

    int choice;

    while (1) {

        printf("Enter 1 for push\n");

        printf("Enter 2 for pop\n");

        printf("Enter 3 for display\n");

        printf("Enter 4 for exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                push();

                break;

            case 2:

                pop();

                break;

            case 3:

                display();

                break;

            case 4:

                printf("Exiting the program\n");

                exit(0);

            default:

                printf("Wrong choice\n");

        }

    }

    return 0;

}

Saturday, 23 September 2023

INFIX TO PREFIX CONVERTION

ALGORITHM:

STEP 1:

Add " ( " at the beginning of the infix expression and then reverse.

STEP 2:

Push " ) " on to the stack.

STEP 3:

Scan every character and repeat step 4 to step 7 until " ( " is encountered.

STEP 4:

If input is operand add in the output.

STEP 5:

If input is " ) " push into the stack.

STEP 6:

If input is "(" pop all the operators from the stack and add in the output one by one until ")" is popped.

STEP 7:

If input is operator pop all the stack which is higher precedence than input operator .Push input operator into the stack.

STEP 8:

Reverse the output


EXAMPLE:




Thursday, 21 September 2023

INFIX TO POSTFIX CONVERSION

Step 1: Add " ) " to the end of the infix expression 

Step 2: Push " ( " on to the stack 

Step 3: Repeat until each character in the infix notation is scanned 

            - IF a " ( " is encountered, push it on the stack IF an operand (whether a digit or a character) is                      encountered, add it postfix expression. 

            - IF a " ) " is encountered, then 

                       a. Repeatedly pop from stack and add it to the postfix expression until a " ( " is                                             encountered. 

                       b. Discard the " ( " . That is, remove the " ( " from stack and do not add it to the postfix                              expression. 

           - IF an operator is encountered, then 

                      a. Repeatedly pop from stack and add each operator (popped from the stack) to the postfix expression which has the same precedence or a higher precedence than  0 .

                      b. Push the operator to the stack [END OF IF] 

Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is empty 

Step 5: EXIT

Saturday, 16 September 2023

PUSH POP OPERATION USING C++

 ARRAY REPRESENTATION OF STACK:- 

(LIFO- LAST IN FIRST OUT)

EXAMPLE:

Initially:

empty stack

top = -1

step 1: push 3

           top ++

step 2: push 5

           top++

step 3,4: push 7,9

              top + +

step 5: push 2

          stack overflow

step 6: pop

            top--

step 7,8,9: pop

                 top--

step 10: pop

             stack underflow

        

 PUSH OPERATION:

step 1: start

step 2: if (top=length-1)

            then

                print("stack overflow")

                return 

           endif

step 3: top=top+1

step 4: read n

step 5: arr[top]=n

step 6: stop

 POP OPERATION:

step 1: start

step 2: if top =-1

           then

                print("stack underflow")

                return

          endif

step 3: n=arr[top]

step 4: print n 

step 5: top=top-1

step 6: stop

  PROGRAM:

#include <iostream>
using namespace std;

#define max 30
class stack
{
    int a[max], top;

    public:
    stack()
    {
        top = -1;
    }

    void push(int n)
    {
        if(top == max-1)
        {
            cout<<"stack overflow"<<endl;
            return;
        }
        top++;
        a[top]=n;
    }

    void pop()
    {
        if(top==top-1)
        {
            cout<<"stack underflow"<<endl;
            return;
        }
        int n=a[top];
        cout<<"popped element"<<a[top]<<endl;
        top--;
    }
    void display()
    {
        if(top == -1)
        {
            cout<<"empty stack"<<endl;
        }
        else
        {
            for(int i=top;i>=0;i--)
            {
                cout<<a[i]<<endl;
            }
        }
    }

    void peep()
    {
        if(top == -1)
        {
            cout<<"empty stack"<<endl;
        }
        else
        {
            cout<<a[top]<<endl;
        }
    }
};

int main()
{
    stack ob;
    while(1)
    {
        cout<<"enter 1 for push"<<endl;
        cout<<"enter 2 for pop"<<endl;
        cout<<"enter 3 for display"<<endl;
        cout<<"enter 4 for peep"<<endl;
        cout<<"enter 5 for exit"<<endl;

        int choice;
        cin>>choice;

        switch (choice)
        {
        case 1:
            cout<<"enter a value for push operation"<<endl;
            break;
        case 2:
            ob.pop();
            break;
        case 3:
            ob.display();
            break;
        case 4:
            ob.peep();
            break;
        case 5:
            cout<<"end of program"<<endl;
            break;
        default:
            cout<<"invalid option"<<endl;
        }
        if(choice==5)
        break;
    }
}

Run The CODE:

https://onlinegdb.com/mgcqIqnYd

Friday, 25 August 2023

Simple Applet programs

 Applet Programs in java:

STEP1: Arik.java

import java.applet.Applet;

import java.Graphics.g;

import java.Applet.*;

import java.awt.*;


public class Arik extends Applet

{

           public void paint(Graphics g)

          {

                    g.drawSring("Hello World",20,20);

          }

}


STEP2: Arik.html

<applet code="Arik" width=200 height=60>

</applet>


STEP3:

javac Arik.java

appletviewer Arik.html



There is no UNIVERSAL PERFECT RULE !

  Yes. Looking across the themes we've discussed, the main topics that seemed to frustrate you or pull you into repeated optimization w...