Introduction to C Programming-Algorithms

Last updated on Sep 26,2020 46.9K Views

Introduction to C Programming-Algorithms

 What is algorithm?

An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of writing a program.

For writing any programs, the following has to be known:

  • Input
  • Tasks to be preformed
  • Output expected

For any task, the instructions given to a friend is different from the instructions given to a computer. Let’s look at the difference between them and know how to give instructions to a computer.

Instruction to a Friend:

Step 1: Go to the College Board.

Step 2: Search for my name on the board.

Step 3: Check the rank against my name.

Instructions to the Computer:

Step 1: Go to the College Board.

Step 2: Read the first name.

Step 3: If this is my name, see rank and come back.

Step 4: Read the next name.

Step 5: Repeat the above two steps until you find my name.

Breaking down the steps into smaller steps is called algorithm.

Example:

Considering the above example, let’s break down the task into steps:

  • Input – Name
  • Tasks to be preformed – Search for my name    
  • Output expected – My rank

It is a good practice to write down the algorithm first before attempting at writing a program. Each step of the algorithm will get converted to a line or a set of lines in the programming language.

More Examples for understanding the concept of Algorithms:

Example 1: Telling A Friend How To Boil Water In A Kettle.

Let’s look at the 3 requirements for writing an algorithm for this task:

  • Input – Kettle, Water
  • Tasks to be preformed – Boiling water, fill up the kettle
  • Output expected – Boiled water

Instructions to a Friend:

Step 1: Fill the kettle with water.

Step 2: Place it on the stove and turn on the burner.

Step 3: Turn off the burner when the water starts boiling.

Instructions to the Computer:

Step 1: Put the kettle under the tap.

Step 2: Turn on the tap.

Step 3: Check if 90% of the kettle is filled.

Step 4: If not, repeat the above step.

Step 5: Turn off the tap.

Step 6: Place the kettle in the burner.

Step 7: Turn on the burner.

Step 8: Check if the water is 100 C

Step 9: If not, repeat the above step.

Step 10: Turn off the burner.

Example 2: Shopping on M.G Road

Let’s break down the task of buying a red color shoe with a bow, from the Bata showroom. The following are the 3 requirements for writing an algorithm for this task:

  • Input – Name of the shop, name of the road, color of the shoe, size of the shoe.
  • Tasks to be preformed – Shopping.
  • Output expected – Shoe.

The algorithm of this task for a friend is fairly easy and simple. So, let’s look at the algorithm for the computer.

Step 1: Walk to the first shop on M.G road.

Step 2: Check if this in the Bata Showroom.

Step 3: Enter the shop and ask for a red shoe size 5.

Step 4: Buy the shoe and come back.

Step 5: Go to the next shop, repeat step 2

Writing algorithms is a crucial step in programming. Take up more real life examples and try to write algorithms for them, and keep practising till the time writing algorithms becomes second nature to you.

Got a question for us? Pleas mention them in the comments section and we will get back to you.

Related Posts:

C & Data Structures Training

C Tutorial

Pointers in C

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.