Program
function []=tra(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;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
I=I+2*f(1,j);
end;
I=(h/2.0)*I
printf('Integration by Trapezoidal rule =%f',I);
endfunction
Output:
-->exec('C:\Users\acer\Documents\sp5(1).sce', -1)
-->x=(0:0.1:1.6);
-->tra(x,sin(x))
Integration by Trapezoidal rule =1.028342
function []=tra(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;
n=xn;
h=x(2)-x(1);
I=f(1,1)+f(1,n);
for j=2:n-1
I=I+2*f(1,j);
end;
I=(h/2.0)*I
printf('Integration by Trapezoidal rule =%f',I);
endfunction
Output:
-->exec('C:\Users\acer\Documents\sp5(1).sce', -1)
-->x=(0:0.1:1.6);
-->tra(x,sin(x))
Integration by Trapezoidal rule =1.028342


3 Comments
// Practical 1: Write a program to perform insertion, deletion, searching and traversal in 1D array.
ReplyDelete#include
#include
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();
}
// Practical 2: Implement stack using arrays with push, pop, peek and display operations.
ReplyDelete#include
#include
#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();
}
// Practical 3: Write a program to convert an infix expression into postfix and evaluate it.
ReplyDelete#include
#include
#include
#include
#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();
}