Q

 Write a 'C' program to sort randomly generated array elements using Insertion sort

method. (Use Random Function)

___________________________________________________________________________

 

ans

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

void insertionSort(int arr[], int n)

{

                int i, key, j;

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

                                key = arr[i];

                                j = i - 1;

 

                                /* Move elements of arr[0..i-1], that are

                                greater than key, to one position ahead

                                of their current position */

                                while (j >= 0 && arr[j] > key) {

                                                arr[j + 1] = arr[j];

                                                j = j - 1;

                                }

                                arr[j + 1] = key;

                }

}

 

int main()

{

                int arr[100], n, i;

                printf("Enter number of elements: ");

                scanf("%d", &n);

               

                // Seed the random number generator

                srand(time(NULL));

               

                // Generate random array elements

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

                                arr[i] = rand() % 100;

               

                printf("Original array: \n");

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

                                printf("%d ", arr[i]);

               

                // Sort the array using Insertion sort

                insertionSort(arr, n);

               

                printf("\nSorted array: \n");

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

                                printf("%d ", arr[i]);

               

                return 0;

}

 

 

Q.Write a script in R to create a list of cities and perform the following

1) Give names to the elements in the list.

2) Add an element at the end of the list.

3) Remove the last element.

4) Update the 3rd Element

___________________________________________________________________________

# Create a list of cities

cities <- c("New York", "London", "Paris", "Berlin")

 

# Give names to the elements in the list

names(cities) <- c("City1", "City2", "City3", "City4")

 

# Add an element at the end of the list

cities <- c(cities, "Tokyo")

names(cities)[5] <- "City5"

 

# Remove the last element

cities <- cities[1:4]

 

# Update the 3rd Element

cities[3] <- "Madrid"

names(cities)[3] <- "City3"

# Display the final list

Cities

 

 

 

 

 

 

Write a C program to read integers and store them in banary search tree and display the nodes level wise

_______________________________________________________________________

#include<stdio.h>

#include<stdlib.h>

 

struct node{

    int data;

    struct node *left;

    struct node *right;

};

 

struct node *newNode(int data){

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

    temp->data = data;

    temp->left = NULL;

    temp->right = NULL;

    return temp;

}

 

struct node *insert(struct node *root, int data){

    if (root == NULL) return newNode(data);

    if (data < root->data)

        root->left = insert(root->left, data);

    else if (data > root->data)

        root->right = insert(root->right, data);

    return root;

}

 

void printLevel(struct node *root, int level){

    if (root == NULL) return;

    if (level == 1) printf("%d ", root->data);

    else if (level > 1){

        printLevel(root->left, level-1);

        printLevel(root->right, level-1);

    }

}

 

int height(struct node *root){

    if (root == NULL) return 0;

    int leftHeight = height(root->left);

    int rightHeight = height(root->right);

    if (leftHeight > rightHeight) return leftHeight + 1;

    else return rightHeight + 1;

}

 

void levelOrder(struct node *root){

    int h = height(root);

    int i;

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

        printLevel(root, i);

}

 

int main(){

    int n, i, data;

    struct node *root = NULL;

    printf("Enter the number of integers to be inserted in the binary search tree: ");

    scanf("%d", &n);

    printf("Enter the integers: \n");

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

        scanf("%d", &data);

        root = insert(root, data);

    }

    printf("The nodes of the binary search tree level wise are: \n");

    levelOrder(root);

    return 0;

}

 

 

 

 

Write a PHP script for the following: Design a form to accept two strings from the user. Find whether the small string appears at the start of the large string. Provide a text box to accept the string that will replace all occurrences of small string present in the large string. Also split the large string into separate words. (Use regular expressions)

 

<html>

  <head>

    <title>String Comparison and Replacement</title>

  </head>

  <body>

    <form action="" method="post">

      <label for="large_string">Large String:</label>

      <input type="text" name="large_string" required><br><br>

      <label for="small_string">Small String:</label>

      <input type="text" name="small_string" required><br><br>

      <label for="replace_string">Replace String:</label>

      <input type="text" name="replace_string" required><br><br>

      <input type="submit" value="Submit">

    </form>

 

    <?php

      if($_POST){

        $large_string = $_POST['large_string'];

        $small_string = $_POST['small_string'];

        $replace_string = $_POST['replace_string'];

 

        if(preg_match("/^$small_string/", $large_string)){

          echo "Small string appears at the start of large string<br><br>";

        }else{

          echo "Small string does not appear at the start of large string<br><br>";

        }

 

        $replaced_string = preg_replace("/$small_string/", $replace_string, $large_string);

        echo "Replaced string: $replaced_string<br><br>";

 

        echo "Split Large String into words:<br>";

        preg_match_all('/\b\w+\b/', $large_string, $matches);

        foreach ($matches[0] as $word) {

          echo $word . "<br>";

        }

      }

    ?>

  </body>

</html>

 

 

Write a 'C' program to sort randomly generated array elements using Insertion sort method. (Use Random Function)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

void insertSort(int arr[], int n){

    int i, key, j;

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

        key = arr[i];

        j = i - 1;

        while (j >= 0 && arr[j] > key) {

            arr[j + 1] = arr[j];

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}

 

int main() {

    int n, i;

    printf("Enter the number of elements: ");

    scanf("%d", &n);

    int arr[n];

    srand(time(0));

    printf("Random elements are: \n");

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

        arr[i] = rand() % 100 + 1;

        printf("%d ", arr[i]);

    }

    printf("\n");

    insertSort(arr, n);

    printf("Sorted elements are: \n");

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

        printf("%d ", arr[i]);

    return 0;

}

 

Write a 'C' program to accept two polynomial and find the addition of accepted polynomials.(use array)

 

#include <stdio.h>

 

struct Polynomial{

    int coeff;

    int expo;

};

 

struct Polynomial p1[10], p2[10], sum[10];

 

void input(struct Polynomial p[], int n){

    int i;

    printf("Enter the coefficient and exponent of the polynomial:\n");

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

        scanf("%d%d", &p[i].coeff, &p[i].expo);

    }

}

 

void add(struct Polynomial p1[], struct Polynomial p2[], int m, int n){

    int i, j, k;

    i = j = k = 0;

    while (i < m && j < n) {

        if (p1[i].expo > p2[j].expo) {

            sum[k++] = p1[i++];

        }

        else if (p1[i].expo < p2[j].expo) {

            sum[k++] = p2[j++];

        }

        else {

            sum[k].expo = p1[i].expo;

            sum[k++].coeff = p1[i++].coeff + p2[j++].coeff;

        }

    }

    while (i < m) {

        sum[k++] = p1[i++];

    }

    while (j < n) {

        sum[k++] = p2[j++];

    }

}

 

void output(struct Polynomial sum[], int m){

    int i;

    printf("Resultant polynomial is:\n");

    for (i = 0; i < m; i++) {

        printf("%d(x^%d) + ", sum[i].coeff, sum[i].expo);

    }

    printf("0\n");

}

 

int main() {

    int m, n;

    printf("Enter the number of terms of first polynomial: ");

    scanf("%d", &m);

    input(p1, m);

    printf("Enter the number of terms of second polynomial: ");

    scanf("%d", &n);

    input(p2, n);

    add(p1, p2, m, n);

    int k = m + n - 1;

    output(sum, k);

    return 0;

}

 

write a c program to evaluate polynomial using function

 

#include <stdio.h>

 

int polyEval(int coeff[], int degree, int x){

    int result = coeff[degree];

    int i;

    for (i = degree - 1; i >= 0; i--) {

        result = result * x + coeff[i];

    }

    return result;

}

 

int main() {

    int degree, x, i;

    printf("Enter the degree of the polynomial: ");

    scanf("%d", &degree);

    int coeff[degree + 1];

    printf("Enter the coefficients of the polynomial:\n");

    for (i = 0; i <= degree; i++) {

        scanf("%d", &coeff[i]);

    }

    printf("Enter the value of x: ");

    scanf("%d", &x);

    int result = polyEval(coeff, degree, x);

    printf("The value of the polynomial is: %d\n", result);

    return 0;

}

 

 

Write menu driven program using 'C' for Binary Search Tree. The menu includes

Create a Binary Search Tree

Traverse it by using Inorder and Postorder traversing technique

 

#include <stdio.h>

#include <stdlib.h>

 

struct node {

    int data;

    struct node *left;

    struct node *right;

};

 

struct node *root = NULL;

 

void insert(int data) {

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

    temp->data = data;

    temp->left = NULL;

    temp->right = NULL;

    if (root == NULL) {

        root = temp;

    } else {

        struct node *current = root;

        while (1) {

            if (data <= current->data) {

                if (current->left == NULL) {

                    current->left = temp;

                    return;

                } else {

                    current = current->left;

                }

            } else {

                if (current->right == NULL) {

                    current->right = temp;

                    return;

                } else {

                    current = current->right;

                }

            }

        }

    }

}

 

void inorder(struct node *root) {

    if (root == NULL) {

        return;

    }

    inorder(root->left);

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

    inorder(root->right);

}

 

void postorder(struct node *root) {

    if (root == NULL) {

        return;

    }

    postorder(root->left);

    postorder(root->right);

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

}

 

int main() {

    int choice, data;

    while (1) {

        printf("\nBinary Search Tree Operations:\n");

        printf("1. Insert node\n");

        printf("2. Inorder Traversal\n");

        printf("3. Postorder Traversal\n");

        printf("4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch(choice) {

            case 1:

                printf("Enter the data to be inserted: ");

                scanf("%d", &data);

                insert(data);

                break;

            case 2:

                printf("Inorder Traversal:\n");

                inorder(root);

                break;

            case 3:

                printf("Postorder Traversal:\n");

                postorder(root);

                break;

            case 4:

                exit(0);

            default:

                printf("Invalid choice\n");

        }

    }

    return 0;

}

 

 

 

 

 

 

Write a PHP script for the following: Design a form to accept two strings from the user. Find whether the small string appears at the start of the large string. Provide a text box to accept the string that will replace all occurrences of small string present in the large string. Also split the large string into separate words. (Use regular expressions)

 

<html>

  <body>

    <form method="post">

      Large String: <input type="text" name="large_string"><br><br>

      Small String: <input type="text" name="small_string"><br><br>

      Replace String: <input type="text" name="replace_string"><br><br>

      <input type="submit" name="submit" value="Submit">

    </form>

 

    <?php

      if(isset($_POST['submit'])) {

        $large_string = $_POST['large_string'];

        $small_string = $_POST['small_string'];

        $replace_string = $_POST['replace_string'];

 

        // Check if small string appears at start of large string

        if(preg_match("/^".$small_string."/", $large_string)) {

          echo "Small string appears at the start of the large string<br><br>";

        } else {

          echo "Small string does not appear at the start of the large string<br><br>";

        }

 

        // Replace all occurrences of small string in large string

        $new_string = preg_replace("/".$small_string."/", $replace_string, $large_string);

        echo "New String: ".$new_string."<br><br>";

 

        // Split large string into separate words

        $words = preg_split("/\s+/", $large_string);

        echo "Words: ";

        foreach($words as $word) {

          echo $word.", ";

        }

      }

    ?>

  </body>

</html>

 

 

Write a program to accept a postfix expression and evaluate the expression using the stack Example: Input: abed- Values: a 4, b-2, c-5, d-3 Answer: 12


 _________________________________________________________________

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

#define MAX_LEN 100

 

int stack[MAX_LEN];

int top = -1;

 

void push(int num)

{

    stack[++top] = num;

}

 

int pop()

{

    return stack[top--];

}

 

int evaluate(int num1, int num2, char operator)

{

    switch(operator)

    {

        case '+': return num1 + num2;

        case '-': return num1 - num2;

        case '*': return num1 * num2;

        case '/': return num1 / num2;

    }

    return 0;

}

 

int evaluatePostfix(char postfix[])

{

    int i, num1, num2;

    for(i = 0; i < strlen(postfix); i++)

    {

        if(isdigit(postfix[i]))

        {

            push(postfix[i] - '0');

        }

        else

        {

            num2 = pop();

            num1 = pop();

            push(evaluate(num1, num2, postfix[i]));

        }

    }

    return stack[top];

}

 

int main()

{

    char postfix[MAX_LEN];

    printf("Enter postfix expression: ");

    scanf("%s", postfix);

    printf("Result: %d", evaluatePostfix(postfix));

    return 0;

}

 

Write a 'C' program to create a singly linked list, reverse it and display both the list.

#include <stdio.h>

#include <stdlib.h>

 

struct Node

{

    int data;

    struct Node* next;

};

 

struct Node* head = NULL;

 

void insert(int data)

{

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

    temp->data = data;

    temp->next = head;

    head = temp;

}

 

void reverse()

{

    struct Node* prev = NULL;

    struct Node* current = head;

    struct Node* next;

    while (current != NULL)

    {

        next = current->next;

        current->next = prev;

        prev = current;

        current = next;

    }

    head = prev;

}

 

void display()

{

    struct Node* temp = head;

    while (temp != NULL)

    {

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

        temp = temp->next;

    }

}

 

int main()

{

    int num, i;

    printf("Enter number of elements: ");

    scanf("%d", &num);

    printf("Enter elements: ");

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

    {

        int data;

        scanf("%d", &data);

        insert(data);

    }

    printf("Original List: ");

    display();

    reverse();

    printf("\nReversed List: ");

    display();

    return 0;

}

 

Write PHP script to define an interface which has methods area), volume(). Define constant Pl. Create a class cylinder which implements this interface and calculate a and volume.

<?php

interface Shape

{

    const Pi = 3.14;

    public function area();

    public function volume();

}

 

class Cylinder implements Shape

{

    private $radius;

    private $height;

 

    public function __construct($radius, $height)

    {

        $this->radius = $radius;

        $this->height = $height;

    }

 

    public function area()

    {

        return (2 * self::Pi * $this->radius * $this->height) + (2 * self::Pi * $this->radius * $this->radius);

    }

 

    public function volume()

    {

        return self::Pi * $this->radius * $this->radius * $this->height;

    }

}

 

$cylinder = new Cylinder(5, 10);

 

echo "Area of Cylinder: " . $cylinder->area() . "<br>";

echo "Volume of Cylinder: " . $cylinder->volume();

 

?>





Write PHP Script to create a class account (accno,cust_name). Derive two classes from account as saving acc(balance, min amount) and current acc(balance, min_amount). Display a menu a) Saving Account b) Current Account For each of this display a menu with the following options. 1. Create account 2. Deposit 3. Withdrawal

class Account { private $accno; private $cust_name; function __construct($accno, $cust_name) { $this->accno = $accno; $this->cust_name = $cust_name; } public function getAccno() { return $this->accno; } public function getCustName() { return $this->cust_name; } } class SavingAcc extends Account { private $balance; private $min_amount; function __construct($accno, $cust_name, $balance, $min_amount) { parent::__construct($accno, $cust_name); $this->balance = $balance; $this->min_amount = $min_amount; } public function deposit($amount) { $this->balance += $amount; } public function withdraw($amount) { if ($this->balance - $amount >= $this->min_amount) $this->balance -= $amount; else echo "Withdrawal not possible. Minimum balance should be maintained."; } public function getBalance() { return $this->balance; } } class CurrentAcc extends Account { private $balance; private $min_amount; function __construct($accno, $cust_name, $balance, $min_amount) { parent::__construct($accno, $cust_name); $this->balance = $balance; $this->min_amount = $min_amount; } public function deposit($amount) { $this->balance += $amount; } public function withdraw($amount) { if ($this->balance - $amount >= $this->min_amount) $this->balance -= $amount; else echo "Withdrawal not possible. Minimum balance should be maintained."; } public function getBalance() { return $this->balance; } } $choice = null; while ($choice != 3) { echo "1. Saving Account\n"; echo "2. Current Account\n"; echo "3. Exit\n"; echo "Enter your choice: "; $choice = readline(); switch ($choice) { case 1: $acc = new SavingAcc(123, "John Doe", 1000, 500); $choice2 = null; while ($choice2 != 4) { echo "1. Create account\n"; echo "2. Deposit\n"; echo "3. Withdraw\n"; echo "4. Exit\n"; echo "Enter your choice: "; $choice2 = readline(); switch ($choice2) { case 1: echo "Account number: " . $acc->getAccno() . "\n"; echo "Customer name: " . $acc->getCustName() . "\n"; break; case 2: echo "


Post a Comment

Previous Post Next Post