Jagannath Institute of Management Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: Programming In C Department of Information Technology Created By: Dr. Arpana Chaturvedi
YOU CAN FIND THE SECRET TO SUCCESS IN YOUR OWN STUDY ROOM. THINK OF THAT AND FOLLOW THAT 1. ROOF OF YOUR ROOM SAYS: AIM HIGH 2. A/C OR FAN IN THE ROOM SAYS: BE COOL 3. CLOCK SAYS: VALUE YOUR TIME, TIME NEVER COMES BACK, EVERY SECOND OF YOUR LIFE IS PRECIOUS, TIME IS MONEY 4. MIRROR SAYS: ALWAYS REFLECT BEFORE YOU ACT 5. WINDOW SAYS; LOOK AT THE WORLD. HOW BEAUTIFUL IT IS? WHAT A WONDERFUL CREATIVITY IT HAS? WE DO NOT KNOW WHO HAS CREATED. UNIMAGINATIVE POWER. 6. CALENDER SAY: BE UP-TO-DATE WITH TIME. IT IS THE DEMAND OF TODAY. YOU HAVE TO LIVE WITH LATEST TECHNOLOGY, IT CHANGING VERY FAST. WITH HIGH DEMAND AND EXPECTATION, TECHNOLOGY IS CHANGING ITSELF AND GIVING ITS BEST. WE NEED TO BE UPDATED WITH THE CHANGE. 7. DOOR SAY: PUSH HARD TO ACHIEVE. PLEDGE TO BE TAKEN
✓ UNTIL YOU SPREAD YOR WINGS, YOU WILL HAVE NO IDEA HOW FAR YOU CAN FLY. ✓ BELIEVE IN THIS THAT EVERYONE IN THIS PLANET IS LOGICAL. ✓ ONE HAS TO REALISE TO MOVE FORWARD ✓ LOGIC WILL GET YOU FROM A TO Z: IMAGINATION WILL GET YOU EVERYWHERE ✓ START TELLING YOURSELF YOU CAN ✓ EXPOSE YOURSELF TO YOUR DEEPEST FEAR, AFTER THAT, FEAR HAS NO POWER, YOU ARE FREE ✓ SAY- I CAN ACCEPT FAILURE, BUT I CANNOT ACCEPT NOT TRYING. ✓ NEVER SAY “I CANT” ASK YOURSELF “HOW CAN I” ✓ YOU ARE THE AUTHOR OF YOUR LIFE, DON’T BE AFRAID TO EDIT OR CHANGE THE SCRIPT ✓ THE QUALITY OF YOUR THINKING DETERMINES THE QUALITY OF YOUR LIFE PLEDGE TO BE TAKEN
PLEDGE TO BE TAKEN MAKE A PROMISE TO YOURSELF YOU WILL DO ALL PROGRAMS USING YOUR OWN LOGIC YOU WILL NEVER COPY IT FROM OTHERS YOU SOLVE YOUR ALL ERRORS BY YOURSELF YOU WILL ALWAYS ASK YOUR DOUBTS THEN AND THERE
Subject: Programming In C Topic: Unit I-Algorithm
Unit-I Introduction to Programming ▰ Algorithm ▰ Properties of an Algorithms ▰ Benefits of an Algorithm ▰ Examples to Design Algorithm ▰ Assignment I
Algorithm
Algorithm ▰ The term algorithm is originally derived from the phonetic pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al- Khowarizmi, who was an Arabic mathematician who invented a set of rules for performing the four basic arithmetic operations (addition, subtraction, multiplication and division) on decimal numbers. ▰ An algorithm is a representation of a solution to a problem written in the form of simple English like statement which are logical and gives the desired output in less execution time.
Algorithm ▰ Problem always contains some given information and looks for some results to be obtained out of it. ▰ Hence every problem is to be understood carefully and logically about the procedure to be applied on given information so that efficiently the required output can be obtained. ▰ Here efficiency means the procedure should not consume more execution time as well as memory space. ▰ Finding of the best suitable process logic and then describing of this found procedure in English like statements is called Algorithm.
More Definitions: Algorithm ▰ Algorithm can be defined as: “A sequence of activities to be processed for getting desired output from a given input.” ▰ We can say that an Algorithm is a set of sequence of clear, well defined steps to be followed to solve a problem. Programming will be the final step or form to express these set of sequential instruction to reach to the solution. ▰ Webopedia defines an algorithm as: “A formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point”.
Points to be considered while writing an Algorithm ▰ Define the required Inputs and Outputs clearly. ▰ Name of variables should be small and meanings should be clearly defined. ▰ The steps of algorithms should be sequential until not using any control statements. ▰ In case of usage of Control statement, the return of step should be clearly mentioned with step number. ▰ Getting specified output is essential after algorithm is executed. ▰ One will get output only if algorithm stops after finite time. ▰ Activities in an algorithm to be clearly defined in other words for it to be unambiguous.
Properties of an Algorithm Donald Ervin Knuth has given a list of five properties for an algorithm, these properties are: ▰ Finiteness: An algorithm must always terminate after a finite number of steps. It means after every step one reach closer to solution of the problem and after a finite number of steps algorithm reaches to an end point. ▰ Definiteness: Each step of an algorithm must be precisely defined. It is done by well thought actions to be performed at each step of the algorithm. Also the actions are defined unambiguously for each activity in the algorithm.
Properties of an Algorithm ▰ Input: Any operation you perform need some beginning value/quantities associated with different activities in the operation. So the value/quantities are given to the algorithm before it begins. ▰ Output: One always expects output/result (expected value/quantities) in terms of output from an algorithm. The result may be obtained at different stages of the algorithm. If some result is from the intermediate stage of the operation then it is known as intermediate result and result obtained at the end of algorithm is known as end result. The output is expected value/quantities always have a specified relation to the inputs
Properties of an Algorithm ▰ Effectiveness: Algorithms to be developed/written using basic operations. Actually operations should be basic, so that even they can in principle be done exactly and in a finite amount of time by a person.
Benefits of using an Algorithm ▰ Reduces the task into a series of smaller steps of more manageable size. ▰ Problems can be approached as a series of small, solvable sub- problems. ▰ Efficient.
Examples of an Algorithm ▰ Problem 1: Find the area of a Circle of radius r. ▰ Inputs to the algorithm: ▰ Radius r of the Circle. ▰ Expected output: ▰ Area of the Circle ▰ Algorithm: ▰ Step1: Start ▰ Step 2:Readinput the Radius r of the Circle ▰ Step 3: Area  PI*r*r // calculation of area ▰ Step 4: Print Area ▰ Step 5: Stop
Examples of an Algorithm Problem 2: Write an algorithm to read two numbers and find their sum. Inputs to the algorithm: ▰ First num1. ▰ Second num2 Expected output: ▰ Sum of the two numbers. Algorithm: Step1: Start Step2: Readinput the first num1. Step3: Readinput the second num2. Step4: Sum  num1+num2 // calculation of sum Step5: Print Sum Step6: End
Examples of an Algorithm Problem 3: Convert temperature Fahrenheit to Celsius Inputs to the algorithm: ▰ Temperature in Fahrenheit Expected output: ▰ Temperature in Celsius Algorithm: ▰ Step1: Start ▰ Step 2: Read Temperature in Fahrenheit F ▰ //FORMULA- T(°C) = (F - 32) × 5/9 ▰ Step 3: C  5/9*(F-32) ▰ Step 4: Print Temperature in Celsius: C
Assignment 1 Write an Alogorithm 1. To Accept Students Details And Display Them? 2. To Find Simple Interest 3. To Find Sum Of Two Numbers 4. To Find Product Of Three Numbers 5. To Find Average Of Marks Scored In 5 Subjects 6. To Find The Total Purchase Made On 4 Different Items Purchased. 7. To Find Square Of A Number 8. To Find Cube Of A Number 9. To Find Square Root Of A Number 10. To Find Area Of A Circle 11. To Find Perimeter Of A Rectangle
To Find The Total Purchase Made On 4 Different Items Purchased. ▰ Thought Process/Brainstorming ▰ Input: cost of 4 different items purchased by me ▰ Logic: sum(total) ▰ Output: totalpurchase ▰ How many input variables: 4 =itm_prc1, itm_prc2, itm_prc3, itm_prc4 ▰ How many output variables :tot_pur_cost
1. Start 2. Declare variables itm_prc1, itm_prc2, itm_prc3, itm_prc4 3. Prompt user to enter price of items purchased 4. Accept in itm_prc1, itm_prc2, itm_prc3, itm_prc4 5. Calculate tot_pur_cost- itm_prc1 6. + itm_prc2+itm_prc3+itm_prc4 7. Display tot_pur_cost 8. end
Start Declare itm_prc1, itm_prc2, itm_prc3, itm_prc4 input variables and tot_pur_cost 1 output variable Prompt User to enter cost of items purchased Accept in itm_prc1, itm_prc2, itm_prc3, itm_prc4 Calculate tot_pur_cost= itm_prc1+ itm_prc2+ itm_prc3+ itm_prc4 Display tot_pur_cost End
Thank You !!

Basics of Algorithm Unit 1 part 1 algorithm

  • 1.
    Jagannath Institute ofManagement Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: Programming In C Department of Information Technology Created By: Dr. Arpana Chaturvedi
  • 2.
    YOU CAN FINDTHE SECRET TO SUCCESS IN YOUR OWN STUDY ROOM. THINK OF THAT AND FOLLOW THAT 1. ROOF OF YOUR ROOM SAYS: AIM HIGH 2. A/C OR FAN IN THE ROOM SAYS: BE COOL 3. CLOCK SAYS: VALUE YOUR TIME, TIME NEVER COMES BACK, EVERY SECOND OF YOUR LIFE IS PRECIOUS, TIME IS MONEY 4. MIRROR SAYS: ALWAYS REFLECT BEFORE YOU ACT 5. WINDOW SAYS; LOOK AT THE WORLD. HOW BEAUTIFUL IT IS? WHAT A WONDERFUL CREATIVITY IT HAS? WE DO NOT KNOW WHO HAS CREATED. UNIMAGINATIVE POWER. 6. CALENDER SAY: BE UP-TO-DATE WITH TIME. IT IS THE DEMAND OF TODAY. YOU HAVE TO LIVE WITH LATEST TECHNOLOGY, IT CHANGING VERY FAST. WITH HIGH DEMAND AND EXPECTATION, TECHNOLOGY IS CHANGING ITSELF AND GIVING ITS BEST. WE NEED TO BE UPDATED WITH THE CHANGE. 7. DOOR SAY: PUSH HARD TO ACHIEVE. PLEDGE TO BE TAKEN
  • 3.
    ✓ UNTIL YOUSPREAD YOR WINGS, YOU WILL HAVE NO IDEA HOW FAR YOU CAN FLY. ✓ BELIEVE IN THIS THAT EVERYONE IN THIS PLANET IS LOGICAL. ✓ ONE HAS TO REALISE TO MOVE FORWARD ✓ LOGIC WILL GET YOU FROM A TO Z: IMAGINATION WILL GET YOU EVERYWHERE ✓ START TELLING YOURSELF YOU CAN ✓ EXPOSE YOURSELF TO YOUR DEEPEST FEAR, AFTER THAT, FEAR HAS NO POWER, YOU ARE FREE ✓ SAY- I CAN ACCEPT FAILURE, BUT I CANNOT ACCEPT NOT TRYING. ✓ NEVER SAY “I CANT” ASK YOURSELF “HOW CAN I” ✓ YOU ARE THE AUTHOR OF YOUR LIFE, DON’T BE AFRAID TO EDIT OR CHANGE THE SCRIPT ✓ THE QUALITY OF YOUR THINKING DETERMINES THE QUALITY OF YOUR LIFE PLEDGE TO BE TAKEN
  • 4.
    PLEDGE TO BETAKEN MAKE A PROMISE TO YOURSELF YOU WILL DO ALL PROGRAMS USING YOUR OWN LOGIC YOU WILL NEVER COPY IT FROM OTHERS YOU SOLVE YOUR ALL ERRORS BY YOURSELF YOU WILL ALWAYS ASK YOUR DOUBTS THEN AND THERE
  • 5.
    Subject: Programming InC Topic: Unit I-Algorithm
  • 6.
    Unit-I Introduction toProgramming ▰ Algorithm ▰ Properties of an Algorithms ▰ Benefits of an Algorithm ▰ Examples to Design Algorithm ▰ Assignment I
  • 7.
  • 8.
    Algorithm ▰ The termalgorithm is originally derived from the phonetic pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al- Khowarizmi, who was an Arabic mathematician who invented a set of rules for performing the four basic arithmetic operations (addition, subtraction, multiplication and division) on decimal numbers. ▰ An algorithm is a representation of a solution to a problem written in the form of simple English like statement which are logical and gives the desired output in less execution time.
  • 9.
    Algorithm ▰ Problem alwayscontains some given information and looks for some results to be obtained out of it. ▰ Hence every problem is to be understood carefully and logically about the procedure to be applied on given information so that efficiently the required output can be obtained. ▰ Here efficiency means the procedure should not consume more execution time as well as memory space. ▰ Finding of the best suitable process logic and then describing of this found procedure in English like statements is called Algorithm.
  • 10.
    More Definitions: Algorithm ▰Algorithm can be defined as: “A sequence of activities to be processed for getting desired output from a given input.” ▰ We can say that an Algorithm is a set of sequence of clear, well defined steps to be followed to solve a problem. Programming will be the final step or form to express these set of sequential instruction to reach to the solution. ▰ Webopedia defines an algorithm as: “A formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point”.
  • 11.
    Points to beconsidered while writing an Algorithm ▰ Define the required Inputs and Outputs clearly. ▰ Name of variables should be small and meanings should be clearly defined. ▰ The steps of algorithms should be sequential until not using any control statements. ▰ In case of usage of Control statement, the return of step should be clearly mentioned with step number. ▰ Getting specified output is essential after algorithm is executed. ▰ One will get output only if algorithm stops after finite time. ▰ Activities in an algorithm to be clearly defined in other words for it to be unambiguous.
  • 12.
    Properties of anAlgorithm Donald Ervin Knuth has given a list of five properties for an algorithm, these properties are: ▰ Finiteness: An algorithm must always terminate after a finite number of steps. It means after every step one reach closer to solution of the problem and after a finite number of steps algorithm reaches to an end point. ▰ Definiteness: Each step of an algorithm must be precisely defined. It is done by well thought actions to be performed at each step of the algorithm. Also the actions are defined unambiguously for each activity in the algorithm.
  • 13.
    Properties of anAlgorithm ▰ Input: Any operation you perform need some beginning value/quantities associated with different activities in the operation. So the value/quantities are given to the algorithm before it begins. ▰ Output: One always expects output/result (expected value/quantities) in terms of output from an algorithm. The result may be obtained at different stages of the algorithm. If some result is from the intermediate stage of the operation then it is known as intermediate result and result obtained at the end of algorithm is known as end result. The output is expected value/quantities always have a specified relation to the inputs
  • 14.
    Properties of anAlgorithm ▰ Effectiveness: Algorithms to be developed/written using basic operations. Actually operations should be basic, so that even they can in principle be done exactly and in a finite amount of time by a person.
  • 15.
    Benefits of usingan Algorithm ▰ Reduces the task into a series of smaller steps of more manageable size. ▰ Problems can be approached as a series of small, solvable sub- problems. ▰ Efficient.
  • 16.
    Examples of anAlgorithm ▰ Problem 1: Find the area of a Circle of radius r. ▰ Inputs to the algorithm: ▰ Radius r of the Circle. ▰ Expected output: ▰ Area of the Circle ▰ Algorithm: ▰ Step1: Start ▰ Step 2:Readinput the Radius r of the Circle ▰ Step 3: Area  PI*r*r // calculation of area ▰ Step 4: Print Area ▰ Step 5: Stop
  • 17.
    Examples of anAlgorithm Problem 2: Write an algorithm to read two numbers and find their sum. Inputs to the algorithm: ▰ First num1. ▰ Second num2 Expected output: ▰ Sum of the two numbers. Algorithm: Step1: Start Step2: Readinput the first num1. Step3: Readinput the second num2. Step4: Sum  num1+num2 // calculation of sum Step5: Print Sum Step6: End
  • 18.
    Examples of anAlgorithm Problem 3: Convert temperature Fahrenheit to Celsius Inputs to the algorithm: ▰ Temperature in Fahrenheit Expected output: ▰ Temperature in Celsius Algorithm: ▰ Step1: Start ▰ Step 2: Read Temperature in Fahrenheit F ▰ //FORMULA- T(°C) = (F - 32) × 5/9 ▰ Step 3: C  5/9*(F-32) ▰ Step 4: Print Temperature in Celsius: C
  • 19.
    Assignment 1 Writean Alogorithm 1. To Accept Students Details And Display Them? 2. To Find Simple Interest 3. To Find Sum Of Two Numbers 4. To Find Product Of Three Numbers 5. To Find Average Of Marks Scored In 5 Subjects 6. To Find The Total Purchase Made On 4 Different Items Purchased. 7. To Find Square Of A Number 8. To Find Cube Of A Number 9. To Find Square Root Of A Number 10. To Find Area Of A Circle 11. To Find Perimeter Of A Rectangle
  • 20.
    To Find TheTotal Purchase Made On 4 Different Items Purchased. ▰ Thought Process/Brainstorming ▰ Input: cost of 4 different items purchased by me ▰ Logic: sum(total) ▰ Output: totalpurchase ▰ How many input variables: 4 =itm_prc1, itm_prc2, itm_prc3, itm_prc4 ▰ How many output variables :tot_pur_cost
  • 21.
    1. Start 2. Declarevariables itm_prc1, itm_prc2, itm_prc3, itm_prc4 3. Prompt user to enter price of items purchased 4. Accept in itm_prc1, itm_prc2, itm_prc3, itm_prc4 5. Calculate tot_pur_cost- itm_prc1 6. + itm_prc2+itm_prc3+itm_prc4 7. Display tot_pur_cost 8. end
  • 22.
    Start Declare itm_prc1, itm_prc2,itm_prc3, itm_prc4 input variables and tot_pur_cost 1 output variable Prompt User to enter cost of items purchased Accept in itm_prc1, itm_prc2, itm_prc3, itm_prc4 Calculate tot_pur_cost= itm_prc1+ itm_prc2+ itm_prc3+ itm_prc4 Display tot_pur_cost End
  • 23.