Scilab Program - simpsons 1/3rd rule - IProgramX

Program

function []=simpson13(x, f)
[x0,xn]=size(x)
[y0,yn]=size(f)
if((x0<>1)|(y0<>1)) then
    error('x of f,or both , not column vector(s)')
    abort;
end;
if((xn<>yn)) then
    error('x and f are not of the same length');
    abort;
end;
if(modulo(xn-1,2)<>0) then
    disp(xn-1,"list size=");
    error('list size must be an even number');
    abort;
end;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
    if(modulo(j,2)==0) then
        I=I+4*f(1,j);
    else
        I=I+2*f(1,j);
    end;
end;
I=(h/3.0)*I
printf('Integration by simpson 1/3 rd rule =%f',I);
endfunction

Output:

-->x=(0:0.1:1.6);

-->simpson13(x,sin(x))
Integration by simpson 1/3 rd rule =1.029200 

Post a Comment

8 Comments

  1. // Practical 1: Write a program to perform insertion, deletion, searching and traversal in 1D array.


    #include iostream.h
    #include conio.h

    void traverseArray(int arr[], int size)
    {
    int i;
    cout << "Array elements: ";
    for (i = 0; i < size; i++)
    cout << arr[i] << " ";
    cout << endl;
    }

    void insertElement(int arr[], int &size, int capacity, int element, int position)
    {
    int i;
    if (size == capacity || position < 0 || position > size)
    {
    cout << "Insertion failed!" << endl;
    return;
    }

    for (i = size; i > position; i--)
    arr[i] = arr[i - 1];

    arr[position] = element;
    size++;
    cout << "Element inserted successfully!" << endl;
    }

    void deleteElement(int arr[], int &size, int position)
    {
    int i;
    if (position < 0 || position >= size)
    {
    cout << "Invalid position!" << endl;
    return;
    }

    for (i = position; i < size - 1; i++)
    arr[i] = arr[i + 1];

    size--;
    cout << "Element deleted successfully!" << endl;
    }

    void searchElement(int arr[], int size, int element)
    {
    int i;
    for (i = 0; i < size; i++)
    {
    if (arr[i] == element)
    {
    cout << "Element " << element << " found at position " << i << "." << endl;
    return;
    }
    }
    cout << "Element not found." << endl;
    }

    void main()
    {
    const int capacity = 100;
    int arr[capacity], size = 0;
    int choice, element, position;

    clrscr();

    do
    {
    cout << "\nMenu:\n1.Traverse 2.Insert 3.Delete 4.Search 5.Exit";
    cout << "\nEnter choice: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    traverseArray(arr, size);
    break;

    case 2:
    cout << "Element to insert: ";
    cin >> element;
    cout << "Position (0-based): ";
    cin >> position;
    insertElement(arr, size, capacity, element, position);
    break;

    case 3:
    cout << "Position to delete: ";
    cin >> position;
    deleteElement(arr, size, position);
    break;

    case 4:
    cout << "Element to search: ";
    cin >> element;
    searchElement(arr, size, element);
    break;

    case 5:
    cout << "Exiting...";
    break;

    default:
    cout << "Invalid choice!";
    }

    } while (choice != 5);

    getch();
    }

    ReplyDelete
  2. // Practical 2: Implement stack using arrays with push, pop, peek and display operations.


    #include iostream.h
    #include conio.h

    #define MAX 100

    class Stack
    {
    int arr[MAX];
    int top;

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

    int isEmpty()
    {
    if (top == -1)
    return 1;
    else
    return 0;
    }

    int isFull()
    {
    if (top == MAX - 1)
    return 1;
    else
    return 0;
    }

    void push(int x)
    {
    if (isFull())
    {
    cout << "Stack Overflow! Cannot push " << x << endl;
    return;
    }
    arr[++top] = x;
    cout << x << " pushed to stack." << endl;
    }

    void pop()
    {
    if (isEmpty())
    {
    cout << "Stack Underflow! Nothing to pop." << endl;
    return;
    }
    cout << arr[top--] << " popped from stack." << endl;
    }

    void peek()
    {
    if (isEmpty())
    {
    cout << "Stack is empty." << endl;
    return;
    }
    cout << "Top element is: " << arr[top] << endl;
    }

    void display()
    {
    int i;
    if (isEmpty())
    {
    cout << "Stack is empty." << endl;
    return;
    }

    cout << "Stack elements from top to bottom: ";
    for (i = top; i >= 0; i--)
    cout << arr[i] << " ";
    cout << endl;
    }
    };

    void main()
    {
    Stack s;
    int choice, value;

    clrscr();

    do
    {
    cout << "\nStack Operations Menu\n";
    cout << "1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n";
    cout << "Enter choice: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    cout << "Enter value to push: ";
    cin >> value;
    s.push(value);
    break;

    case 2:
    s.pop();
    break;

    case 3:
    s.peek();
    break;

    case 4:
    s.display();
    break;

    case 5:
    cout << "Exiting program...";
    break;

    default:
    cout << "Invalid choice!";
    }

    } while (choice != 5);

    getch();
    }

    ReplyDelete
  3. // Practical 3: Write a program to convert an infix expression into postfix and evaluate it.


    #include iostream.h
    #include conio.h
    #include ctype.h
    #include string.h

    #define MAX 100

    char stackOp[MAX];
    int topOp = -1;

    int stackVal[MAX];
    int topVal = -1;

    void pushOp(char x)
    {
    stackOp[++topOp] = x;
    }

    char popOp()
    {
    return stackOp[topOp--];
    }

    char peekOp()
    {
    return stackOp[topOp];
    }

    int isEmptyOp()
    {
    if (topOp == -1)
    return 1;
    else
    return 0;
    }

    void pushVal(int x)
    {
    stackVal[++topVal] = x;
    }

    int popVal()
    {
    return stackVal[topVal--];
    }

    int precedence(char op)
    {
    if (op == '+' || op == '-')
    return 1;
    if (op == '*' || op == '/')
    return 2;
    if (op == '^')
    return 3;
    return 0;
    }

    void infixToPostfix(char infix[], char postfix[])
    {
    int i, k = 0;
    char ch;

    for (i = 0; infix[i] != '\0'; i++)
    {
    ch = infix[i];

    if (isdigit(ch))
    {
    postfix[k++] = ch;
    }
    else if (ch == '(')
    {
    pushOp(ch);
    }
    else if (ch == ')')
    {
    while (!isEmptyOp() && peekOp() != '(')
    {
    postfix[k++] = popOp();
    }
    popOp();
    }
    else
    {
    while (!isEmptyOp() && precedence(peekOp()) >= precedence(ch))
    {
    postfix[k++] = popOp();
    }
    pushOp(ch);
    }
    }

    while (!isEmptyOp())
    {
    postfix[k++] = popOp();
    }

    postfix[k] = '\0';
    }

    int evaluatePostfix(char postfix[])
    {
    int i, a, b, res;
    char ch;

    for (i = 0; postfix[i] != '\0'; i++)
    {
    ch = postfix[i];

    if (isdigit(ch))
    {
    pushVal(ch - '0');
    }
    else
    {
    b = popVal();
    a = popVal();

    switch (ch)
    {
    case '+': pushVal(a + b); break;
    case '-': pushVal(a - b); break;
    case '*': pushVal(a * b); break;
    case '/': pushVal(a / b); break;
    case '^':
    res = 1;
    while (b > 0)
    {
    res = res * a;
    b--;
    }
    pushVal(res);
    break;
    }
    }
    }
    return popVal();
    }

    void main()
    {
    char infix[50], postfix[50];
    int result;

    clrscr();

    cout << "Enter infix expression (single digit operands): ";
    cin >> infix;

    infixToPostfix(infix, postfix);

    cout << "Postfix: " << postfix << endl;

    result = evaluatePostfix(postfix);

    cout << "Result: " << result;

    getch();
    }

    ReplyDelete
  4. // Practical 4: Implement queue using arrays with enqueue, dequeue and display operations.


    #include iostream.h
    #include conio.h

    #define MAX 100

    class Queue
    {
    int arr[MAX];
    int front, rear, capacity;

    public:
    Queue(int size)
    {
    capacity = size;
    front = -1;
    rear = -1;
    }

    int isFull()
    {
    if (rear == capacity - 1)
    return 1;
    else
    return 0;
    }

    int isEmpty()
    {
    if (front == -1 || front > rear)
    return 1;
    else
    return 0;
    }

    void enqueue(int val)
    {
    if (isFull())
    {
    cout << "Queue Overflow. Cannot insert " << val << endl;
    return;
    }

    if (isEmpty())
    front = 0;

    arr[++rear] = val;
    cout << val << " enqueued to queue." << endl;
    }

    void dequeue()
    {
    if (isEmpty())
    {
    cout << "Queue Underflow. No element to dequeue." << endl;
    return;
    }

    cout << arr[front++] << " dequeued from queue." << endl;

    if (front > rear)
    {
    front = rear = -1;
    }
    }

    void display()
    {
    int i;
    if (isEmpty())
    {
    cout << "Queue is empty." << endl;
    return;
    }

    cout << "Queue elements: ";
    for (i = front; i <= rear; i++)
    cout << arr[i] << " ";
    cout << endl;
    }
    };

    void main()
    {
    int size, choice, val;
    clrscr();

    cout << "Enter queue capacity: ";
    cin >> size;

    Queue q1(size);

    do
    {
    cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit";
    cout << "\nEnter choice: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    cout << "Enter value to enqueue: ";
    cin >> val;
    q1.enqueue(val);
    break;

    case 2:
    q1.dequeue();
    break;

    case 3:
    q1.display();
    break;

    case 4:
    cout << "Exiting...";
    break;

    default:
    cout << "Invalid choice!";
    }

    } while (choice != 4);

    getch();
    }

    ReplyDelete
  5. // Practical 5: Implement circular queue using arrays.



    #include iostream.h
    #include conio.h

    #define MAX 20

    class CircularQueue
    {
    int arr[MAX];
    int front, rear, capacity;

    public:
    CircularQueue(int size)
    {
    capacity = size;
    front = -1;
    rear = -1;
    }

    int isFull()
    {
    if (front == (rear + 1) % capacity)
    return 1;
    else
    return 0;
    }

    int isEmpty()
    {
    if (front == -1)
    return 1;
    else
    return 0;
    }

    void enqueue(int val)
    {
    if (isFull())
    {
    cout << "Overflow: Cannot insert " << val << endl;
    return;
    }

    if (isEmpty())
    front = rear = 0;
    else
    rear = (rear + 1) % capacity;

    arr[rear] = val;
    cout << val << " enqueued." << endl;
    }

    void dequeue()
    {
    if (isEmpty())
    {
    cout << "Underflow: No element to dequeue." << endl;
    return;
    }

    cout << arr[front] << " dequeued." << endl;

    if (front == rear)
    front = rear = -1;
    else
    front = (front + 1) % capacity;
    }

    void display()
    {
    int i;
    if (isEmpty())
    {
    cout << "Queue is empty." << endl;
    return;
    }

    cout << "Queue elements: ";
    i = front;
    while (1)
    {
    cout << arr[i] << " ";
    if (i == rear)
    break;
    i = (i + 1) % capacity;
    }
    cout << endl;
    }
    };

    void main()
    {
    int size, choice, val;

    clrscr();

    cout << "Enter queue capacity: ";
    cin >> size;

    CircularQueue cq(size);

    do
    {
    cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit";
    cout << "\nChoice: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    cout << "Value: ";
    cin >> val;
    cq.enqueue(val);
    break;

    case 2:
    cq.dequeue();
    break;

    case 3:
    cq.display();
    break;

    case 4:
    cout << "Exit...";
    break;

    default:
    cout << "Invalid choice!";
    }

    } while (choice != 4);

    getch();
    }

    ReplyDelete
  6. // Practical 9: Write a program to create a Binary Search Tree (BST) to perform inorder, preorder and postorder traversal.



    #include iostream.h
    #include conio.h
    #include stdlib.h

    struct Node
    {
    int data;
    struct Node *left;
    struct Node *right;
    };

    struct Node* createNode(int val)
    {
    struct Node *newNode;
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = val;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
    }

    struct Node* insert(struct Node* root, int val)
    {
    if (root == NULL)
    return createNode(val);

    if (val < root->data)
    root->left = insert(root->left, val);
    else if (val > root->data)
    root->right = insert(root->right, val);

    return root;
    }

    void inorder(struct Node* root)
    {
    if (root != NULL)
    {
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
    }
    }

    void preorder(struct Node* root)
    {
    if (root != NULL)
    {
    cout << root->data << " ";
    preorder(root->left);
    preorder(root->right);
    }
    }

    void postorder(struct Node* root)
    {
    if (root != NULL)
    {
    postorder(root->left);
    postorder(root->right);
    cout << root->data << " ";
    }
    }

    void main()
    {
    struct Node *root = NULL;

    clrscr();

    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 70);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 60);
    root = insert(root, 80);

    cout << "Inorder traversal of BST: ";
    inorder(root);
    cout << endl;

    cout << "Preorder traversal of BST: ";
    preorder(root);
    cout << endl;

    cout << "Postorder traversal of BST: ";
    postorder(root);
    cout << endl;

    getch();
    }

    ReplyDelete
  7. // Practical 10: Implement BFS for graph traversal.



    #include iostream.h
    #include conio.h

    #define MAX 4

    int graph[MAX][MAX] =
    {
    {0, 1, 1, 0},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {0, 1, 1, 0}
    };

    int visited[MAX];
    int queue[MAX];
    int front = 0, rear = -1;

    void enqueue(int v)
    {
    queue[++rear] = v;
    }

    int dequeue()
    {
    return queue[front++];
    }

    int isEmpty()
    {
    if (front > rear)
    return 1;
    else
    return 0;
    }

    void bfs(int start)
    {
    int i, v;

    enqueue(start);
    visited[start] = 1;

    cout << "BFS Traversal: ";

    while (!isEmpty())
    {
    v = dequeue();
    cout << v << " ";

    for (i = 0; i < MAX; i++)
    {
    if (graph[v][i] == 1 && visited[i] == 0)
    {
    visited[i] = 1;
    enqueue(i);
    }
    }
    }
    }

    void main()
    {
    int i;

    clrscr();

    for (i = 0; i < MAX; i++)
    visited[i] = 0;

    bfs(0);

    getch();
    }

    ReplyDelete
  8. // Practical 12: Write a program to find factorial of number using recursion.



    #include iostream.h
    #include conio.h

    long factorial(int n)
    {
    if (n == 0 || n == 1)
    return 1;
    else
    return n * factorial(n - 1);
    }

    void main()
    {
    int num;

    clrscr();

    cout << "Enter number: ";
    cin >> num;

    cout << "Factorial of " << num << " is " << factorial(num);

    getch();
    }

    ReplyDelete