## Algorithms

What are algorithms?

In programming, algorithm are the set of well defined instruction in sequence to solve a program. An algorithm should always have a clear stopping point.

Qualities of a good algorithm

1. Inputs and outputs should be defined precisely.

2. Each steps in algorithm should be clear and unambiguous.

3. Algorithm should be most effective among many different ways to solve a problem.

4. An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages.

In programming, algorithm are the set of well defined instruction in sequence to solve a program. An algorithm should always have a clear stopping point.

Qualities of a good algorithm

1. Inputs and outputs should be defined precisely.

2. Each steps in algorithm should be clear and unambiguous.

3. Algorithm should be most effective among many different ways to solve a problem.

4. An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages.

## Examples Of Algorithms In Programming

## An algorithm to add two numbers entered by user

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop

## Programming and Screen Shot

I used Java Script to add 2 numbers entered by user.

## An algorithm to find the largest among three different numbers entered by user

Step 1: Start

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a>b

If a>c

Display a is the largest number.

Else

Display c is the largest number.

Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a>b

If a>c

Display a is the largest number.

Else

Display c is the largest number.

Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

## Programming and Screen Shot

I used C Programming language to compile it.

/* C program to find largest number using if...else statement */

#include <stdio.h>

int main(){

float a, b, c;

printf("Enter three numbers: ");

scanf("%f %f %f", &a, &b, &c);

if (a>=b)

{

if(a>=c)

printf("Largest number = %.2f",a);

else

printf("Largest number = %.2f",c);

}

else

{

if(b>=c)

printf("Largest number = %.2f",b);

else

printf("Largest number = %.2f",c);

}

return 0;

}

#include <stdio.h>

int main(){

float a, b, c;

printf("Enter three numbers: ");

scanf("%f %f %f", &a, &b, &c);

if (a>=b)

{

if(a>=c)

printf("Largest number = %.2f",a);

else

printf("Largest number = %.2f",c);

}

else

{

if(b>=c)

printf("Largest number = %.2f",b);

else

printf("Largest number = %.2f",c);

}

return 0;

}

## An algorithm to find all roots of a quadratic equation ax2+bx+c=0.

Step 1: Start

Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;

Step 3: Calculate discriminant

D←b2-4ac

Step 4: If D≥0

r1←(-b+√D)/2a

r2←(-b-√D)/2a

Display r1 and r2 as roots.

Else

Calculate real part and imaginary part

rp←b/2a

ip←√(-D)/2a

Display rp+j(ip) and rp-j(ip) as roots

Step 5: Stop

Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;

Step 3: Calculate discriminant

D←b2-4ac

Step 4: If D≥0

r1←(-b+√D)/2a

r2←(-b-√D)/2a

Display r1 and r2 as roots.

Else

Calculate real part and imaginary part

rp←b/2a

ip←√(-D)/2a

Display rp+j(ip) and rp-j(ip) as roots

Step 5: Stop

## Programming and Screen Shot

## An algorithm to find the factorial of a number entered by user.

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+1

Step 6: Display factorial

Step 7: Stop

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+1

Step 6: Display factorial

Step 7: Stop

## Programming and Screen Shot

## An algorithm to check whether a number entered by user is prime or not.

Step 1: Start

Step 2: Declare variables n,i,flag.

Step 3: Initialize variables

flag←1

i←2

Step 4: Read n from user.

Step 5: Repeat the steps until i<(n/2)

5.1 If remainder of n÷i equals 0

flag←0

Go to step 6

5.2 i←i+1

Step 6: If flag=0

Display n is not prime

else

Display n is prime

Step 7: Stop

Step 2: Declare variables n,i,flag.

Step 3: Initialize variables

flag←1

i←2

Step 4: Read n from user.

Step 5: Repeat the steps until i<(n/2)

5.1 If remainder of n÷i equals 0

flag←0

Go to step 6

5.2 i←i+1

Step 6: If flag=0

Display n is not prime

else

Display n is prime

Step 7: Stop

## Programming and Screen Shot

## An algorithm to find the Fibonacci series till term≤1000.

Step 1: Start

Step 2: Declare variables first_term,second_term and temp.

Step 3: Initialize variables first_term←0 second_term←1

Step 4: Display first_term and second_term

Step 5: Repeat the steps until second_term≤1000

5.1: temp←second_term

5.2: second_term←second_term+first term

5.3: first_term←temp

5.4: Display second_term

Step 6: Stop

Step 2: Declare variables first_term,second_term and temp.

Step 3: Initialize variables first_term←0 second_term←1

Step 4: Display first_term and second_term

Step 5: Repeat the steps until second_term≤1000

5.1: temp←second_term

5.2: second_term←second_term+first term

5.3: first_term←temp

5.4: Display second_term

Step 6: Stop

## Programming and Screen Shot

I used C++ for this algorithm.

Algorithm is not the computer code. Algorithm are just the instructions which gives clear idea to you idea to write the computer code.

## Real Life Application

Let's say that you have a friend arriving at the airport, and your friend needs to get from the airport to your house. Here are four different algorithms that you might give your friend for getting to your home:

**The taxi algorithm**:

1. Go to the taxi stand.

2. Get in a taxi.

3. Give the driver my address.

**The call-me algorithm**:

1. When your plane arrives, call my cell phone.

2. Meet me outside baggage claim.

**The rent-a-car algorithm**:

1. Take the shuttle to the rental car place.

2. Rent a car.

3. Follow the directions to get to my house.

**The bus algorithm**:

1. Outside baggage claim, catch bus number 70.

2. Transfer to bus 14 on Main Street.

3. Get off on Elm street.

4. Walk two blocks north to my house.

All four of these algorithms accomplish exactly the same goal, but each algorithm does it in completely different way. Each algorithm also has a different cost and a different travel time. Taking a taxi, for example, is probably the fastest way, but also the most expensive. Taking the bus is definitely less expensive, but a whole lot slower. You choose the algorithm based on the circumstances.

## Video

## Used Materials:

- Phone
- Microphone
- Headphone
- Computer
- Video Animator(GOANIMATE)
- C Programming Language
- Java Script Programming Language
- C++ Programming Language
- Internet

## Reflection Questions:

1. What did I learn?

How to use a phone.

2. What is something that I did during this project that I will remember for the rest of your life?

Talking to my friend.

3. What was the most challenging part of this project for me?

Finding the time.

4. What is something that was hard for me at the start of the project, but is easy now?

Finding what to talk with my friend about.

5. In what area do I feel that I have made my biggest improvements?

Communication

6. If I could turn back time and do this project again, what would I do differently?

Talking with my friend longer.

How to use a phone.

2. What is something that I did during this project that I will remember for the rest of your life?

Talking to my friend.

3. What was the most challenging part of this project for me?

Finding the time.

4. What is something that was hard for me at the start of the project, but is easy now?

Finding what to talk with my friend about.

5. In what area do I feel that I have made my biggest improvements?

Communication

6. If I could turn back time and do this project again, what would I do differently?

Talking with my friend longer.