Program
function[]=eu(x0,y0,xn,h,f)
y(1)=y0;
j=(xn-x0)/h;
for i=1:j
y(i+1)=y(i)+h*f(x0,y(i))
x0=x0+h;
printf('\ny(%g)=%g\n',x0,y(i+1));
end
endfunction
Output:
deff('y=f(x,y)','y=2+sqrt(x*y)')
eu(1,1,1.6,0.1,f)
y(1.1)=1.3
y(1.2)=1.61958
y(1.3)=1.95899
y(1.4)=2.31858
y(1.5)=2.69874
y(1.6)=3.09994
16 Comments
@Slip – 1
ReplyDeleteQ. 1) Write a PHP script to keep track of number of times the web page has been accessed (Use Session
Tracking).
Ans:
Q. 2)Create ‘Position_Salaries’ Data set. Build a linear regression model by identifying independent and
Target variable. Split the variables into training and testing sets. Then divide the training and testing sets
Into a 7:3 ratio, respectively and print them. Build a simple linear regression model.
Ans:
Import numpy as np
Import pandas as pd
From sklearn.model_selection import train_test_split
From sklearn.linear_model import LinearRegression
# Create the Position_Salaries dataset
Data = {‘Position’: [‘CEO’, ‘charman’, ‘director’, ‘Senior Manager’, ‘Junior Manager’, ‘Intern’],
‘Level’: [1, 2, 3, 4, 5, 6],
‘Salary’: [50000, 80000, 110000, 150000, 200000, 250000]}
Df = pd.DataFrame(data)
# Identify the independent and target variables
X = df.iloc[:, 1:2].values
Y = df.iloc[:, 2].values
# Split the variables into training and testing sets with a 7:3 ratio
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# Print the training and testing sets
Print(“X_train:\n”, X_train)
Print(“y_train:\n”, y_train)
Print(“X_test:\n”, X_test)
Print(“y_test:\n”, y_test)
# Build a simple linear regression model
Regressor = LinearRegression()
Regressor.fit(X_train, y_train)
# Print the coefficients and intercept
Print(“Coefficients:”, regressor.coef_)
Print(“Intercept:”, regressor.intercept_)
Practical 1 — 1D array (insert, delete, search, traverse)
ReplyDelete#include iostream
using namespace std;
void traverseArray(int arr[], int size) {
cout << "Array elements: ";
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void insertElement(int arr[], int &size, int capacity, int element, int position) {
if (size == capacity || position < 0 || position > size) {
cout << "Insertion failed!" << endl;
return;
}
for (int 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) {
if (position < 0 || position >= size) {
cout << "Invalid position!" << endl;
return;
}
for (int 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) {
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
cout << "Element " << element << " found at position " << i << "." << endl;
return;
}
}
cout << "Element not found." << endl;
}
int main() {
const int capacity = 100;
int arr[capacity], size = 0;
int choice, element, position;
do {
cout << "\nMenu:\n1.Traverse 2.Insert 3.Delete 4.Search 5.Exit\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.\n"; break;
default: cout << "Invalid choice!\n";
}
} while (choice != 5);
return 0;
}
Practical 2 — Stack using arrays (push, pop, peek, display)
ReplyDelete#include iostream
using namespace std;
#define MAX 100
class Stack {
int arr[MAX];
int top;
public:
Stack() { top = -1; }
bool isEmpty() { return top == -1; }
bool isFull() { return top == MAX - 1; }
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() {
if (isEmpty()) { cout << "Stack is empty." << endl; return; }
cout << "Stack elements from top to bottom: ";
for (int i = top; i >= 0; i--) cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Stack s;
int choice, value;
do {
cout << "\nStack Operations Menu:\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\nEnter 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." << endl; break;
default: cout << "Invalid choice! Try again." << endl;
}
} while (choice != 5);
return 0;
}
Practical 3 — Infix → Postfix and Evaluate
ReplyDelete#include iostream
#include
#include
#include
using namespace std;
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
string infixToPostfix(const string &infix) {
stack s;
string postfix;
for (char c : infix) {
if (isdigit(c)) {
postfix += c;
} else if (c == '(') {
s.push(c);
} else if (c == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top(); s.pop();
}
if (!s.empty()) s.pop();
} else {
while (!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top(); s.pop();
}
s.push(c);
}
}
while (!s.empty()) { postfix += s.top(); s.pop(); }
return postfix;
}
int evaluatePostfix(const string &postfix) {
stack s;
for (char c : postfix) {
if (isdigit(c)) s.push(c - '0');
else {
int b = s.top(); s.pop();
int a = s.top(); s.pop();
switch (c) {
case '+': s.push(a + b); break;
case '-': s.push(a - b); break;
case '*': s.push(a * b); break;
case '/': s.push(a / b); break;
case '^': {
int res = 1; for (int i = 0; i < b; i++) res *= a;
s.push(res); break;
}
}
}
}
return s.top();
}
int main() {
string infix;
cout << "Enter infix expression (single digit operands): ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix: " << postfix << "\n";
cout << "Result: " << evaluatePostfix(postfix) << "\n";
return 0;
}
Practical 4 — Queue using array (teacher uses dynamic array in PDF)
ReplyDelete#include iostream
using namespace std;
class Queue {
int front, rear, capacity;
int* arr;
public:
Queue(int size) {
capacity = size;
arr = new int[capacity];
front = -1; rear = -1;
}
~Queue() { delete[] arr; }
bool isFull() { return rear == capacity - 1; }
bool isEmpty() { return front == -1 || front > rear; }
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() {
if (isEmpty()) { cout << "Queue is empty." << endl; return; }
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) cout << arr[i] << " ";
cout << endl;
}
};
int main() {
int size, choice, val;
cout << "Enter queue capacity: "; cin >> size;
Queue q(size);
do {
cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nEnter choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter value to enqueue: "; cin >> val; q.enqueue(val); break;
case 2: q.dequeue(); break;
case 3: q.display(); break;
case 4: cout << "Exiting..." << endl; break;
default: cout << "Invalid choice." << endl;
}
} while (choice != 4);
return 0;
}
Practical 5 — Circular Queue (teacher’s version with visual helper)
ReplyDelete#include iostream
#include vector
using namespace std;
class CircularQueue {
int capacity, front, rear;
vector arr;
public:
CircularQueue(int size): capacity(size), front(-1), rear(-1), arr(size) {}
bool isFull() { return capacity == 0 ? true : (front == (rear + 1) % capacity); }
bool isEmpty() { return front == -1; }
void showCircle() {
cout << "\nQueue indices: ";
for (int i = 0; i < capacity; i++) cout << i << " ";
cout << "\nQueue view: ";
vector vis(capacity, '_');
if (!isEmpty()) {
int i = front;
while (true) {
vis[i] = 'E';
if (i == rear) break;
i = (i + 1) % capacity;
}
}
for (char c : vis) cout << c << " ";
cout << "\nFront: " << front << ", Rear: " << rear << "\n\n";
}
void enqueue(int val) {
if (capacity == 0) { cout << "Capacity must be > 0\n"; return; }
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;
showCircle();
}
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;
showCircle();
}
void display() {
if (isEmpty()) { cout << "Queue is empty." << endl; return; }
cout << "Queue elements: ";
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % capacity;
}
cout << endl;
}
};
int main() {
int size, choice, val;
cout << "Enter queue capacity: "; cin >> size;
CircularQueue cq(size);
do {
cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\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.\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 4);
return 0;
}
Practical 6 — Singly Linked List (insert beginning, end, position)
ReplyDelete#include iostream
#include cstdlib
using namespace std;
struct Node { int data; Node* next; };
void insertAtBeginning(Node*& head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = head; head = newNode;
}
void insertAtEnd(Node*& head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = NULL;
if (head == NULL) { head = newNode; return; }
Node* temp = head;
while (temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
void insertAtPosition(Node*& head, int value, int position) {
if (position <= 1) { insertAtBeginning(head, value); return; }
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
Node* temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++) temp = temp->next;
if (temp == NULL) { cout << "Position out of bounds. Inserting at end." << endl; insertAtEnd(head, value); free(newNode); return; }
newNode->next = temp->next; temp->next = newNode;
}
void display(Node* head) {
if (head == NULL) { cout << "List is empty." << endl; return; }
Node* temp = head;
while (temp != NULL) { cout << temp->data << " -> "; temp = temp->next; }
cout << "NULL" << endl;
}
int main() {
Node* head = NULL;
int choice, value, pos;
do {
cout << "\nMenu:\n1. Insert at Beginning\n2. Insert at End\n3. Insert at Position\n4. Display\n5. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter value to insert at beginning: "; cin >> value; insertAtBeginning(head, value); break;
case 2: cout << "Enter value to insert at end: "; cin >> value; insertAtEnd(head, value); break;
case 3: cout << "Enter value to insert: "; cin >> value; cout << "Enter position: "; cin >> pos; insertAtPosition(head, value, pos); break;
case 4: display(head); break;
case 5: cout << "Exiting..." << endl; break;
default: cout << "Invalid choice!";
}
} while (choice != 5);
return 0;
}
Practical 7 — Doubly Linked List (insert, delete, traverse forward & backward)
ReplyDelete#include iostream
#include cstdlib
using namespace std;
struct Node { int data; Node* prev; Node* next; };
Node* head = NULL;
void insertEnd(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next = NULL; newNode->prev = NULL;
if (head == NULL) { head = newNode; return; }
Node* temp = head; while (temp->next != NULL) temp = temp->next;
temp->next = newNode; newNode->prev = temp;
}
void deleteNode(int value) {
Node* temp = head;
while (temp != NULL && temp->data != value) temp = temp->next;
if (temp == NULL) return;
if (temp->prev != NULL) temp->prev->next = temp->next; else head = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
}
void traverseForward() {
Node* temp = head;
while (temp != NULL) { cout << temp->data << " "; temp = temp->next; }
cout << endl;
}
void traverseBackward() {
Node* temp = head;
if (temp == NULL) return;
while (temp->next != NULL) temp = temp->next;
while (temp != NULL) { cout << temp->data << " "; temp = temp->prev; }
cout << endl;
}
int main() {
insertEnd(10); insertEnd(20); insertEnd(30);
cout << "Forward: "; traverseForward();
cout << "Backward: "; traverseBackward();
deleteNode(20);
cout << "After deletion (forward): "; traverseForward();
cout << "After deletion (backward): "; traverseBackward();
return 0;
}
Practical 8 — Circular Linked List (insert at end, delete by value, display)
ReplyDelete#include iostream
#include cstdlib
using namespace std;
struct Node { int data; Node* next; };
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data; newNode->next = NULL; return newNode;
}
void insertAtEnd(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == NULL) { head = newNode; newNode->next = head; return; }
Node* temp = head; while (temp->next != head) temp = temp->next;
temp->next = newNode; newNode->next = head;
}
void deleteNode(Node*& head, int key) {
if (head == NULL) { cout << "List is empty." << endl; return; }
Node *temp = head, *prev = NULL;
do {
if (temp->data == key) {
if (temp == head && temp->next == head) { free(head); head = NULL; }
else if (temp == head) {
while (temp->next != head) temp = temp->next;
temp->next = head->next;
Node* toFree = head; head = head->next; free(toFree);
} else {
prev->next = temp->next; free(temp);
}
return;
}
prev = temp; temp = temp->next;
} while (temp != head);
cout << "Key not found." << endl;
}
void displayList(Node* head) {
if (head == NULL) { cout << "List is empty." << endl; return; }
Node* temp = head;
do { cout << temp->data << " "; temp = temp->next; } while (temp != head);
cout << endl;
}
int main() {
Node* head = NULL;
insertAtEnd(head, 10); insertAtEnd(head, 20); insertAtEnd(head, 30);
cout << "Circular Linked List: "; displayList(head);
deleteNode(head, 20);
cout << "After deleting 20: "; displayList(head);
return 0;
}
Practical 10 — BFS (teacher adjacency-list + queue)
ReplyDelete#include iostream
#include queue
#include map
#include vector
using namespace std;
int main() {
map> graph;
graph['A'] = {'B', 'E', 'D'};
graph['B'] = {'A', 'C'};
graph['C'] = {'B'};
graph['D'] = {'A'};
graph['E'] = {'A', 'F'};
graph['F'] = {'E', 'G', 'H'};
graph['G'] = {'F'};
graph['H'] = {'F'};
queue q;
map visited;
char start = 'A';
q.push(start);
visited[start] = true;
cout << "BFS Traversal starting from A: ";
while (!q.empty()) {
char node = q.front(); q.pop();
cout << node << " ";
for (char neigh : graph[node]) {
if (!visited[neigh]) { visited[neigh] = true; q.push(neigh); }
}
}
cout << endl;
return 0;
}
11.1 Bubble Sort
ReplyDelete#include iostream
using namespace std;
int main() {
int n; cout << "Enter number of elements: "; cin >> n;
int a[100]; cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (a[j] > a[j + 1]) swap(a[j], a[j + 1]);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl; return 0;
}
11.2 Selection Sort
ReplyDelete#include iostream
using namespace std;
int main() {
int n; cout << "Enter number of elements: "; cin >> n;
int a[100]; cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) if (a[j] < a[minIndex]) minIndex = j;
swap(a[i], a[minIndex]);
}
cout << "Sorted array: ";
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl; return 0;
}
11.3 Insertion Sort
ReplyDelete#include iostream
using namespace std;
int main() {
int n; cout << "Enter number of elements: "; cin >> n;
int a[100]; cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; }
a[j + 1] = key;
}
cout << "Sorted array: ";
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl; return 0;
}
11.4 Merge Sort (teacher uses divide & merge)
ReplyDelete#include iostream
using namespace std;
void mergeArr(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid;
int *L = new int[n1], *R = new int[n2];
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++];
delete[] L; delete[] R;
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right);
mergeArr(arr, left, mid, right);
}
}
int main() {
int n; cout << "Enter number of elements: "; cin >> n;
int *arr = new int[n]; cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) cin >> arr[i];
mergeSort(arr, 0, n - 1);
cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl; delete[] arr; return 0;
}
11.5 Quick Sort
ReplyDelete#include iostream
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); }
}
swap(arr[i + 1], arr[high]); return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high);
}
}
int main() {
int n; cout << "Enter number of elements: "; cin >> n;
int *arr = new int[n]; cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) cin >> arr[i];
quickSort(arr, 0, n - 1);
cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl; delete[] arr; return 0;
}
Practical 12 — Factorial using recursion
ReplyDelete#include iostream
using namespace std;
long factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
int main() {
int num; cout << "Enter number: "; cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}