Docsity

Understanding Algorithms: Instructions for Solving Problems and Converting Temperatures, Exercises of Computer science

Partial preview of the text

Download Understanding Algorithms: Instructions for Solving Problems and Converting Temperatures and more Exercises Computer science in PDF only on Docsity!

Algorithm

BY HIMAL BHANDARI

Algorithm

 (^) an algorithm is a set of well-defined instructions to solve a particular problem. It takes a set of input and produces a desired output. For example,  (^) An algorithm to add two numbers: Step 1:start Step 2:Take two number inputs Step 3:Add numbers using the + operator Step4:Display the result Step 5: stop

write a Algorithms to make a

cup of tea

Step 1: Step 2:Fill up the kettle with water. Step 3: Boil the kettle. Step 4: Place a teabag in your favorite mug. Step 5: Pour boiling water into your favorite mug. Step 6: Brew the tea for a few moments. Step 7: Remove and dispose of the teabag. Step 8: Add milk. Step 9: Add sugar. Step 10: Stop

Algorithm & Flowchart to convert

temperature from Celsius to Fahrenheit

Step-1: Start Step-2: Input temperature in Celsius say C Step-3: F = (9.0/5.0 x C) + 32 Step-4: Display Temperature in Fahrenheit F Step-5: Stop

Algorithm & Flowchart to Swap Two Numbers using Temporary Variable Step-1: Start Step-2: Input Two Numbers Say NUM1,NUM Step-3: Display Before Swap Values NUM1, NUM Step-4: TEMP = NUM Step-5: NUM1 = NUM Step-6: NUM2 = TEMP Step-7: Display After Swap Values NUM1,NUM Step-8: Stop

Algorithm & Flowchart to find the smallest of two numbers: Step-1: Start Step-2: Input two numbers say NUM1,NUM Step-3: IF NUM1 < NUM2 THEN print smallest is NUM ELSE print smallest is NUM Step-4: Stop

Algorithm & Flowchart to find sum of

series 1+2+3+…..+N:

Step-1: Start Step-2: Input Value of N Step-3: I = 1, SUM= Step-4: IF (I >N) THEN GO TO Step- Step-5: SUM = SUM + I Step-6: I = I + 1 Step-7: Go to step- Step-8: Display value of SUM Step-9: Stop

Find the factorial of a number

Step 1: Start Step 2: Declare variables n, factorial and i. Step 3: Initialize variables factorial ← 1 i ← 1 Step 4: Read value of n Step 5: Repeat the steps until i = n 5.1: factorial ← factorial*i 5.2: i ← i+ Step 6: Display factorial Step 7: Stop

Bubble Sort Algorithm

Step1: Start Step2: begin BubbleSortAlgorithm( Array ) Step3:For all the elements of the array step3.1: if array[i] > array [i + 1] switch ( array[i] , array[i+1]) end if end for Step 4: return array Step5: end BubbleSortAlgorithm

Bubble Sort Algorithm

Step1: Start // 1st^ pass, 2nd^ pass … (n-1) Step2: begin BubbleSortAlgorithm( Array ) Step3:For all the elements of the array till (n-1) step3.1: Starting from the first index, compare the first and the second elements. step3.2: If the first element is greater than the second element, they are swapped. The above Step5: endprocess goes on until the last element. Step 4: Display array

Check whether a number is

prime or not ( a number that is divisible only by itself

and 1STEP 1: Take num as input. )

STEP 2: Initialize a variable temp to 1. STEP 3: Iterate a “for” loop from 2 to num-1. STEP 4: If num is divisible by lo op iterator, then update temp value to 0. STEP 5: If the temp is equal to 1, Return “Num IS PRIME”. Else, Return “Num IS NOT PRIME”. Step6: Stop

Algorithm vs Code

 (^) Algorithm: is a series of steps for solving a problem, completing a task or performing a calculation. Algorithms are usually executed by computer programs but the term can also apply to steps in domains such as mathematics for human problem solving.  (^) Code is a series of steps that machines can execute. In many cases, code is composed in a high level language that is then automatically translated into instructions that machines understand.  (^) The term algorithm is traditionally used to indicate code that has a highly optimized design such as a well accepted solution to a complex problem in computer science. The term suggests code that has a rigorous design that solves a daunting problem.

The generation process of code

 (^) code generation is the process by which a compiler's code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine.

Language Processors –

 (^) Compilers, interpreters, translate programs written in high-level languages into machine code that a computer understands. And assemblers translate programs written in low-level or assembly language into machine code.

**1. Compiler

  1. Pre-processor
  2. Assembler
  3. Interpreter
  4. linker**