Monday, 27 April 2020

DS TEST-3 SHORT ANSWER

QUESTIONS AND ANSWERS:



Q1.What is stack? Explain the algorithm of push() and pop() operation.

stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top.

Algorithm of push() and pop() operations:

Push operation
 Algorithm to push an item into stack.
            
    1) IF TOP = MAX   then
    Print “Stack is full”;
    Exit;
    2) Otherwise
    TOP: = TOP + 1;        /*increment TOP*/
    STACK (TOP):= ITEM;
    3) End of IF
    4) Exit
Pop operation
Algorithm to pop an element from stack.

    1) IF TOP = 0 then
        Print “Stack is empty”;
        Exit;
    2) Otherwise
        ITEM: =STACK (TOP);
        TOP:=TOP – 1;
    3) End of IF
    4) Exit

Q2.Define recursion and types of recursion?

Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls.

Example, sorting, searching, and traversal problem.

Types of recursion:

linear recursion
Tail recursion
Binary recursion
Exponential recursion.
Nested Recursion.
Mutual Recursion.

Q3.Explain tower of hanoi rules and write program.


Tower of Hanoi consists of three pegs or towers with n disks placed one over the other. The objective of the puzzle is to move the stack to another peg following these simple rules. Only one disk can be moved at a time. No disk can be placed on top of the smaller disk.

program:
#include<stdio.h>
#include<conio.h>
int main ()
{
clrscr();
int n;
printf("Enter number of disks required: \n");
scanf ("%d",  &n);
TOH (n, 'A', 'B',' C');
getch();
return 0;
}
void TOH (int n, char src, char spare, char dest)
{
if (n==1)
printf("Move from %c to %c \n", src, dest);

else
{
TOH(n-1, src, dest, spare) ;
TOH(1, src, spare, dest);
TOH(n-1, spare, src, dest);
}
}
Copy
Q4.Algorithm of postfix evaluation: 231*+ 9-

Let the given expression be “2 3 1 * + 9 -“. 

We scan all elements one by one.

1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’

2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)

3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’

4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1 which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.

5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 + 2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.

6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.

7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 – 9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.

8) There are no more elements to scan, we return the top element from stack (which is the only element left in stack).

Q5.Algorithm of infix to post fix conversion:(a+b)x(c-d)+e

"COMING SOON" 

NOTE: all answer are explained so before copying just go with answers once. Ping me for any help or support. 

thankyou:)




Saturday, 18 April 2020

SELECTION SORT

|| SELECTION SORT ||


WHAT IS SELECTION SORT?

Selection sort is a simple sorting algorithm.This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.


EXAMPLE:
Consider the array:
[10,5,2,1]
The first element is 10. The next part we must find the smallest number from the remaining array. The smallest number from 5 2 and 1 is 1. So, we replace 10 by 1.
The new array is [1,5,2,10] Again, this process is repeated.
Finally, we get the sorted array as [1,2,5,10].


ALGORITHM:

selectionSort(array, size)
  repeat (size - 1) times
  set the first unsorted element as the minimum
  for each of the unsorted elements
    if element < currentMinimum
      set element as new minimum
  swap minimum with first unsorted position
end selectionSort

CODE:

 // Selection sort in C

#include <stdio.h>

// function to swap the the position of two elements
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

void selectionSort(int array[], int size) {
  for (int step = 0; step < size - 1; step++) {
    int min_idx = step;
    for (int i = step + 1; i < size; i++) {

      // To sort in descending order, change > to < in this line.
      // Select the minimum element in each loop.
      if (array[i] < array[min_idx])
        min_idx = i;
    }

    // put min at the correct position
    swap(&array[min_idx], &array[step]);
  }
}

// function to print an array
void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i) {
    printf("%d  ", array[i]);
  }
  printf("\n");
}

// driver code
int main() {
  int data[] = {20, 12, 10, 15, 2};
  int size = sizeof(data) / sizeof(data[0]);
  selectionSort(data, size);
  printf("Sorted array in Acsending Order:\n");
  printArray(data, size);
}

THANKYOU:)

RAJ ARYAN

Monday, 13 April 2020

BUBBLE SORT

BUBBLE SORT


WHAT IS BUBBLE SORT?

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

EXAMPLE:

enter image description here


ALGORITHM:

begin BubbleSort(list)

   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
   
   return list
   
end BubbleSort

CODE:

#include <stdio.h>
void bubble_sort(int a[], int n) {
    int i = 0, j = 0, tmp;
    for (i = 0; i < n; i++) {   // loop n times - 1 per element
        for (j = 0; j < n - i - 1; j++) { // last i elements are sorted already
            if (a[j] > a[j + 1]) {  // swop if order is broken
                tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
            }
        }
    }
}
int main() {
  int a[100], n, i, d, swap;
  printf("Enter number of elements in the array:\n");
  scanf("%d", &n); 
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  bubble_sort(a, n);
  printf("Printing the sorted array:\n");
  for (i = 0; i < n; i++)
     printf("%d\n", a[i]);
  return 0;
}

WATCH FULL VIDEO ON YOUTUBE
  🔻🔻🔻🔻🔻🔻🔻


THANKYOU

RAJ ARYAN

BCA ASSIGNMENT 2K20.

ASSIGNMENT SOLUTION



DATABASE MANAGEMENT SYSTEM

ASSIGNMENT-1

CLICK HERE TO VIEW THE PDF.

DATA STRUCTURE

ASSIGNMENT-1

CLICK HERE TO VIEW DS PDF.

MATHEMATICS

ASSIGNMENT-1

NOTE: PING ME FOR ANY HELP OR SUPPORT !!

THANKS FOR VISITING:)

RAJ ARYAN

Wednesday, 8 April 2020

ARDUINO BOARD

Arduino Board 


   

Definition: 


Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. 

Specification: 

The Arduino board includes the following specifications. 
It is an ATmega328P based Microcontroller 
The Operating Voltage of the Arduino is 5V 
The recommended input voltage ranges from 7V to 12V 
The i/p voltage (limit) is 6V to 20V 
Digital input and output pins-14 
Digital input & output pins (PWM)-6 
Analog i/p pins are 6 
DC Current for each I/O Pin is 20 mA 
DC Current used for 3.3V Pin is 50 mA 
Flash Memory -32 KB, and 0.5 KB memory is used by the boot loader 
SRAM is 2 KB 
EEPROM is 1 KB 
The speed of the CLK is 16 MHz 
In Built LED 
Length and width of the Arduino are 68.6 mm X 53.4 mm 
The weight of the Arduino board is 25 g 

Application: 


  • Arduino Uno is used in Do-it-Yourself projects prototyping. 
  • In developing projects based on code-based control. 
  • Development of Automation System. 
  • Designing of basic circuit designs.



  • THANKYOU:)
  • ARYAN


JAVA PROGRAMMING CODES || AryanTechsource

  PART-A 1. Implement a stack using a stack class which has methods push, pop. import java.util.Stack; class StackMain { public static void ...

Popular Posts